CS FundamentalsHardGATE CS · Amazon/Microsoft SDE MCQ

How many times is x printed? (Every fork() succeeds, and each print is an unbuffered write.)

How many times is x printed? (Every fork() succeeds, and each print is an unbuffered write.)
int main() {
    fork();
    fork() && fork();
    fork();
    printf("x");
}
  1. A. 8
  2. B. 16
  3. C. 12
  4. D. 10

Correct answer: C. 12

The first fork gives 2 processes. In fork() && fork(), each process's parent side continues to the second fork while the child side short-circuits, so each process becomes 3. The final fork doubles: 2 × 3 × 2 = 12.

More Operating Systems questions

Exams that ask this topic