2,420 views

for ( i = 1; i<=3; i++ )

{

fork();

fork();

}

How many child process will create ?

Before going to this question, let analyze

for(i=1;i<=3;i++)

{

fork();

}

it is equivalent to

1.fork();

2.fork();

3.fork();

using fork() system call we create one more process ===> double the no.of processes

before the 1st line ===> totally you have 1 process

after the first line  ===> totally you have 2 processes

after the second line  ===> totally you have 4 processes

after the third line  ===> totally you have 8 processes ===> total = 2n processes

in this 8 process, 1 parent process, 3 intermediate processes , and 4 leave processes

Child processes = 3+4 = 7 ( note that every intermediate process is a child process for some process ) = 2n-1 processes

for(i=1;i<=3;i++)

{

fork();
fork();

}

it is equivalent to

1.fork();

fork();

2.fork();

fork();

3.fork();

fork();

it is equivalent to, let m=2n

for(i=1;i<=m;i++)

{

fork();

}

===> child processes = 2m-1 =  22n-1 =  26-1 = 63

what a beautiful explanation
It should be best answer :3
@shaikh bro thnxx for this explanation :)

for first time i understood fork

1 vote