A process executes the following segment of code :
for(i = 1; i < = n; i++) fork ();
The number of new processes created is
(A) n
(B) ((n(n + 1))/2)
(C) 2n – 1
(D) 3n – 1
Answer: (C)
Explanation:
fork (); // Line 1 fork (); // Line 2 fork (); // Line 3 .....till n L1 // There will be 1 child process created by line 1 / \ L2 L2 // There will be 2 child processes created by line 2 / \ / \ L3 L3 L3 L3 // There will be 4 child processes created by line 3 ........
We can also use direct formula to get the number of child processes.
With n fork statements, there are always 2n – 1 child processes. Also see this post for more details.
Quiz of this Question