retagged by
29,091 views
44 44 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 ________________ .

11 Answers

Best answer
52 52 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
4 4 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
1 1 vote
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
Answer:
Position:
Show:

Related questions

46 46 votes
7 answers 7 answers
30.5k
30.5k views
Arjun asked Feb 7, 2019
30,518 views
Consider three concurrent processes $P_1, P_2$ and $P_3$ as shown below, which access a shared variable $D$ that has been initialized to $100.$ $$\begin{array}{|c|c|c|} \...
74 74 votes
6 answers 6 answers
47.5k
47.5k views
Arjun asked Feb 7, 2019
47,469 views
Consider the following four processes with arrival times (in milliseconds) and their length of CPU bursts (in milliseconds) as shown below:$$\begin{array}{|c|c|c|c|c|} \h...
39 39 votes
6 answers 6 answers
35.9k
35.9k views
Arjun asked Feb 7, 2019
35,857 views
The index node (inode) of a Unix -like file system has $12$ direct, one single-indirect and one double-indirect pointers. The disk block size is $4$ kB, and the disk bloc...
115 115 votes
12 answers 12 answers
39.3k
39.3k views
Arjun asked Feb 7, 2019
39,297 views
Assume that in a certain computer, the virtual addresses are $64$ bits long and the physical addresses are $48$ bits long. The memory is word addressible. The page size i...