int doWork(){ fork(); fork(); printf("Hello world!\n"); } int main() { doWork(); printf("Hello world!\n"); exit(0); }
Basically it’ll execute 8 times printf() , 4 times in doWork and 4 times in main function. main func calls doWork(), doWork executes like this , exactly like this. http://web.stanford.edu/~hhli/CS110Notes/CS110NotesCollection/Topic%202%20Multiprocessing%20(1).html Practical implementation of your program fetches the below result. (GNU- GCC —7.5.0) The number in bubbles are PIDs and L1 , L2 are 1st and 2nd forks , and L3 the line 3 ,i.e , printf in doWork() If there are n forks then total processes = child processes + main-parent process = ( (2^n)-1 ) +1 = 2^n
Thank you @rd8794
64.3k questions
77.9k answers
244k comments
80.0k users