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?

  1. A. V1 only
  2. B. V1 and V2 only
  3. C. V1 and V3 only
  4. 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

Exams that ask this topic