Sub topic 6
Mutual Exclusion, Progress and Bounded Waiting
Assume you know process and thread states, context switching, preemption, and how to read C-like pseudocode. This chapter gives the rules used to judge whether a synchronisation method correctly protects a critical section.
A critical section is the part of a program that accesses shared data. A race condition occurs when the result depends on the order in which concurrent operations happen. For example, if two threads execute counter++, both may read the same old value before either writes the new value. One increment is then lost.
A correct solution to the critical-section problem must satisfy three requirements.
The three requirements
Mutual exclusion
Mutual exclusion means that at most one process is inside the critical section at a time. If process P0 is updating shared data, process P1 must remain outside until P0 leaves.
This prevents conflicting operations from overlapping. It does not require that a process enter quickly or that every waiting process eventually enter.
Progress
Progress applies when no process is inside the critical section and one or more processes want to enter. The system must choose one of those waiting processes without waiting forever for a process that is not interested in entering.
A solution fails progress if a process outside the critical section can prevent another process from entering indefinitely.
Bounded waiting
Bounded waiting means that a process will wait for only a limited number of other entries after it requests entry. The limit must not depend on luck or an endless stream of other processes.
Bounded waiting prevents starvation. A lock can provide mutual exclusion but still allow one unlucky process to wait forever.
| Requirement | Question to ask |
|---|---|
| Mutual exclusion | Can two processes enter together? |
| Progress | If the section is free, can an interested process be blocked forever? |
| Bounded waiting | Can one waiting process be overtaken forever? |
Testing a proposed solution
Analyze a solution in three stages:
- Look for an execution in which two processes enter together. That disproves mutual exclusion.
- Assume the critical section is empty. Check whether an uninterested process can stop an interested process. That disproves progress.
- Let one process request entry and allow other processes to run repeatedly. If they can enter forever before it, bounded waiting fails.
For example, a lock with a single shared variable busy is unsafe if a process checks busy == 0 and sets busy = 1 as two separate operations. Both processes can observe zero before either performs the assignment. The check and update must instead be one indivisible, or atomic, operation.
Peterson’s algorithm satisfies these three requirements for two processes in the textbook model with sequentially consistent memory. Hardware atomic instructions, mutexes, and semaphores provide other ways to build or enforce the same guarantees.
Exercise
A lock always allows one waiting process to enter eventually, but another process can repeatedly enter first without limit. Which requirement fails?
Show answer
Bounded waiting fails. The first process does not starve because it eventually enters, but there is no fixed limit on how many entries can occur before it.