CS FundamentalsHardGATE CS

How many times are 'c' and 'p' printed in total? (Every fork() succeeds.)

How many times are 'c' and 'p' printed in total? (Every fork() succeeds.)
int main() {
    for (int i = 0; i < 3; i++) {
        if (fork() == 0) printf("c");
        else printf("p");
    }
}
  1. A. 10 c and 3 p
  2. B. 7 c and 7 p
  3. C. 3 c and 3 p
  4. D. 14 c and 14 p

Correct answer: B. 7 c and 7 p

At iteration i there are 2^i processes, each forks once; the child prints c and the parent prints p. So each iteration prints 2^i c's and 2^i p's: total 1 + 2 + 4 = 7 of each.

More Operating Systems questions

Exams that ask this topic