Operating Systems · Question 23 · Medium

How many times is "A" printed when this C program runs? Assume every fork() call succeeds.

#include <stdio.h>
#include <unistd.h>

int main() {
    for (int i = 0; i < 2; i++) {
        fork();
        printf("A\n");
        fflush(stdout);
    }
    return 0;
}
  1. 4
  2. 8
  3. 3
  4. 6
Answer and explanation

Answer: D. 6

Explanation: In the first iteration the original process forks, and 2 processes print A. In the second iteration each of those 2 forks, and 4 processes print A. Total = 2 + 4 = 6. fflush ensures no buffered output is copied into a child.

Previous: Process (arrival time, burst time) in ms: P1 (0, 7), P2 (2, 4), P3 (4, 1), P4… · Next: A disk has cylinders 0 to 199 and the head is at cylinder 50. The request…

Practise operating systems

Try a fresh set of operating systems questions with an explanation after every answer, free and without signing up: Operating Systems practice. Or sit a full placement mock test.

More operating systems questions

More technical & programming practice

Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code · C Programming · Java · Python

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge