CS FundamentalsHardGATE CS

How many processes print "x" (including the original)? (Every fork() succeeds.)

How many processes print "x" (including the original)? (Every fork() succeeds.)
int main() {
    for (int i = 0; i < 4; i++)
        if (fork() != 0)
            break;
    printf("x\n");
}
  1. A. 16
  2. B. 4
  3. C. 8
  4. D. 5

Correct answer: D. 5

Each parent breaks out after one fork; only the newest child keeps looping. This builds a chain: original + 4 children = 5 processes, not 2⁴ = 16.

More Operating Systems questions

Exams that ask this topic