Operating Systems · Question 10 · Medium

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

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

int main() {
    if (fork() == 0)
        fork();
    printf("Hi\n");
    return 0;
}
  1. 2
  2. 4
  3. 3
  4. 8
Answer and explanation

Answer: C. 3

Explanation: The parent gets a non-zero value from fork(), so it skips the inner fork and prints once. The child gets 0 and calls fork() again, after which the child and the grandchild each print. That is 3 prints in total.

Previous: A counting semaphore S is initialised to 8. Then 11 wait (P) operations and 5… · Next: In which memory-management scheme does internal fragmentation occur?

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