in Operating System retagged by
2,420 views
7 votes
7 votes

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

{

   fork();

   fork();

}

How many child process will create ?

in Operating System retagged by
2.4k views

1 Answer

28 votes
28 votes
Best answer

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

 

coming to your original question,

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

selected by

4 Comments

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

for first time i understood fork
3
3

Related questions