Skip to content
Official PYQ · 2019

GATE CS 2019 Q17 — System Calls

Explain with AI

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

View worked solution

Answer: 31

To find the total number of child processes created, let's analyze the execution of the loop:

The loop variable ii ranges from 00 to 99. The fork() system call is executed only when the condition i % 2 == 0 is true. This condition is satisfied for the following values of ii:

{0,2,4,6,8}\{0, 2, 4, 6, 8\}

This means there are exactly 55 iterations where a fork() is executed.

Since both the parent and the newly created child processes continue executing the loop from the next iteration, every process existing at the start of an even iteration will execute the fork() call. This doubling effect is identical to executing 55 consecutive unconditional fork() statements:

  • Initially, there is 11 parent process.
  • After the 1st1^{\text{st}} fork (at i=0i=0): 21=22^1 = 2 processes.
  • After the 2nd2^{\text{nd}} fork (at i=2i=2): 22=42^2 = 4 processes.
  • After the 3rd3^{\text{rd}} fork (at i=4i=4): 23=82^3 = 8 processes.
  • After the 4th4^{\text{th}} fork (at i=6i=6): 24=162^4 = 16 processes.
  • After the 5th5^{\text{th}} fork (at i=8i=8): 25=322^5 = 32 processes.

At the end of the program, there are 3232 total processes.

The number of child processes created is:

Total processes−Original parent process=32−1=31\text{Total processes} - \text{Original parent process} = 32 - 1 = 31

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore System Calls questions