The Critical Section Problem
In concurrent programming, whenever multiple threads share mutable state without synchronization, race conditions arise.
Any valid solution to the Critical Section problem must satisfy three fundamental criteria:
-
Mutual Exclusion: If process
P_iis executing in its critical section, no other processes can be executing in their critical sections. - Progress: If no process is executing in its critical section and some processes wish to enter, only those processes not executing in their remainder sections can participate in deciding who enters next.
- Bounded Waiting: There must be a bound on the number of times other processes are allowed to enter their critical sections after a process has made a request to enter and before that request is granted.
Peterson's Algorithm (1981)
Gary Peterson published an ingenious, classic software-based solution for two processes (P₀ and P₁) that requires zero specialized hardware atomic instructions.
It relies on two shared variables:
bool flag[2] = {false, false}; // Expresses intent to enter
int turn = 0; // Expresses polite priority
The Process Structure for Thread i (with peer j = 1 - i):
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j) {
// Busy wait (spin)
}
// --- CRITICAL SECTION ---
flag[i] = false;
// --- REMAINDER SECTION ---
} while (true);
The Mathematical Proof of Mutual Exclusion
To prove mutual exclusion holds, we use proof by contradiction:
- Suppose both
P₀andP₁are simultaneously executing in their critical sections. - For
P₀to exit thewhileloop, eitherflag[1] == falseorturn == 0. - For
P₁to exit thewhileloop, eitherflag[0] == falseorturn == 1. - Since both are in their critical sections, both
flag[0] == trueandflag[1] == true. - Therefore, we must have
turn == 0ANDturn == 1simultaneously. -
Contradiction:
turnis a scalar memory location in shared RAM; it can only hold one integer value at a time. The last write toturnoverrides the previous write.
Thus, mutual exclusion is mathematically guaranteed.
Why Peterson's Algorithm Breaks on Modern CPUs
Peterson developed his proof under the assumption of Sequential Consistency—that memory reads and writes occur in the exact chronological order specified by the source code.
Modern hardware, however, relies heavily on:
- Compiler Instruction Reordering: The optimizer may reorder independent writes.
-
Out-of-Order CPU Execution: Store buffers on x86 and weak memory models on ARM can make the store to
turnvisible to other cores before the store toflag[i].
If turn = j is reordered before flag[i] = true:
-
P₀setsturn = 1. -
P₁setsturn = 0. -
P₁setsflag[1] = true. -
P₁checks:flag[0]is stillfalseimplies P₁enters! -
P₀setsflag[0] = true. -
P₀checks:turn == 0isfalse(it's 0, butP₀checksturn == 1)implies P₀enters! - MUTUAL EXCLUSION FAILS!
To run Peterson's algorithm correctly on modern multicore hardware, explicit Memory Barriers (std::atomic_thread_fence(std::memory_order_seq_cst)) are strictly required.
Explore our full Operating Systems concurrency curriculum on Binary Dose.
Top comments (0)