CS FundamentalsHardProduct-company MCQ pattern

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

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

Correct answer: B. 6

After the first fork there are 2 processes. In each, the parent of `fork() || fork()` gets a non-zero value and short-circuits; only the child forks again, so each process becomes 3: 2 × 3 = 6. Ignoring the short-circuit gives 2³ = 8, forgetting the first fork gives 3, and 2 counts only the first fork.

More Operating Systems questions

Exams that ask this topic