The critical section problem: ensure that when one process executes its critical section, no other process may simultaneously enter. Solutions must satisfy three requirements: mutual exclusion (safety), progress (no unnecessary deadlock), and bounded waiting (no starvation).
Analyze solutions (Peterson's, Dekker's) formally; trace through scenarios where each requirement is violated.
From your work with threads and race conditions, you have seen what goes wrong when two threads modify shared data simultaneously — corrupted counters, lost updates, inconsistent structures. The critical section problem takes that practical experience and distills it into a formal framework. Rather than just saying "we need a lock," it specifies exactly what properties a correct solution must have, giving you a precise yardstick for evaluating any synchronization mechanism.
The setup is straightforward. Each process (or thread) has a section of code — the critical section — that accesses shared resources. The rest of its code is the remainder section. Before entering the critical section, the process executes an entry section (requesting access). After leaving, it executes an exit section (releasing access). A correct solution must satisfy three properties. Mutual exclusion: at most one process is in the critical section at any time. Progress: if no process is in the critical section and some processes wish to enter, only those processes not in their remainder section participate in deciding who enters next — and the decision is made in finite time. Bounded waiting: there exists a bound on how many times other processes can enter the critical section after a process has requested entry and before that request is granted.
These three requirements are more subtle than they first appear. Mutual exclusion is the obvious one — it is the whole point of the exercise. Progress is trickier: it rules out solutions where processes that do not even want to enter the critical section can block those that do, and it rules out livelock where processes endlessly defer to each other without anyone entering. Bounded waiting prevents starvation — a scenario where one process is perpetually unlucky and never gets its turn, even though the system keeps making progress overall. A naive spin-lock satisfies mutual exclusion and progress but may not satisfy bounded waiting if the scheduler always favors the same thread.
Classical software solutions like Peterson's algorithm and Dekker's algorithm demonstrate how to achieve all three properties using only shared memory reads and writes, without special hardware instructions. Peterson's algorithm for two processes uses a turn variable and two flag variables: each process sets its flag to indicate interest, defers to the other process by setting turn, and then spins until either the other process is not interested or it is its turn. Tracing through the algorithm against the three requirements is the best way to internalize what each property means in practice. However, these software solutions have a critical limitation on modern hardware: CPUs and compilers reorder memory operations for performance. Without memory barriers or fence instructions, the carefully ordered reads and writes in Peterson's algorithm may execute in a different order than written, breaking mutual exclusion. This is why modern systems use hardware-supported atomic instructions (test-and-set, compare-and-swap) as the foundation for locks — they provide the ordering guarantees that software-only solutions cannot.