CS FundamentalsHardGATE CS
What is printed (ignoring order)? (Every fork() succeeds.)
What is printed (ignoring order)? (Every fork() succeeds.)
int main() {
for (int i = 0; i < 3; i++) {
if (fork() == 0) {
printf("%d ", i);
exit(0);
}
}
for (int i = 0; i < 3; i++) wait(NULL);
}- A. 0 1 1 2 2 2 2
- B. 0 1 2 0 1 2
- C. 0 1 2
- D. 0 0 0
Correct answer: C. 0 1 2
Each child prints its i and exits immediately, so only the parent keeps forking: three children print 0, 1 and 2 once each (in some order). Without exit(0), children would continue the loop.
More Operating Systems questions
- How many numbers are printed in total, and what is their sum? (Every fork() succeeds; output unbuffered.) c int main() { for (int i = 0; i…
- Round Robin, quantum 2, processes (arrival, burst): A (0, 3), B (1, 5), C (2, 2), D (3, 4), E (4, 1). A process arriving exactly when a…
- Processes spend 60% of their time waiting for I/O, independently. Using CPU utilisation = 1 − pⁿ, what is the minimum degree of…
- How many of these statements about CPU scheduling are correct? 1. SJF minimises average waiting time among non-preemptive algorithms when…
- Three jobs of 10 ms each arrive at time 0. Compare the average turnaround time under FCFS and under Round Robin with a 1 ms quantum (no…
- Assertion (A): Shortest Remaining Time First is optimal for average waiting time when preemption is free. Reason (R): Running the job with…