Sub topic 7
Peterson's Algorithm for Two Processes
Assume you already understand processes, preemption, shared variables, and basic C-like pseudocode. This chapter uses a textbook model in which reads and writes are atomic and memory is sequentially consistent: operations appear to occur in one global order.
Peterson's algorithm is a software solution to the critical-section problem for exactly two processes. A critical section is code that accesses shared data and must not be executed by both processes at the same time.
The shared variables
The algorithm uses two shared variables:
flag[i]is true when processiwants to enter its critical section.turnidentifies which process should wait if both processes want to enter.
Each process has an identifier, either 0 or 1. When process i wants to enter, it announces its interest and gives the other process priority.
int flag[2] = {0, 0};
int turn = 0;
void enter_region(int i) {
int j = 1 - i;
flag[i] = 1; // I want to enter
turn = j; // Let the other process go first
while (flag[j] && turn == j)
; // Busy wait
}
void leave_region(int i) {
flag[i] = 0; // I no longer need the critical section
}A process uses the algorithm like this:
enter_region(0);
/* critical section */
leave_region(0);The same pattern applies to process 1.
How entry works
Suppose process 0 wants to enter:
- It sets
flag[0]to1. - It sets
turnto1. - It waits only while process
1also wants to enter andturnis still1.
If process 1 does not want to enter, flag[1] is zero, so process 0 proceeds immediately.
If both processes request entry at nearly the same time, both flags become true. The last process to write turn gives priority to the other process. For example, if process 1 writes turn = 0 last, process 0 proceeds and process 1 waits.
Why it satisfies the requirements
Peterson's algorithm provides the three standard requirements under its textbook assumptions:
- Mutual exclusion: Both processes cannot pass the
whilecondition at the same time. If both flags are true,turncan name only one process, so the other process waits. - Progress: If the critical section is empty and one process wants to enter, that process does not wait for a process that is not interested.
- Bounded waiting: After a process announces its interest, the other process can enter at most once before it gets its turn.
The loop is a busy wait, also called spinning. The waiting process repeatedly tests shared memory and consumes CPU time.
> warning > Peterson's algorithm is mainly a teaching tool. On modern multicore systems, compiler reordering, CPU memory reordering, and caches can violate the simple sequential-consistency assumption. A practical implementation needs suitable atomic operations and memory-ordering rules.
Common tracing mistake
Do not reason from source-code order alone. Consider possible interleavings between individual reads and writes. When both processes set their flags, inspect the final write to turn. That write determines which process waits.
Exercise
Both processes have set their flags to true. Process 0 is waiting for turn == 0, and process 1 is waiting for turn == 1. Which process can enter if the current value of turn is 0?
Show answer
Process 1 can enter. Process 0's waiting condition is true, while process 1's condition is false because turn is not 1.