CS FundamentalsVery hardGATE CS (PYQ pattern)

Assume every fork() succeeds (it returns a non-zero value in the parent and 0 in the child) and each process prints X once when it reaches the end. int main(void) { if (fork() && (fork() || fork())) fork(); fork(); printf("X\n"); return 0; } How many times is X printed?

  1. A. 8
  2. B. 10
  3. C. 12
  4. D. 14

Correct answer: C. 12

The first fork's child makes the condition false. The parent evaluates (fork() || fork()): its own branch is true at once, and the child of that fork runs the second fork, giving one true and one false. So 4 processes reach the if, 2 of them true; those 2 run the body fork (6 processes), and the final fork doubles it to 12.

More Operating Systems questions

Exams that ask this topic