Synchronization & Concurrency
![Question 11(b), 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
Can a waiting process fail forever to enter? Give an execution and a corrected algorithm.](../../assets/questions/CS-1991-Q57-1.webp)
![Question 11(b), 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
Can a waiting process fail forever to enter? Give an execution and a corrected algorithm.](../../assets/questions/CS-1991-Q57-2.webp)
Answer & solution
Answer: Yes. Two contenders can spin forever when turn names an inactive third process. A three-process bakery lock is a correct replacement. (no official key was published)
Take turn=2, with process 2 remaining in its noncritical section and flag[2]=false. Let processes 0 and 1 both set their flags true before either completes its outer test. Each sees the other's true flag. Its case selects process 2, but the guarded branch sees flag[2]=false, so neither process withdraws or changes turn. They repeat forever, even under fair scheduling of all active processes. Only a critical-section exit changes turn, and neither contender can reach such an exit. Thus progress fails, not merely a timing bound. An inner waiter can also be stranded when turn is handed to an inactive process.
A safe repair is to replace the entire entry/exit protocol with Lamport's bakery algorithm, rather than merely skipping a false flag: false can mean a temporarily withdrawn waiter, not an uninterested process. Use separate choosing and ticket arrays, initially false and zero. Atomic reads/writes and sequential consistency are assumed, and mathematical tickets are unbounded integers.
For process i:
choosing[i] := true
ticket[i] := 1 + max(ticket[0], ticket[1], ticket[2])
choosing[i] := false
for r := 0 to 2, r != i:
while choosing[r]: skip
while ticket[r] != 0 and (ticket[r],r) < (ticket[i],i): skip
critical section
ticket[i] := 0
noncritical section
repeatPairs are compared lexicographically, first by ticket and then process number. The choosing flag prevents observing a half-completed ticket selection as if the process were absent. If two processes competed to enter, the one with larger pair must wait for the smaller pair's nonzero ticket; both cannot be inside together. A process which starts choosing after i has published its ticket obtains a larger ticket and cannot overtake i. Only finitely many already competing choices can precede i. Under fair scheduling, finite ticket-selection and critical-section execution, and no process failures, those predecessors finish, so i eventually enters. An inactive process has choosing=false and ticket=0 and is ignored. These assumptions are necessary: no software lock can promise entry if its current owner stops forever.