- Peterson's algorithm
Peterson's algorithm is a
concurrent programming algorithm formutual exclusion that allows two processes to share a single-use resource without conflict, using only shared memory for communication. It was formulated by Gary Peterson in1981 at theUniversity of Rochester . While Peterson's original formulation worked with only two processes, the algorithm can be generalised for more than two, as discussed in "Operating Systems Review, January 1990 ('Proof of a Mutual Exclusion Algorithm', M Hofri)"."The algorithm
flag [0] = 0 flag [1] = 0 turn = 0 P0: flag [0] = 1 P1: flag [1] = 1 turn = 1 turn = 0 while( flag [1] && turn = 1 ); while( flag [0] && turn = 0 ); // do nothing // do nothing // critical section // critical section ... ... // end of critical section // end of critical section flag [0] = 0 flag [1] = 0
The algorithm uses two variables, "flag" and "turn". A flag value of 1 indicates that the process wants to enter the
critical section . The variable turn holds the ID of the process whose turn it is. 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 of
mutual exclusion :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 [0] is 1 and either flag [1] is false or turn is 0. In both cases, P1 cannot be in its critical section.
Progress requirement
If process P0 does not want to enter its critical section, P1 can enter it without waiting. There is not strict alternating between P0 and P1.
Bounded waiting
A process will not wait longer than one turn for entrance to the critical section: After giving priority to the other process, this process will run to completion and set its flag to 0, thereby allowing the other process to enter the critical section.
C implementation example using two POSIX threads
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 orcompare-and-swap that, by locking the memory bus, can be used to provide mutual exclusion in SMP systems.Many modern CPUs reorder instruction execution and 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 an out-of-order processor generally require use of such operations to work correctly to keep sequential operations from happening in an incorrect order.Most such CPU's also have some sort of guaranteed
atomic operation , such as XCHG onx86 processors andLoad-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.External links
* [http://paul.luminos.nl/documents/show_document.php?d=339 Java implementation of Peterson's algorithm] , including documentation and source code
See also
*
Dekker's algorithm
*Lamport's bakery algorithm
Wikimedia Foundation. 2010.