CS FundamentalsVery hardGATE CS (PYQ pattern)
Two processes use shared variables flag[2] (initially 0) and turn. Process i (j = 1−i) runs: V1: flag[i]=1; turn=j; while(flag[j] && turn==j); CS; flag[i]=0. V2: turn=j; flag[i]=1; while(flag[j] && turn==j); CS; flag[i]=0. V3: flag[i]=1; turn=i; while(flag[j] && turn==i); CS; flag[i]=0. Which variants guarantee mutual exclusion for any interleaving of atomic reads and writes?
- A. V1 only
- B. V1 and V2 only
- C. V1 and V3 only
- D. V1, V2 and V3
Correct answer: C. V1 and V3 only
V1 is Peterson's algorithm; V3 merely flips the meaning of turn (the last writer yields), so it is also correct. In V2: P0 sets turn=1, P1 sets turn=0, P1 sets flag[1] and sees flag[0]=0 so enters; P0 then sets flag[0], sees turn==0 (not 1) and enters too, violating mutual exclusion.
More Operating Systems questions
- Binary semaphore m is initialised to 1. P1: P(m); print A; V(m); print B. P2: P(m); print C; V(m); print D. P3: P(m); print E; V(m). All…
- Priority-based preemptive scheduling with L (low), M (medium), H (high). L arrives at t=0 and needs 10 units; at t=1 L locks a mutex and…
- 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…
- 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…