Peterson's algorithm
Peterson's algorithm (or Peterson's solution) 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.[src] It was formulated by Gary L.[src] Peterson in 1981.[src] Peterson's original algorithm worked with only two processes; it can be generalized for more than two.[src]
Peterson's algorithm (or Peterson's solution) 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.[src] It was formulated by Gary L.[src] Peterson in 1981.[src] Peterson's original algorithm worked with only two processes; it can be generalized for more than two.[src] ==The algorithm== The algorithm uses two variables: flag and turn .[src] A flag[n] value of true indicates that the process n wants to enter the critical section.[src] 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 .[src] The algorithm satisfies the three essential criteria to solve the critical-section problem.[src] The while condition works even with preemption.[src] The three criteria are mutual exclusion, progress, and bounded waiting.[src] 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.[src] P0 and P1 can never be in the critical section at the same time.[src] If P0 is in its critical section, then flag[0] is true.[src] In addition, either flag[1] is false (meaning that P1 has left its critical section), or turn is 0 (meaning that P1 is just now trying to enter the critical section, but graciously waiting), or P1 is at label P1_gate (trying to enter its critical section, after setting flag[1] to true but before setting turn to 0 and busy waiting).[src] So if both processes are in their critical sections, then we conclude that the state must satisfy flag[0] and flag[1] and turn = 0 and turn = 1 .[src] No state can satisfy both turn = 0 and turn = 1 , so there can be no state where both processes are in their critical sections.[src] (This recounts an argument that is made rigorous in Schneider 1997.[src] ) === Filter algorithm: Peterson's algorithm for more than two processes === The filter algorithm generalizes Peterson's algorithm to processes.[src] Instead of a boolean flag, it requires an integer variable per process, stored in a single-writer/multiple-reader (SWMR) atomic register, and additional variables in similar registers.[src] 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.[src] Processes advance from one room to the next, finishing in room , which is the critical section.[src] Specifically, to acquire a lock, process executes i ← ProcessNo for ℓ from 0 to N − 1 exclusive level[i] ← ℓ last_to_enter[ℓ] ← i while last_to_enter[ℓ] = i and there exists k ≠ i, such that level[k] ≥ ℓ wait To release the lock upon exiting the critical section, process sets to −1.[src] That this algorithm achieves mutual exclusion can be proven as follows.[src] 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.[src] 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.[src] 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.[src] Unlike the two-process Peterson algorithm, the filter algorithm does not guarantee bounded waiting.[src] ==Modern problems== On modern machines, Peterson's algorithm (as shown here) fails to provide mutual exclusion.[src] Additional declarations and/or instructions can make it work (although different methods using the language features and/or new machine instructions can accomplish mutual exclusion more efficiently).[src] Computers have been made far more complex since 1981; they no longer execute instructions in lockstep.[src] Most compilers change the order of operations in ways that produce the same results faster, but they do not consider the interaction of concurrent operations that access the same data.[src] CPUs reorder instructions within their execution pipelines.[src] CPUs pre-read memory contents, cache memory contents, and delay and combine writes.[src] Multiple CPU cores can access the same memory with true concurrency.[src] Multiple CPUs can have separate caches that can delay synchronization.[src] Any of these can break Peterson's algorithm.[src] To allow usable mutual exclusion in this new environment, machine instructions have been added, such as memory barrier and atomic read-and-modify operations.[src] These instructions let things "catch up" when necessary.[src] Programming languages have added features that invoke these instructions.[src] In several languages, declaring a variable volatile produces executable code that accesses the variable more carefully.[src] Peterson's algorithm is typically not needed to ensure atomic access.[src] In earlier processors and operating systems, it sufficed to disable interrupts immediately before a critical section and then re-enable interrupts after it was complete.[src] Most modern processors have special instructions that provide ways to build synchronization primitives more efficiently than can be done with pure shared-memory approaches.[src] These instructions, by locking the memory bus, can be used to guarantee atomicity and provide mutual exclusion in symmetric multiprocessing systems.[src] Examples include test-and-set ( XCHG ) and compare-and-swap ( CMPXCHG ) on x86 processors and load-link/store-conditional on Alpha, MIPS, PowerPC, and other architectures.[src] Most modern CPUs reorder memory accesses to improve execution efficiency (see memory ordering for types of reordering allowed).[src] Such processors invariably provide some way to force ordering in a stream of memory accesses, typically through a memory barrier instruction.[src] Implementation of Peterson's and related algorithms on processors that reorder memory accesses generally requires use of such operations to work correctly to keep sequential operations from happening in an incorrect order.[src] Reordering of memory accesses can happen even on processors that don't reorder instructions (such as the PowerPC processor in the Xbox 360).[src] ==See also== Dekker's algorithm Eisenberg & McGuire algorithm Lamport's bakery algorithm Semaphore (programming) Szymański's algorithm ==Footnotes== ==External links== https://elixir.bootlin.com/linux/v5.6.19/source/arch/arm/mach-tegra/sleep-tegra20.S#L120 Example of Peterson's algorithm formerly being used in the linux kernel (removed in v5.7).[src]
54 sentences · 54 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Peterson's algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/peterson-s-algorithm. Accessed 25 November 2026.
Chicago
"Peterson's algorithm." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/peterson-s-algorithm.
APA
encyclopegia.org. (2026). Peterson's algorithm. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/peterson-s-algorithm
Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON