CS FundamentalsModerateTCS NQT · Infosys pattern

How many times is "hi" printed? (Assume every fork() succeeds.)

How many times is "hi" printed? (Assume every fork() succeeds.)
int main() {
    for (int i = 0; i < 3; i++)
        fork();
    printf("hi\n");
    return 0;
}
  1. A. 8
  2. B. 7
  3. C. 6
  4. D. 3

Correct answer: A. 8

Each fork doubles the number of processes, so after 3 forks there are 2³ = 8 processes and each prints once. 7 is the number of child processes created, not the number of printers.

More Operating Systems questions

Exams that ask this topic