retagged by
17,129 views
24 votes
24 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 ________________ .

retagged by

8 Answers

Best answer
29 votes
29 votes

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
13 votes
13 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

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

25 votes
25 votes
6 answers
1
40 votes
40 votes
4 answers
2
18 votes
18 votes
4 answers
3
13 votes
13 votes
4 answers
4
Arjun asked Feb 7, 2019
9,562 views
Consider the following C program :#include<stdio.h int jumble(int x, int y){ x = 2*x+y; return x; } int main(){ int x=2, y=5; y=jumble(y,x); x=jumble(y,x); printf("%d \n"...