please elaborate.

The Gateway to Computer Science Excellence

+19 votes

A process executes the following code

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

The total number of child processes created is

- $n$
- $2^n-1$
- $2^n$
- $2^{n+1} - 1$

+30 votes

Best answer

Each fork() creates a child which start executing from that point onward. So, number of child processes created will be $2^n - 1$.

At each fork, the number of processes doubles like from $1 - 2- 4 - 8 ... 2^n$. Of these except $1$, all are child processes.

Reference: https://gateoverflow.in/3707/gate2004-it_64

0 votes

F0 // There will be 1 child process created by first fork / \ F1 F1 // There will be 2 child processes created by second fork / \ / \ F2 F2 F2 F2 // There will be 4 child processes created by third fork / \ / \ / \ / \ ............... // and so on

If we sum all levels of above tree for i = 0 to n-1, we get 2n - 1. So there will be 2n – 1 child processes. On the other hand, the total number of

**Note:**The maximum number of process is 2n and may vary due to fork failures.Also see this post for more details.

52,345 questions

60,468 answers

201,792 comments

95,271 users