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?

  1. 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
  2. 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
  3. 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
  4. 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

Exams that ask this topic