Sub topic 6
Mutual exclusion, progress and bounded waiting
This chapter assumes you know what a critical section is and how a race condition arises from interleaved access to shared data. It also assumes you can read C-like pseudocode. The previous chapter defined the critical-section problem. This chapter gives the three tests that any proposed solution must pass, and shows how to apply them.
The setting and its assumptions
Every process has the same structure:
while (1) {
/* entry section */
/* critical section */
/* exit section */
/* remainder section */
}A solution consists of the entry and exit sections. The standard analysis makes two assumptions:
- Each process runs at a nonzero speed, but you may not assume anything about the relative speeds of processes.
- A process may stay forever in its remainder section, or stop there. It does not stop inside its critical section or inside its entry or exit code.
These assumptions matter. A correct solution must work for every interleaving the scheduler could produce, including a very slow process or one that never wants the section again.
The three requirements
| Requirement | Meaning | What a violation looks like |
|---|---|---|
| Mutual exclusion | At most one process is in its critical section at any time. | Two processes inside at once, so a race condition. |
| Progress | If no process is in its critical section and some want to enter, only processes outside their remainder sections take part in choosing the next one, and the choice cannot be postponed forever. | Deadlock, or a process blocked by an idle process. |
| Bounded waiting | After a process requests entry, there is a bound on how many times others may enter before it does. | Starvation: one process loses every race. |
Mutual exclusion
This is the safety property. To check it, look for an interleaving that puts two processes in the critical section together. If you can build one, the solution fails. If you can argue that no such interleaving exists, it holds.
Progress
This is the liveness property for the case where the section is free. It has two parts:
- A process in its remainder section must not influence who enters next. This rules out making a process wait for one that is not interested.
- The decision must terminate. Two processes that both want to enter must not block each other forever.
Bounded waiting
This is a fairness property for a process that is already waiting. The bound is a number, such as n−1 for n processes. It does not have to be small. It has to be finite and independent of how fast the processes run.
Progress and bounded waiting are often confused. Progress is about whether the section gets handed out at all. Bounded waiting is about whether a particular process eventually gets it.
Applying the tests
Strict alternation
int turn = 0;
void enter_region(int i) {
while (turn != i)
;
}
void leave_region(int i) {
turn = 1 - i;
}- Mutual exclusion holds. Only the process whose number equals
turncan pass, andturnhas one value. - Bounded waiting holds. After a process leaves, the other one goes next, so the bound is 1.
- Progress fails. Suppose P0 leaves and sets
turn = 1, and P1 stays in its remainder section for a long time. P0 now wants to enter again and spins, although nobody is in the critical section. A process in its remainder section is blocking another.
Flag only
int flag[2] = {0, 0};
void enter_region(int i) {
flag[i] = 1;
while (flag[1 - i])
;
}
void leave_region(int i) {
flag[i] = 0;
}Mutual exclusion holds. Suppose P0 passes its test at time t. Then P0 had set flag[0] before t, and it saw flag[1] == 0. P1 can only have set flag[1] after t, so P1 sees flag[0] == 1 and waits.
Progress fails. If both processes execute flag[i] = 1 before either tests, both spin forever. Nobody is in the critical section, both want to enter, and the decision is never made.
If you swap the two lines (test first, then set), the deadlock goes away but mutual exclusion fails. Both processes can see the other's flag as 0 and both enter.
Peterson's algorithm
Peterson's algorithm combines the two ideas: a flag array to announce interest and a turn variable to break ties.
int flag[2] = {0, 0};
int turn = 0;
void enter_region(int i) {
int j = 1 - i;
flag[i] = 1;
turn = j;
while (flag[j] && turn == j)
;
}
void leave_region(int i) {
flag[i] = 0;
}- Mutual exclusion: if both were inside, both would have seen
flag[j] == 0orturn == i. Becauseturnholds a single value and each process setsflag[i]before writingturn, both cannot be true together. - Progress: if P1 is in its remainder section,
flag[1] == 0and P0 passes immediately. If both are in the entry section,turnsettles on one value and exactly one of them passes. - Bounded waiting: after P0 leaves and wants back in, it sets
turn = 1. If P1 is waiting, P1 passes first. So P1 waits for at most one entry by P0.
Test-and-set spinlock
#include <stdatomic.h>
atomic_flag lock = ATOMIC_FLAG_INIT;
void acquire(void) {
while (atomic_flag_test_and_set(&lock))
;
}
void release(void) {
atomic_flag_clear(&lock);
}- Mutual exclusion holds, because the test and set happen in one indivisible step.
- Progress holds. When the lock is free, some spinner's test-and-set succeeds, and a process in its remainder section takes no part.
- Bounded waiting fails. Nothing orders the waiters. One unlucky process can lose every race against faster processes that keep releasing and reacquiring.
Test-and-set with bounded waiting
This version adds a waiting array. A leaving process hands the lock to the next waiting process in cyclic order, so a waiter is passed over at most n−1 times.
int waiting[N]; /* all false initially */
int lock = 0; /* shared, accessed with test_and_set */
void enter_region(int i) {
waiting[i] = 1;
int key = 1;
while (waiting[i] && key)
key = test_and_set(&lock);
waiting[i] = 0;
}
void leave_region(int i) {
int j = (i + 1) % N;
while (j != i && !waiting[j])
j = (j + 1) % N;
if (j == i)
lock = 0;
else
waiting[j] = 0; /* hand over directly, lock stays 1 */
}Here test_and_set(&lock) is the atomic instruction that returns the old value and sets the lock to 1.
Summary of the solutions
| Solution | Mutual exclusion | Progress | Bounded waiting |
|---|---|---|---|
| Strict alternation | Yes | No | Yes |
| Flag only (set, then test) | Yes | No (deadlock) | Not applicable, since no entry is guaranteed |
| Flag only (test, then set) | No | Not relevant | Not relevant |
| Peterson | Yes | Yes | Yes |
| Test-and-set spinlock | Yes | Yes | No |
| Test-and-set with `waiting[]` | Yes | Yes | Yes (bound n−1) |
A checking procedure
For each question, try to construct a concrete interleaving as a counterexample. Write the statements of each process in order and choose where the scheduler switches. If the counterexample is a deadlock, the failed requirement is progress. If it is a starvation scenario with a bounded number of processes, the failed requirement is bounded waiting.
Common errors
- Calling a deadlock a mutual-exclusion failure. In the flag-only solution nobody enters, so mutual exclusion is intact and progress is what fails.
- Treating strict alternation as fully correct because it is "fair". It is fair, but it fails progress.
- Treating a spinlock as fully correct because it guarantees mutual exclusion. It fails bounded waiting.
- Assuming a process might never leave its critical section. The analysis assumes it does leave.
- Assuming Peterson's algorithm works as written on modern CPUs. Compilers and processors reorder memory accesses, so real hardware needs memory barriers or atomic operations.
Exercise
Consider two processes, each running: while (1) { while (turn != i); CS; turn = 1 - i; remainder; }, with turn initially 0. P1 never leaves its remainder section after its first pass through it. Which of the three requirements fail, and why?
Show answer
Progress fails. After P0 enters once and sets turn = 1, P0 can never enter again, because only P1 can set turn back to 0 and P1 is stuck in its remainder section. The critical section is free and P0 wants it, yet a process outside its critical section blocks it. Mutual exclusion holds, because only the process matching turn can pass. Bounded waiting holds in the sense that no process is overtaken more than once, although P0 does wait indefinitely because of the progress failure.
Exercise
A test-and-set spinlock is used by three processes. Which requirement can fail, and what change guarantees a bound?
Show answer
Bounded waiting can fail, because the order in which spinners win the lock is not controlled and one process can lose every race. Adding a waiting[] array and having the leaving process hand the lock to the next waiting process in cyclic order makes the bound n−1, which is 2 for three processes.