Szymański's algorithm


Szymański's Mutual Exclusion Algorithm is a mutual exclusion algorithm devised by computer scientist Dr. Bolesław Szymański, which has many favorable properties including linear wait, and which extension solved the open problem posted by Leslie Lamport whether there is an algorithm with a constant number of communication bits per process that satisfies every reasonable fairness and failure-tolerance requirement that Lamport conceived of.

The algorithm

The algorithm is modeled on a waiting room with an entry and exit doorway. Initially the entry door is open and the exit door is closed. All processes which request entry into the critical section at roughly the same time enter the waiting room; the last of them closes the entry door and opens the exit door. The processes then enter the critical section one by one. The last process to leave the critical section closes the exit door and reopens the entry door, so the next batch of processes may enter.
The implementation consists of each process having a flag variable which is written by that process and read by all others. The status of the entry door is computed by reading the flags of all processes. Pseudo-code is given below:

  1. Entry protocol
flag ← 1 # Standing outside waiting room
await # Wait for open door
flag ← 3 # Standing in doorway
if any flag = 1: # Another process is waiting to enter
flag ← 2 # Waiting for other processes to enter
await # Wait for a process to enter and close the door
flag ← 4 # The door is closed
await # Wait for everyone of lower ID to finish exit protocol
  1. Critical section
  2. ...
  3. Exit protocol
await # Ensure everyone in the waiting room has
# realized that the door is supposed to be closed
flag ← 0 # Leave. Reopen door if nobody is still in the waiting room

Note that the order of the "all" and "any" tests must be uniform. Also the "any" tests should be satisfied by a thread other than self. For example, if the test is any flag = 1 and only flag = 1, then the test is said to have failed/returned 0.
Despite the intuitive explanation, the algorithm was not easy to prove correct, however due to its favorable properties a proof of correctness was desirable and multiple proofs have been presented.