CS FundamentalsHardGATE CS (PYQ pattern)
Assume every fork() succeeds and output is unbuffered. int main(void) { int i; for (i = 0; i < 3; i++) { if (fork() == 0 && fork() == 0) continue; break; } printf("%d\n", i); return 0; } How many lines are printed, and what is the sum of the printed values?
- A. 7 lines, sum 12
- B. 7 lines, sum 9
- C. 15 lines, sum 9
- D. 6 lines, sum 6
Correct answer: B. 7 lines, sum 9
In each iteration the current process P forks a child C; P sees a non-zero value, the && short-circuits and P breaks. C forks G; C sees non-zero and breaks, G sees 0 and continues to the next iteration. So iterations 0, 1, 2 each leave two processes printing that i (0, 0, 1, 1, 2, 2), and the last G leaves the loop with i = 3. 7 lines, sum 9.
More Operating Systems questions
- Banker's algorithm with two resource types (R1, R2). Allocation: P0 (0,2), P1 (1,4), P2 (0,4), P3 (6,1), P4 (0,2). Maximum: P0 (1,4), P1…
- Banker's algorithm, a single resource type, 6 processes P0..P5. Allocation: 1, 2, 2, 5, 3, 4. Maximum: 9, 3, 3, 7, 7, 9. Available = 1…
- The head is at cylinder 100 with pending requests 55, 58, 39, 18, 90, 160, 150, 38 and 184 (cylinders 0 to 199; no new requests arrive)…
- A machine uses 4-level paging, a TLB (10 ns lookup, done before every access) and 100 ns memory. On a TLB miss the walk first consults a…
- Memory access takes 200 ns. A page fault takes 8 ms if the victim page is clean and 20 ms if it must first be written back; 60% of victim…
- Free memory consists of three holes (start, size in KB): (0, 300), (400, 200), (700, 500); everything else is allocated. Operations in…