CS FundamentalsVery hardAmazon · Microsoft MCQ pattern

Consider this loop:

Consider this loop:
for (int i = 0; i < 2; i++) {
    fork();
    printf("-");
}
stdout is redirected to a file (fully buffered) and no fflush is called. How many dashes appear in the file?
  1. A. 4
  2. B. 8
  3. C. 3
  4. D. 6

Correct answer: B. 8

6 printf calls run in total, but the buffer is copied by fork. At i = 1, the 2 processes each hold "-" unflushed and fork, so all 4 final processes hold "--" and flush at exit: 4 × 2 = 8. With line-buffered output and a newline it would be 6.

More Operating Systems questions

Exams that ask this topic