CS FundamentalsVery hardGATE CS (PYQ pattern)
Assume every fork() succeeds (it returns a non-zero value in the parent and 0 in the child) and each process prints X once when it reaches the end. int main(void) { if (fork() && (fork() || fork())) fork(); fork(); printf("X\n"); return 0; } How many times is X printed?
- A. 8
- B. 10
- C. 12
- D. 14
Correct answer: C. 12
The first fork's child makes the condition false. The parent evaluates (fork() || fork()): its own branch is true at once, and the child of that fork runs the second fork, giving one true and one false. So 4 processes reach the if, 2 of them true; those 2 run the body fork (6 processes), and the final fork doubles it to 12.
More Operating Systems questions
- Assume every fork() succeeds and output is unbuffered. int main(void) { int i; for (i = 0; i < 3; i++) { if (fork() == 0 && fork() == 0)…
- 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…