12,886 views

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 ________________ .

edited by

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

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

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$

### 1 comment

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

{

fork();

}

is equivalent to

fork();

fork();
Total $5$ times fork is called.
Total number of child process would be $2^5 -1 = 31$ 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

### 1 comment

good explanation
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
by

1
12,693 views