in Operating System retagged ago by
12,478 views
19 votes
19 votes

The following C program is executed on a Unix/Linux system :

#include<unistd.h>
int main()
{
    int i;
    for(i=0; i<10; i++)
        if(i%2 == 0)
            fork();
    return 0;
}

The total number of child processes created is ________________ .

in Operating System retagged ago by
by
12.5k views

4 Comments

edited by

 

5(0,2,4,6,8) times fork is called.
Total number of child process would be 2^5−1=31

1
1
HOW 0 is divided by 2 or a even no  is still a mystery right
0
0
fork will execute 5 times (i=0,2,4,6,8). so child processes created are 2^5-1=31
0
0

8 Answers

20 votes
20 votes
Best answer

Answer is $31$
Fork is called whenever $i$ is even, so we can re-write the code as

for(i=0; i<10; i=i+2)
            fork();

fork() will be called $5$ times($i=0,2,4,6,8)$

$\therefore$ Total number of process $2^5=32$ 

Total number of child process would be $2^5−1=31$

edited by

1 comment

A little addition

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

{

      fork();

}

is equivalent to

fork();

fork();
2
2
17 votes
17 votes
Total $5$ times fork is called.
Total number of child process would be $2^5 -1 = 31$
edited by
9 votes
9 votes

void main()
{
   int i;

   for (i=0;i<3;i++)
   {
      fork();
   }
}

this is an example so

 

fork() will be called 5 times(i=0,2,4,6,8) then 

so answer is 2^5-1=31.

source https://stackoverflow.com/questions/26793402/visually-what-happens-to-fork-in-a-for-loop

1 comment

good explanation
0
0
3 votes
3 votes
The fork() call is made only for the even values of i in the range 0-9(0,2,4,6,8). 5 times hence 2^5-1 =31
Answer:

Related questions