769 views

2 Answers

2 votes
2 votes

After first fork there will be one child. After second fork, 2 new child processes (as now 2 processes are executing fork)

So at level $i$ (starting from 0) we have $2^{i}$ child processes newly created.

In the given code we have 4 levels and thus $ 1 + 2 + 4 + 8 = 15$ child processes plus 1 parent process. 

In general it will form  complete binary tree when fork is executed in a loop without any further condition. 

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

No. of child processes created $=2^{n}-1$. 

0 votes
0 votes
this is not a question from any book , i just want to verify my understanding how process are getting repliacted at each level

Assuming parent (root at level 1)

So node at level 1 = 1

level 2= 4

level 3 = 6

level 4 = 4

level 5 = 1

And the no of node at each level is also equal to no of print statement at each level .

total process created is 16

Related questions

2 votes
2 votes
3 answers
2
Philosophical_Virus asked Dec 10, 2023
898 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
0 votes
0 votes
1 answer
3
Erwin Smith asked Apr 11, 2023
786 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...