CS FundamentalsHardGATE CS

How many times is each letter printed? (Every fork() succeeds.)

How many times is each letter printed? (Every fork() succeeds.)
int main() {
    if (fork() == 0) {
        if (fork() == 0) printf("A");
        else printf("B");
    } else {
        fork();
        printf("C");
    }
}
  1. A. A 1, B 1, C 1
  2. B. A 2, B 2, C 2
  3. C. A 1, B 1, C 4
  4. D. A 1, B 1, C 2

Correct answer: D. A 1, B 1, C 2

The first child forks: its child prints A, it prints B. The original forks once more and both copies print C. Totals: A 1, B 1, C 2.

More Operating Systems questions

Exams that ask this topic