Skip to main content

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

GATE CSE 2019 PYQ: The following C program is executed on a Unix/Linux system : #include <unistd.h> int main () { int i ; for (…

The following C program is executed on a Unix/Linux system :

  1. #include<unistd.h>
  2. int main()
  3. {
  4. int i;
  5. for(i=0; i<10; i++)
  6. if(i%2 == 0)
  7. fork();
  8. return 0;
  9. }

The total number of child processes created is ________________ .

    Answer

    31

    Approach

    Count how many times fork() is actually executed, then use the fact that each successful fork() doubles the number of processes. The number of child processes is total processes minus the original parent.

    Since fork() executes for i = 0, 2, 4, 6, 8, there are 5 fork calls. Five fork calls produce $2^5 = 32$ total processes including the original parent, hence 32 - 1 = 31 child processes.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.