8,729 views

A process executes the following segment of code :

for(i = 1; i <= n; i++)
fork ();

The number of new processes created is

1. $n$
2. $((n(n + 1))/2)$
3. $2^n - 1$
4. $3^n - 1$

Total count print is 8 (one for the main function)

So, the answer is $2^{3} -1 = 7$

But why static variable count is printing only 1 ????

@Deepak Poonia @Arjun Sir

@Abhrajyoti00

Ans: C (2^n)-1

For some people who get confused without proper visual representation as to how no. of children are doubling at each level, this tree should help you out.

Interpretation: Main process on calling its first fork, creates a child process ”1”, and now both main and child “1” have 2 forks() in them. Therefore the 2nd level in the tree is representing the two processes that are active after the 1st fork. Notice that only one child is created by the first fork, which is marked by a square box. Total square boxes are doubling at each level, as shown by the figure to the right of the tree.

I hope this helps

by