CS FundamentalsVery hardGATE CS

How many processes are created in total, including the original? (Every fork() succeeds.)

How many processes are created in total, including the original? (Every fork() succeeds.)
int main() {
    for (int i = 0; i < 3; i++)
        if (fork() && fork())
            break;
    printf("x\n");
}
  1. A. 15
  2. B. 8
  3. C. 27
  4. D. 9

Correct answer: A. 15

In each iteration a looping process creates two children: the first child (fork() == 0) and the second child (second fork() == 0) both continue looping, while the original breaks. So the number of looping processes doubles each round: 1 → 2 → 4 → 8 continue past i = 2. Total processes = 1 + 2 + 4 + 8 = 15.

More Operating Systems questions

Exams that ask this topic