CS FundamentalsVery hardGATE CS (PYQ pattern)
Assume every fork() succeeds. for (int i = 0; i < 4; i++) { if (fork() == 0 && i % 2 == 1) execl("/bin/true", "true", NULL); } printf("X\n"); How many times is X printed in total? (A successful execl never returns to this program, and /bin/true prints nothing.)
- A. 4
- B. 16
- C. 8
- D. 6
Correct answer: A. 4
Define g(j) as the processes that reach the final printf starting at index j: g(4) = 1; at an odd j the child execs away so g(j) = g(j+1); at an even j both parent and child continue so g(j) = 2 g(j+1). g(3) = 1, g(2) = 2, g(1) = 2, g(0) = 4.
More Operating Systems questions
- A machine with 4 KB pages and demand paging has a 6-byte memory-to-memory MOVE instruction stored at virtual address 0x0FFC. It copies 300…
- Six threads each take two locks in a fixed order and hold the first while acquiring the second: T1 takes A then B, T2: B then C, T3: C…
- Four processes (arrival, burst) in ms: A (0, 7), B (1, 2), C (2, 1), D (2, 2). Scheduling is non-preemptive. Compare non-preemptive SJF…
- A 2-slot bounded buffer uses mutex = 1, empty = 2, full = 0. Two producers each insert 2 items with the (buggy) code wait(mutex)…
- SRTF (preemptive shortest remaining time first; ties go to the earlier arrival) is used for processes (arrival, burst) in ms: P1 (0, 9)…
- Preemptive priority scheduling (a smaller number means higher priority; equal priorities are served by earlier arrival, without time…