Race conditions and the critical-section problem
Race conditions and the critical-section problem
Recorded by Dr. Krithika L.B. Slot m3-race
Why counter++ is unsafe, what any solution to the critical-section problem must guarantee, and two classic software solutions — Peterson for two processes and the Bakery algorithm for n.
By the end of this session you should be able to:
Return to the producer–consumer buffer of Session 3.1, now with a shared count of items. The producer executes
count++ and the consumer count--. Each looks like one statement, but in machine code each is three
instructions — load, modify, store — and a context switch can happen between any two.
A situation where several processes access and manipulate the same data concurrently and the outcome depends on the particular order in which the accesses take place. To prevent it, only one process at a time may manipulate the shared data — the processes must be synchronized.
Each of n processes has a segment of code — its critical section — in which it accesses shared data. The problem is to design a protocol so that no two processes are in their critical sections at the same time.
do {
entry section /* ask permission */
critical section /* touch shared data */
exit section /* announce you left */
remainder section /* everything else */
} while (true);If a process is in its critical section, no other process can be in its critical section.
If no process is in its CS and some want to enter, only those not in their remainder sections take part in the decision, and it cannot be postponed forever.
There is a limit on how many times others may enter their CS after a process has asked to enter and before it is allowed in (no starvation).
Assume each process runs at non-zero speed, but make no assumption about relative speeds or the number of CPUs.
turnwhile (turn != i) ; /* CS */ turn = j;
Mutual exclusion ✔. Progress ✘: if P1 is in its remainder section and P0 wants to enter twice in a row, P0 waits for P1 to take its turn.
flag onlyflag[i] = true; while (flag[j]) ; /* CS */ flag[i] = false;
Mutual exclusion ✔. Progress ✘: if both set their flags at the same time, both wait forever (a livelock/deadlock).
Peterson (1981) combines both ideas: flag[i] says “I want to enter”, and turn says “whose turn it is if we
both want to”.
int turn;
bool flag[2] = {false, false};
do {
flag[i] = true; /* I want to enter */
turn = j; /* but you may go first */
while (flag[j] && turn == j)
; /* busy wait */
/* critical section */
flag[i] = false; /* I am out */
/* remainder section */
} while (true);turn equalled its own index — impossible, turn has one value.turn = i, so Pi enters after at most one entry by Pj.Modern CPUs and compilers may reorder independent reads and writes, which can break Peterson's solution. It is taught because it shows the reasoning clearly; real systems use hardware instructions (Session 3.3) or memory barriers.
Lamport's Bakery algorithm generalises to n processes, inspired by a bakery where each customer takes a
numbered token and the smallest number is served next. Two processes may draw the same number, so ties are broken by process ID:
compare pairs (number, id) lexicographically.
bool choosing[n]; /* all false */
int number[n]; /* all 0 */
do {
choosing[i] = true;
number[i] = 1 + max(number[0], ..., number[n-1]); /* take a token */
choosing[i] = false;
for (j = 0; j < n; j++) {
while (choosing[j])
; /* wait while j picks */
while (number[j] != 0 &&
(number[j] < number[i] ||
(number[j] == number[i] && j < i)))
; /* j goes first */
}
/* critical section */
number[i] = 0; /* return the token */
/* remainder section */
} while (true);P1, P2 and P3 want to enter. P1 takes 1; P2 and P3 compute max + 1 at the same moment and both take 2.
Pairs are compared as (number, id): (1,1) < (2,2) < (2,3). So the order is P1, then P2, then P3.
P1 finds no smaller pair and enters. P2 and P3 spin on P1's number.
P1 exits and sets number[1] = 0. P2 now has the smallest pair and enters; P3 waits because (2,2) < (2,3).
P2 exits; P3 enters. Every process is served in FCFS order of tokens → bounded waiting.
Why choosing? Without it, Pi might compare against Pj's number while Pj is still computing it (reading 0) and wrongly enter at the same time.
Race conditions and the critical-section problem
Recorded by Dr. Krithika L.B. Slot m3-race
Peterson's solution and the Bakery algorithm
Recorded by Dr. Krithika L.B. Slot m3-peterson-bakery
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0count++ not atomic?It compiles to load, add, store. A context switch between them lets another process read or write the stale value.
Progress: a process may be blocked from entering even though the other is not in or trying to enter its CS.
Only the last write survives; the process that wrote last waits and the other enters — mutual exclusion is preserved.
P2, because (4, 2) < (4, 5).
Yes — waiting processes spin in a loop, wasting CPU cycles.
Software solutions are subtle and can be broken by modern hardware reordering. Real systems rely on atomic hardware instructions and the locks built on them.
Continue to 3.3 — Hardware synchronization and mutex locks →