CS FundamentalsVery hardGATE CS (PYQ pattern)

Assume every fork() succeeds. for (int i = 0; i < 4; i++) { if (fork() == 0 && i % 2 == 1) execl("/bin/true", "true", NULL); } printf("X\n"); How many times is X printed in total? (A successful execl never returns to this program, and /bin/true prints nothing.)

  1. A. 4
  2. B. 16
  3. C. 8
  4. D. 6

Correct answer: A. 4

Define g(j) as the processes that reach the final printf starting at index j: g(4) = 1; at an odd j the child execs away so g(j) = g(j+1); at an even j both parent and child continue so g(j) = 2 g(j+1). g(3) = 1, g(2) = 2, g(1) = 2, g(0) = 4.

More Operating Systems questions

Exams that ask this topic