CS FundamentalsVery hardGATE CS · Amazon/Microsoft SDE MCQ

What is the sum of all values printed? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)

What is the sum of all values printed? (Every fork() succeeds, and each print is an unbuffered write, so nothing is printed twice through copied buffers.)
int main() {
    int c = 0;
    for (int i = 0; i < 3; i++)
        if (fork()) c++;
    printf("%d\n", c);
}
  1. A. 8
  2. B. 12
  3. C. 24
  4. D. 7

Correct answer: B. 12

8 processes print, and each process's c counts how many of its 3 forks returned the parent side, i.e. a 3-bit pattern. The counts follow 0, 1, 1, 1, 2, 2, 2, 3, summing to 12.

More Operating Systems questions

Exams that ask this topic