Peterson's algorithm


Peterson's algorithm is a concurrent programming algorithm for mutual exclusion that allows two or more processes to share a single-use resource without conflict, using only shared memory for communication. It was formulated by Gary L. Peterson in 1981. While Peterson's original formulation worked with only two processes, the algorithm can be generalized for more than two.

The algorithm

The algorithm uses two variables, flag and turn. A flag value of true indicates that the process n wants to enter the critical section. Entrance to the critical section is granted for process P0 if P1 does not want to enter its critical section or if P1 has given priority to P0 by setting turn to 0.


The algorithm satisfies the three essential criteria to solve the critical section problem, provided that changes to the variables turn, flag, and flag propagate immediately and atomically. The while condition works even with preemption.
The three criteria are mutual exclusion, progress, and bounded waiting.
Since turn can take on one of two values, it can be replaced by a single bit, meaning that the algorithm requires only three bits of memory.

Mutual exclusion

P0 and P1 can never be in the critical section at the same time: If P0 is in its critical section, then flag is true. In addition, either flag is false, or turn is 0, or P1 is at label P1_gate. So if both processes are in their critical sections then we conclude that the state must satisfy flag and flag and turn = 0 and turn = 1. No state can satisfy both turn = 0 and turn = 1, so there can be no state where both processes are in their critical sections.

Progress

Progress is defined as the following: if no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in making the decision as to which process will enter its critical section next. Note that for a process or thread, the remainder sections are parts of the code that are not related to the critical section. This selection cannot be postponed indefinitely. A process cannot immediately re-enter the critical section if the other process has set its flag to say that it would like to enter its critical section.

Bounded waiting

Bounded waiting, or bounded bypass means that the number of times a process is bypassed by another process after it has indicated its desire to enter the critical section is bounded by a function of the number of processes in the system. In Peterson's algorithm, a process will never wait longer than one turn for entrance to the critical section.

Filter algorithm: Peterson's algorithm for more than two processes

The filter algorithm generalizes Peterson's algorithm to processes. Instead of a Boolean flag, it requires an integer variable per process, stored in a single writer/multiple reader atomic register, and additional variables in similar registers. The registers can be represented in pseudocode as arrays:
level : array of N integers
last_to_enter : array of N−1 integers
The variables take on values up to, each representing a distinct "waiting room" before the critical section. Processes advance from one room to the next, finishing in room which is the critical section. Specifically, to acquire a lock, process executes
i ← ProcessNo
forfrom 0 to N−1 exclusive
level ← ℓ
last_to_enter ← i
while last_to_enter = i and there exists k ≠ i, such that level ≥ ℓ
wait
To release the lock upon exiting the critical section, process sets to −1.
That this algorithm achieves mutual exclusion can be proven as follows. Process exits the inner loop when there is either no process with a higher level than, so the next waiting room is free; or, when, so another process joined its waiting room. At level zero, then, even if all processes were to enter waiting room zero at the same time, no more than will proceed to the next room, the final one finding itself the last to enter the room. Similarly, at the next level, will proceed, etc., until at the final level, only one process is allowed to leave the waiting room and enter the critical section, giving mutual exclusion.
The algorithm can also be shown to be starvation-free, meaning that all processes that enter the loop eventually exit it. The proof proceeds by induction from downward. A process at is in the critical section, and by assumption will exit it. At all lower levels, it is impossible for a process to wait forever, since either another process will enter the waiting room, setting and "liberating" ; or this never happens, but then all processes that are also in the waiting rooms must be at higher levels and by the inductive hypothesis, they will eventually finish the loop and reset their levels, so that for all, and again exits the loop.
Starvation freedom is in fact the highest liveness guarantee that the algorithm gives; unlike the two-process Peterson algorithm, the filter algorithm does not guarantee bounded waiting.

Note

When working at the hardware level, Peterson's algorithm is typically not needed to achieve atomic access.
Some processors have special instructions, like test-and-set or compare-and-swap, that, by locking the memory bus, can be used to provide mutual exclusion in SMP systems.
Most modern CPU s reorder memory accesses to improve execution efficiency. Such processors invariably give some way to force ordering in a stream of memory accesses, typically through a memory barrier instruction. Implementation of Peterson's and related algorithms on processors which reorder memory accesses generally requires use of such operations to work correctly to keep sequential operations from happening in an incorrect order. Note that reordering of memory accesses can happen even on processors that don't reorder instructions.
Most such CPU s also have some sort of guaranteed atomic operation, such as XCHG on x86 processors and load-link/store-conditional on Alpha, MIPS, PowerPC, and other architectures. These instructions are intended to provide a way to build synchronization primitives more efficiently than can be done with pure shared memory approaches.

Footnotes