CS FundamentalsVery hardGATE CS (PYQ pattern)
Assume every fork() succeeds and each printf output is flushed immediately. int main(void) { for (int i = 0; i < 4; i++) { if (fork() == 0) printf("A"); else if (i % 2 == 1) { printf("B"); break; } } printf("C"); return 0; } How many characters are printed in total over all processes?
- A. 32
- B. 29
- C. 25
- D. 21
Correct answer: C. 25
Let f(j) be the characters output by a process entering the loop at index j: f(4) = 1; f(j) = (1 + f(j+1)) for the child plus either 2 (B and C, parent breaks, j odd) or f(j+1) (parent continues, j even). f(3) = 4, f(2) = 9, f(1) = 12, f(0) = 25.
More Operating Systems questions
- A Unix-style file system has 1 KB blocks, 4-byte block pointers and inodes with 12 direct, one single-indirect and one double-indirect…
- Holes in memory (in KB, in address order): 250, 240, 230, 180, 140. Processes arrive and stay resident in this order: 100, 190, 200, 150…
- A disk delivers 20 MB/s (1 MB = 10^6 bytes) to memory by DMA, 4 bytes per bus cycle. Each word transfer steals one 40 ns memory cycle from…
- Three threads each execute x = x + 1; twice on a shared int x that starts at 0 (so six increments in all). Each increment is a separate…
- A process has a 1 MB address space split into 4 segments, each paged separately; the single-level page tables together need one 8-byte…
- A process owns 100 private writable pages. It calls fork() and both processes use copy-on-write. Before either exits, the child writes to…