CS FundamentalsHardGATE CS

How many times is "x" printed? (Every fork() succeeds.)

How many times is "x" printed? (Every fork() succeeds.)
int main() {
    pid_t a = fork();
    pid_t b = fork();
    if (a > 0 && b > 0)
        fork();
    printf("x\n");
}
  1. A. 5
  2. B. 4
  3. C. 6
  4. D. 8

Correct answer: A. 5

After two forks there are 4 processes; only the original (a > 0 and b > 0) forks again, adding 1. Total 5.

More Operating Systems questions

Exams that ask this topic