Skip to main content

GATE CSE 2008Operating System › Process Concepts, Threads & System CallsVerified

GATE CSE 2008 PYQ: A process executes the following code for ( i = 0 ; i < n ; i ++) fork (); The total number of child…

A process executes the following code

for(i=0; i<n; i++) fork();

The total number of child processes created is

  1. A
    nn
  2. B
    2n−12^n-1
    Correct answer
  3. C
    2n2^n
  4. D
    2n+1−12^{n+1} - 1

Answer

(B) 2 n − 1 2^n-1 2 n − 1

Approach

Trace the loop as a process-creation tree. In every iteration of the for loop, every process currently in the system executes fork(). Thus the number of processes doubles after each iteration: starting with 1 process, after n iterations there are $2^n$ processes in total. Exactly one of these is the original parent, and all the rest are child processes (descendants), so the number of child processes is $2^n - 1$.

Each fork() call doubles the number of processes because both the parent and the newly created child continue executing the same loop. After n iterations there are $2^n$ processes total. Since exactly one process is the original parent and all others are children, the number of child processes is $2^n - 1$. Therefore option B is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.