CS FundamentalsModerateGATE CS

What is printed? (fork succeeds.)

What is printed? (fork succeeds.)
int x = 10;
int main() {
    if (fork() == 0) { x += 5; printf("%d ", x); }
    else { wait(NULL); x -= 5; printf("%d ", x); }
}
  1. A. 15 10
  2. B. 5 15
  3. C. 15 5
  4. D. 15 15

Correct answer: C. 15 5

The child gets its own copy of x, so its change does not affect the parent. wait() makes the parent print only after the child exits: 15 then 5.

More Operating Systems questions

Exams that ask this topic