1,359 views

What is the number of child process created ?

Answer given was 63, BUT i am getting 9 !

When the first fork() call is executed the parent copies itself. So now we have two process executing. These two when execute the 2nd fork call will result in 4 processes. At 3rd fork 2*4=8 and so on. Total 64 processes would be created. Out of 64 one is parent the other are children. Hence 63 is the answer.

fork()

fork()

fork()

fork()

fork()

fork()

use formula = 2n-1 = 26-1 = 63

No of child process created = $2^{2*3} - 1$ = 63.
ok thanks

Here's a rough explanation,

In the above figure, you can see that after every fork() there is a formation of 2 processes. And at the end we can also see that there are a total of 64 processes. But the process that started this whole chain is the first one(which is also the only supreme parent to all these children).

So we conclude that 26 - 1 = No. of children of the process.

During 1st iteration (i=0), there is 2 fork() calls so there will be 4 processes.

In the 2nd iteration (i=1), those 4 processes will execute 2 fork() calls i.e. 4 processes, so there will be 4*4 processes i.e. 16.

Similarly, in the 3rd iteration (i=2), those 16 processes will execute 2 fork() calls i.e. 4 processes, so there will be 16*4 = 64 processes.

Now the question is asking for number of child processes, so 64 – 1 = 63.

Hence the answer is 63 child processes.

by