CS FundamentalsHardGATE CS

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

How many times is "x" printed? (Every fork() succeeds; output unbuffered.)
int main() {
    for (int i = 0; i < 2; i++) {
        if (fork() == 0) continue;
        printf("x");
    }
}
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: B. 3

In each iteration every existing process forks; the parent prints and the child continues. Iteration 0: 1 parent prints; iteration 1: 2 processes each fork and print once. Total = 1 + 2 = 3.

More Operating Systems questions

Exams that ask this topic