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;
}
- 2
- 4
- 3
- 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
- Process (arrival time, burst time) in ms: P1 (0, 8), P2 (1, 4), P3 (2, 2), P4…
- A system has 3 page frames, all initially empty. For the page reference string…
- A counting semaphore S is initialised to 8. Then 11 wait (P) operations and 5…
- In which memory-management scheme does internal fragmentation occur?
- A paged system has a page size of 4 KB, a logical address space of 64 pages…
- A system has 3 page frames, all initially empty. For the page reference string…
- A TLB lookup takes 20 ns and a main-memory access takes 100 ns. On a TLB miss…
- Two processes enforce mutual exclusion using only a shared variable turn…
- The Banker's algorithm is used for which deadlock-handling strategy?
- Which of the following is NOT shared between threads of the same process?
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