CS FundamentalsHardGATE CS (PYQ pattern)
A C program runs: int i; for(i=0;i<4;i++){ if(fork()==0 && i%2==0) break; } printf("X"); Every process, including the original, reaches the printf exactly once. Assuming all fork calls succeed, how many X characters are printed in total?
- A. 7
- B. 5
- C. 8
- D. 16
Correct answer: A. 7
Let f(i) be processes reaching printf from loop index i: f(4)=1, f(3)=f(4)+f(4)=2 (odd i: the child continues), f(2)=f(3)+1=3 (even i: the child breaks), f(1)=2·f(2)=6, f(0)=f(1)+1=7.
More Operating Systems questions
- Banker's algorithm with 3 resource types. Available = (1,1,1). Allocation rows P0..P3: (1,0,1), (0,2,0), (2,1,0), (1,1,1); Maximum rows…
- A disk has cylinders 0-199. The head is at cylinder 50 moving towards higher numbers. Pending requests: 176, 79, 34, 60, 92, 11, 41, 114…
- A 1024 KB buddy system (minimum block 32 KB, lowest-address-first allocation) serves A=70 KB, B=35 KB, C=80 KB, D=60 KB in that order…
- A system uses the clock (second-chance) page replacement with 4 frames. Initially all frames are empty; a page has its reference bit set…
- Memory access takes 200 ns. A page fault on a clean victim takes 8 ms to service and on a dirty victim 20 ms; 40% of victims are dirty…
- A system has 48-bit virtual addresses, 8 KB pages and 16 GB of physical memory. An inverted page table has one entry per frame, holding a…