The Gateway to Computer Science Excellence
+9 votes
3.7k 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 ________________ .

in Operating System by Veteran (432k points)
edited by | 3.7k views
+3
31
0
Can anybody explain me the concept step by step, I am not able to digest the formula.

TIA.
0
0

 

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

7 Answers

+5 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$

by Active (1.5k points)
selected by
+13 votes
Total $5$ times fork is called.
Total number of child process would be $2^5 -1 = 31$
by Veteran (60.9k points)
edited by
+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
by Active (1.2k points)
0 votes
From 0 to 9, 0,2,4,6,8 are the values which will satisfy if condition. so fork will be called 5 times. total number of child processes will be 2^5-1=31
by (429 points)
0 votes
2^5 - 1 = 32 - 1 = 31 child processes.
by (11 points)
0 votes
fork is called at 0 , 2 ,4 ,6 ,8 i.e total 5 time

So total number of child is  2^5-1 = 31.
by Junior (629 points)
0 votes
i is initialized as 0 and it should be incremented by 2 and should go up to less than 10.

consider this code:

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

                                  fork();

fork ( is a function) shall be called 5 times(i=0,2,4,6,8) equal to the no. of even numbers less than 10

∴ Total number of process 2*2*2*2*2 =32 

Total number of child process shall be 32−1=31

ago by (17 points)
reshown ago by
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,390 answers
198,586 comments
105,435 users