CS FundamentalsHardGATE CS

What values are printed (in any order)? (Every fork() succeeds.)

What values are printed (in any order)? (Every fork() succeeds.)
int main() {
    int x = 0;
    for (int i = 0; i < 2; i++) {
        if (fork() == 0) x += 1;
        else x += 10;
    }
    printf("%d ", x);
}
  1. A. 2, 11, 11, 20
  2. B. 2, 11, 20
  3. C. 1, 10, 11, 20
  4. D. 0, 1, 10, 11

Correct answer: A. 2, 11, 11, 20

Each process makes two decisions (child: +1, parent: +10). The four processes take the four combinations: parent-parent 20, parent-child 11, child-parent 11, child-child 2.

More Operating Systems questions

Exams that ask this topic