GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 11(a)

Mutual exclusion of the three-process flag scheme

Operating Systems · Synchronization & Concurrency · Full GATE CSE 1991 paper

GATE 1991, Q11(a) 5 marks · Descriptive
Report issue

Synchronization & Concurrency

Question 11(a), GATE 1991: Shared atomic variables: flag[0..2] initially false; turn in {0,1,2}. For process i let j=(i+1) mod 3 and k=(i+2) mod 3. Repeat:
  flag[i] := true
  while flag[j] or flag[k]:
    case turn:
      j: if flag[j]:
           flag[i] := false
           while turn != i: skip
           flag[i] := true
      k: if flag[k]:
           flag[i] := false
           while turn != i: skip
           flag[i] := true
  critical section
  if turn == i: turn := j
  flag[i] := false
  noncritical section
Does this scheme ensure mutual exclusion? Explain.
Question 11(a), GATE 1991: Shared atomic variables: flag[0..2] initially false; turn in {0,1,2}. For process i let j=(i+1) mod 3 and k=(i+2) mod 3. Repeat:
  flag[i] := true
  while flag[j] or flag[k]:
    case turn:
      j: if flag[j]:
           flag[i] := false
           while turn != i: skip
           flag[i] := true
      k: if flag[k]:
           flag[i] := false
           while turn != i: skip
           flag[i] := true
  critical section
  if turn == i: turn := j
  flag[i] := false
  noncritical section
Does this scheme ensure mutual exclusion? Explain.
Tap to enlarge
Answer & solution

Answer: Yes, under atomic reads/writes and sequential consistency; this does not guarantee progress. (no official key was published)

First separate safety from liveness. A process sets its flag true before testing the outer while condition, and a process admitted to its critical section keeps that flag true until after leaving the critical section. Withdrawing its flag happens only inside the entry loop, never while it is in the critical section.

Suppose two distinct processes p and q were simultaneously in their critical sections. Consider the final assignments flag[p]=true and flag[q]=true preceding these particular admissions. Assume p's assignment occurred first. Its flag remains true from that assignment through its critical section: if it had withdrawn and raised the flag again, the assignment chosen was not final. Process q performs its successful outer test after its own final assignment, hence after p's final assignment. If q enters while p is still inside, q must encounter flag[p]=true and cannot pass the test. If p has already left when q reads false, the two critical sections do not overlap. This contradiction proves mutual exclusion. The argument works with separate atomic flag reads as well: a continuously true competing flag cannot be read as false.

The turn variable controls who backs off, but the persistent true flag is the safety invariant. No fairness assumption is needed for this safety proof. Atomic coherent shared-memory operations are needed; the bare pseudocode is not automatically a correct implementation on a weak-memory machine without suitable synchronization.

Original paper, page 7