in Operating System
1,390 views
0 votes
0 votes

int doWork(){
       fork();
       fork();
       printf("Hello world!\n");
}
int main() {
       doWork();
       printf("Hello world!\n");
       exit(0);
}

in Operating System
1.4k views

1 Answer

2 votes
2 votes
Best answer

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

selected by

1 comment

Thank you @rd8794

0
0

Related questions

1 vote
1 vote
2 answers
3
Warrior asked in Operating System Nov 10, 2018
1,350 views
Warrior asked in Operating System Nov 10, 2018
by Warrior
1.4k views