CS FundamentalsVery hardGATE CS

How many times is "x" printed? (Every fork() succeeds.)

How many times is "x" printed? (Every fork() succeeds.)
int main() {
    if (fork() || fork())
        fork();
    printf("x\n");
}
  1. A. 5
  2. B. 4
  3. C. 6
  4. D. 8

Correct answer: A. 5

fork() || fork() yields 3 processes: the original (true), the first child whose second fork returned nonzero (true), and the grandchild (false). The two true ones fork once more: 3 + 2 = 5.

More Operating Systems questions

Exams that ask this topic