GATE - 2019 | The following C program is executed on a Unix/Linux system:

GATE - 2019 | The following C program is executed on a Unix/Linux system:
Posted on 02-02-2022

GATE - 2019 [Operating System]

Question: 

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

Solution 1:

Fork( ) statement is executed when 'i' is even. Thus the above code is equivalent to
#include
int main( )
{
int i;
fork( );
fork( );
fork( );
fork( );
fork( );
}
n - fork statements will have 2n-1 child.
Hence, n = 5 ⇒ We have 31 child processes.

-----------------------------------------------------------------

Solution 2:

Condition “if” will be satisfy for i = 0, 2, 4, 6, 8 only. So, “fork()” will call for 5 times.

Total number of processes created is,

= 2number of fork() 

There is always only one parent process of these processes and remaining will be child processes.

Hence number of child process created is,

= 2number of fork() - 1
= 25 - 1
= 32 - 1
= 31


Thank You
  1. GATE - 2019 | Consider three concurrent processes P1, P2 and P3 as shown below
  2. GATE - 2019 | The following C program is executed on a Unix/Linux system:
  3. GATE - 2019 | Assume that in a certain computer, the virtual addresses are 64 bits long
  4. GATE - 2019 | Consider the following four processes with arrival times (in milliseconds)