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())
        fork();
    printf("x\n");
}
  1. A. 9
  2. B. 7
  3. C. 8
  4. D. 12

Correct answer: C. 8

(fork() && fork()) || fork() leaves 5 processes; for each, the condition is true unless it is the child of the last fork(), which got 0. Four of the five have a true condition and fork once more: 5 + 4 = 8.

More Operating Systems questions

Exams that ask this topic