CS FundamentalsVery hardGATE CS (PYQ pattern)
Two processes i and j (j is the other one) use shared flag[2] (initially false) and turn to guard a critical section. Entry code of four candidate protocols: (I) flag[i]=true; turn=j; while (flag[j] && turn==j); (II) turn=j; flag[i]=true; while (flag[j] && turn==j); (III) flag[i]=true; turn=i; while (flag[j] && turn==i); (IV) flag[i]=true; turn=j; while (flag[j] || turn==j); The exit code is flag[i]=false. All loads and stores are atomic and sequentially consistent. Which statement is correct?
- A. Only I is correct; III violates mutual exclusion because turn is set to the process's own id, and II and IV violate it as well, in different ways
- B. I and III satisfy mutual exclusion and progress; II violates mutual exclusion; IV keeps mutual exclusion but can block a process although the other is outside its critical section
- C. I, II and III are all correct because the order of two assignments is irrelevant; only IV can block a process when the other is not competing
- D. I and II are correct, since swapping the two assignments does not matter; III and IV both violate mutual exclusion because of the own-id turn value
Correct answer: B. I and III satisfy mutual exclusion and progress; II violates mutual exclusion; IV keeps mutual exclusion but can block a process although the other is outside its critical section
Exhaustive state exploration: I (Peterson) and III (turn names the process that yields, so the last writer waits) are correct; II lets both enter (turn=j is overwritten, flag not yet visible); IV keeps mutual exclusion but the || makes a process spin on a stale turn value after the other has left and cleared its flag.
More Operating Systems questions
- Arrays A and B are 128 x 128 int matrices stored row-major, 4-byte ints, 512-byte pages (each row occupies exactly one page, A and B…
- A Unix-like file system has 1 KB blocks, 4-byte pointers, and inodes with 10 direct pointers and one single-indirect pointer. Every…
- Banker's algorithm with 3 resource types. Allocation: P0 (1,0,0), P1 (1,2,0), P2 (0,0,0), P3 (1,4,3), P4 (3,1,1). Maximum: P0 (5,3,2), P1…
- Every fork() succeeds, wait() blocks until some child has terminated (it returns at once if the caller has no un-waited children), and…
- A buddy allocator manages 1024 KB (minimum block 16 KB, requests rounded up to powers of two; the lowest-addressed free block of the…
- A 128 KB physically-tagged, virtually-indexed L1 cache has 64-byte lines and is 16-way set-associative. The system uses 4 KB pages. How…