Barry Barry - 4 years ago 116
C Question

How many child processes are created?

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


How many child processes are created in the above code ?




My attempt :

I first solved the inner loop, and got total 32 processes in which 31 are child processes and 1 parent process.

Then, I went for outer loop which loops 5 times, thus producing 31 * 5 = 155 child processes.




Have I got this right ?

Answer Source
 for(int i=0; i<n; i++)
     fork();

For such kind of situation the total no of processes created is always 2^n - 1 as fork() will get called by n time.

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

For this double for-loop, fork() gets called by n^2 times, Hence total no of processes created will be,

2^(n^2) - 1

What is important for such questions is to calculate the no of times your fork() gets called.

For you case n=5 so total no of child processes will be 2^25 - 1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download