• retagged by
29,272 views
45 45 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

48 48 votes
7 answers 7 answers
30.8k
30.8k views
Arjun asked Feb 7, 2019
30,752 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|} \...
76 76 votes
6 answers 6 answers
47.7k
47.7k views
Arjun asked Feb 7, 2019
47,694 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...
40 40 votes
6 answers 6 answers
36.1k
36.1k views
Arjun asked Feb 7, 2019
36,096 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.6k
39.6k views
Arjun asked Feb 7, 2019
39,595 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...