CS FundamentalsHardGATE CS (PYQ pattern)
Assume every fork() succeeds and output is not lost or duplicated. int x = 1; int main(void) { int *p = &x; if (fork() == 0) { *p += 2; if (fork() == 0) x *= 3; } else { wait(NULL); x += 10; } printf("%d ", x); return 0; } What is the sum of all the numbers printed?
- A. 23
- B. 37
- C. 14
- D. 29
Correct answer: A. 23
Each process has its own copy of x (p points into the caller's own copy). The child makes x = 3 and prints 3; its child (the grandchild) inherits 3, makes 9 and prints 9; the original parent still has x = 1 and prints 11 after waiting. Sum = 3 + 9 + 11 = 23. Treating x as shared memory gives values like 9, 9, 19 (37); forgetting the grandchild gives 14.
More Operating Systems questions
- Three periodic tasks (execution time, period = relative deadline), all released at t = 0: T1 (2, 6), T2 (3, 8), T3 (3, 12). They are…
- Which of the following statements about system calls and interrupts are correct? I. A system call enters the kernel through a trap…
- Which of the following statements about disk scheduling are correct? I. SSTF can starve requests far from the head. II. SCAN bounds the…
- A process maps a region starting at virtual address 0x40000000 whose size is 4 GB + 5 MB + 12 KB. The OS uses 1 GB, 2 MB and 4 KB pages…
- A 32-bit machine has 512 MB of RAM, of which the resident kernel permanently uses 128 MB, and a 1 GB swap partition. The top 1 GB of every…
- On a single-CPU system processes arrive at 3 per second in steady state, on average 6 processes sit in the ready queue (not counting the…