Chapter 6: Synchronization

Operating Systems
Race conditionsCritical-section requirementsPeterson and the memory model Memory barrierstest_and_set, CASAtomic variables Spinlocks and mutexesSemaphoresClassic problems Monitors and condition variablesPOSIX and Java APIsPriority inversion, lock-free
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Problem

Explain race conditions, the critical section, and the three requirements a solution must meet: mutual exclusion, progress, bounded waiting.

Software and the memory model

Evaluate the lock variable, strict alternation, and Peterson's solution; explain how reordering on modern CPUs breaks them and how memory barriers help.

Hardware support

Build locks and counters with test_and_set, compare_and_swap, XCHG, and atomic variables.

Tools

Compare spinlocks, mutexes, semaphores, monitors, and condition variables; apply the Mesa-style while rule.

Classic problems

Solve the bounded-buffer, readers–writers, and dining-philosophers problems; recognize the risks of starvation and deadlock.

Real systems

Use the POSIX and Java APIs; analyze priority inversion (Mars Pathfinder) and the basics of lock-free data structures.

Introduction

Why Synchronization?

  • Synchronization: coordinating how concurrent processes or threads access shared data so that it stays consistent.
  • A process can be interrupted at any point in its instruction stream, leaving an update half done.
  • On a multicore system no interrupt is needed: two cores can write the same memory truly simultaneously.
  • Symptoms:
    • inconsistent data, lost updates, a corrupted linked list;
    • a non-reentrant function's shared buffer getting overwritten;
    • threads waiting on each other forever (deadlock);
    • a thread that never gets its turn (starvation).
shared datacount = 5T1T2both: read → modify → writeinterrupt / context switchupdate left half done

Introduction

Race Condition: fork() and pid

P0P1pid = fork();pid = fork();next_available_pid2615Adapted from Silberschatz Figure 6.2

The kernel keeps the next free pid in next_available_pid: 2615. P0 and P1 call fork() at the same time.

P0P1pid = fork();pid = fork();next_available_pid2615reads: 2615interrupted before incrementAdapted from Silberschatz Figure 6.2

P0's kernel code reads the value (2615) but is interrupted before it can increment and store it.

P0P1pid = fork();pid = fork();next_available_pid2616reads: 2615reads: 2615child pid 2615Adapted from Silberschatz Figure 6.2

P1 also reads 2615, gives it to its child, and writes 2616.

P0P1pid = fork();pid = fork();next_available_pid2616reads: 2615reads: 2615child pid 2615child pid 2615same pid given to two processes!both wrote 2616; one increment lostAdapted from Silberschatz Figure 6.2

P0 resumes and gives its child 2615 as well. The outcome depends on the order of execution: a race condition.

The same race arises in user programs when count++ interleaves at the register level.

Introduction

Race Condition: Printer Spooler Directory

spooler directory4abc5prog.c6prog.n7—out = 4in = 7process Anext_free = ?process Bnext_free = ?

Files to print go into slots in order. out is the next file to print, in the first free slot.

spooler directory4abc5prog.c6prog.n7—out = 4in = 7process Anext_free = 7process Bnext_free = ?

A reads in (7) into its local next_free; right then its time slice expires.

spooler directory4abc5prog.c6prog.n7—out = 4in = 7process Anext_free = 7process Bnext_free = 7

B also reads in: 7 again!

spooler directory4abc5prog.c6prog.n7b.txtout = 4in = 8process Anext_free = 7process Bnext_free = 7

B writes its file name into slot 7 and sets in = 8.

spooler directory4abc5prog.c6prog.n7a.txtout = 4in = 8process Anext_free = 7process Bnext_free = 7

A resumes: since next_free = 7, it writes slot 7, overwriting B's entry; in = 8.

spooler directory4abc5prog.c6prog.n7a.txtout = 4in = 8process Anext_free = 7process Bnext_free = 7B's file is never printed!

The directory looks consistent, but B's output is gone. The result depends on who runs when: a race condition.

Race condition: two or more processes access shared data and the final result depends on the order of execution.

Introduction

Non-reproducible Execution

/* Process 1 */ /* Process 2 */
repeat          repeat
  n = n + 1;      print(n);
                  n = 0;

Possible orders with n = 5:

  • n=n+1; print; n=0 → prints 6
  • print; n=0; n=n+1 → prints 5, n=1
  • print; n=n+1; n=0 → prints 5, increment lost

Even one line of C (count++) is three machine instructions:

T₀producerreg₁ = countreg₁ = 5

Initially count = 5. For count++ the producer first loads the value into a register.

T₀producerreg₁ = countreg₁ = 5T₁producerreg₁ = reg₁ + 1reg₁ = 6

The producer increments its register to 6, then is interrupted before storing it.

T₀producerreg₁ = countreg₁ = 5T₁producerreg₁ = reg₁ + 1reg₁ = 6T₂consumerreg₂ = countreg₂ = 5

The consumer runs count--: memory still holds 5.

T₀producerreg₁ = countreg₁ = 5T₁producerreg₁ = reg₁ + 1reg₁ = 6T₂consumerreg₂ = countreg₂ = 5T₃consumerreg₂ = reg₂ − 1reg₂ = 4

The consumer decrements its register: 4.

T₀producerreg₁ = countreg₁ = 5T₁producerreg₁ = reg₁ + 1reg₁ = 6T₂consumerreg₂ = countreg₂ = 5T₃consumerreg₂ = reg₂ − 1reg₂ = 4T₄producercount = reg₁count = 6

The producer stores: count = 6.

T₀producerreg₁ = countreg₁ = 5T₁producerreg₁ = reg₁ + 1reg₁ = 6T₂consumerreg₂ = countreg₂ = 5T₃consumerreg₂ = reg₂ − 1reg₂ = 4T₄producercount = reg₁count = 6T₅consumercount = reg₂count = 4

The consumer overwrites it: count = 4. The right answer was 5; another order could give 6.

Critical Section

The Critical-Section Problem

  • n processes {P0, P1, …, Pn-1}; each has a critical section in which it accesses shared data: writing a global, updating a table, writing a file…
  • Rule: while one process is in its critical section, no other process may be in its critical section.
  • Each process requests entry in its entry section and announces its exit in its exit section.
  • Race condition: the result depends on execution order. Mutual exclusion: only one access to shared data at a time.
while (true) {
    /* entry section             */
        /* critical section      */
    /* exit section              */
        /* remainder section       */
}
entry sectioncritical sectionexit sectionremainder sectionlooprequest → use → release

Critical Section

Three Requirements for a Solution

1. Mutual exclusionWhile Pi is in its critical section, no other process may be in its own.
2. ProgressIf no process is in its critical section, only those not in their remainder sections decide who enters next, and the choice cannot be postponed indefinitely.
3. Bounded waitingOnce a process asks to enter, others may enter only a bounded number of times before it does (no starvation).
Tanenbaum's four conditionsEquivalent
No two processes may be in their critical sections at once.Mutual exclusion
No assumptions about CPU speed or count.Nonzero speed; relative speeds unknown
No process outside its critical section may block others.Progress
No process should wait forever.Bounded waiting

Silberschatz 10e §6.2; Tanenbaum MOS §2.3.2. Exclusion is a safety property; the other two are liveness properties.

Critical Section

Critical-Section Timeline

T1T2T3T4ABA in critical sectiontime →

T1: A enters its critical section.

T1T2T3T4ABA in critical sectionB blockedtime →

T2: B tries to enter; the section is taken, so B blocks (or busy-waits).

T1T2T3T4ABA in critical sectionB blockedB in critical sectiontime →

T3: A leaves; the waiting B enters at once. At no moment are two processes inside.

T1T2T3T4ABA in critical sectionB blockedB in critical sectiontime →

T4: B leaves. A correct solution must guarantee this under any schedule.

Adapted from Tanenbaum MOS Figure 2-22.

Software Approaches

Disabling Interrupts

  • A process disables interrupts before entering its critical section and re-enables them on exit: with no timer interrupt, no context switch can occur.
  • Problems:
    • A process that forgets to re-enable interrupts (or loops forever) hangs the system.
    • I/O and timer interrupts wait meanwhile; a long critical section makes the system unresponsive.
    • Useless on multicore: the other cores keep running.
    • cli/sti are privileged instructions; user programs cannot execute them.
  • Inside the kernel, brief local use is common: Linux local_irq_save(), and spin_lock_irqsave() for data shared with an interrupt handler.
Core 0Core 1cli · interrupts offinterrupts on, runningshared data(e.g., ready queue)disabling interrupts stops only the local core

Software Approaches · Step through the code

Lock Variable

int lock = 0;           /* shared */

void enter(void) {
  while (lock != 0)
    ;                   /* busy wait */
  lock = 1;
}
void leave(void) {
  lock = 0;
}

/* each process: enter(); critical(); leave(); */
P0outside critical sectionP1outside critical section

Shared lock is 0: the section is free.

P0lock != 0 ? → noP1ready

P0 reads lock: 0. It exits the loop…

P0interrupted (before line 6)P1lock != 0 ? → no

…but is interrupted before writing lock = 1. P1 also reads 0.

P0readyP1lock = 1 → entered

P1 sets the lock to 1 and enters its critical section.

P0lock = 1 → enteredP1in critical section

P0 resumes; it already tested the lock, so it also writes 1 and enters.

P0in critical sectionP1in critical sectiontwo processes in the critical section!

Mutual exclusion is broken: test and set are separate steps. The fix is a single atomic instruction for both (TSL).

The lock itself is a shared variable, and protecting it would need yet another lock. A software lock alone is not enough.

Software Approaches

Strict Alternation

/* P0 */
while (true) {
  while (turn != 0) ;  /* wait */
  critical_section();
  turn = 1;
  noncritical();
}
/* P1 */
while (true) {
  while (turn != 1) ;  /* wait */
  critical_section();
  turn = 0;
  noncritical();
}
P0CSP1waitturn = 0

turn = 0: P0 enters, P1 busy-waits (CS critical section, NCS noncritical section).

P0CSNCSwaitP1waitCSturn=1turn = 1

On exit P0 sets turn = 1; P1 enters. Mutual exclusion holds.

P0CSNCSwaitCSNCSP1waitCSnoncritical (long)turn=1turn=0turn=1turn = 1

P1 leaves (turn = 0) and starts a long noncritical stretch. P0 enters and leaves once more: turn = 1.

P0CSNCSwaitCSNCSwait (turn = 1)P1waitCSnoncritical (long)turn=1turn=0turn=1turn = 1section free, but P0 can't enter →

P0 wants in again, but it is P1's turn. The section is free, yet P1, outside its critical section, blocks P0: progress is violated.

Software Approaches

Peterson's Solution

/* shared */
bool flag[2] = { false, false };
int  turn;

/* P_i  (j = 1 - i) */
while (true) {
  flag[i] = true;    /* I want in      */
  turn = j;          /* after you      */
  while (flag[j] && turn == j)
    ;                /* busy wait      */
  /* critical section */
  flag[i] = false;
  /* remainder section */
}

G. L. Peterson, 1981. Works for two processes only; for n processes there is the bakery algorithm.

Mutual exclusionIf both were inside, flag[0] = flag[1] = true; but turn cannot be 0 and 1 at once. Whoever wrote turn last waits.
ProgressIf Pj is not interested, flag[j] = false: Pi enters without waiting.
Bounded waitingIf Pj leaves and wants back in, it sets turn = i; Pi waits for at most one entry.
The cost: busy waiting burns CPU. Worse, the proof assumes sequential consistency, which modern hardware does not provide →

Software Approaches · Step through the code

Peterson's Solution: Code Trace

bool flag[2] = { false, false };
int turn;
void enter(int i) {
  int j = 1 - i;
  flag[i] = true;   /* I want in */
  turn = j;         /* after you */
  while (flag[j] && turn == j)
    ;               /* busy wait */
}
void leave(int i) {
  flag[i] = false;
}
P0remainderP1remainderfalseflag[0]falseflag[1]?turncritical section

Both processes are in their remainder sections; both flags are false. Memory is assumed sequentially consistent (all operations fit one global order that preserves each thread's program order).

P0enteringP1remaindertrueflag[0]falseflag[1]?turncritical section

P0 wants in: flag[0] = true.

P0enteringP1enteringtrueflag[0]trueflag[1]?turncritical section

P1 asks at almost the same moment: flag[1] = true.

P0enteringP1enteringtrueflag[0]trueflag[1]1turncritical section

P0 is polite: turn = 1 ("after you").

P0enteringP1enteringtrueflag[0]trueflag[1]0turncritical section

P1 writes turn = 0 too. P1 wrote turn last, so P1 is the one that waits.

P0CSP1enteringtrueflag[0]trueflag[1]0turncritical sectionP0

P0: flag[1] && turn == 1 is false since turn is 0; it exits the loop and enters its critical section.

P0CSP1spinningtrueflag[0]trueflag[1]0turncritical sectionP0

P1: flag[0] && turn == 0 is true → busy-waits. Mutual exclusion holds.

P0remainderP1spinningfalseflag[0]trueflag[1]0turncritical section

P0 leaves: flag[0] = false.

P0remainderP1CSfalseflag[0]trueflag[1]0turncritical sectionP1

P1's condition is now false (flag[0] is false) → P1 enters. Progress holds.

P0enteringP1CStrueflag[0]trueflag[1]1turncritical sectionP1

P0 asks again: flag[0] = true, turn = 1.

P0spinningP1CStrueflag[0]trueflag[1]1turncritical sectionP1

P0: flag[1] && turn == 1 is true → waits. Now P0 wrote turn last.

P0spinningP1remaindertrueflag[0]falseflag[1]1turncritical section

P1 leaves: flag[1] = false.

P0CSP1remaindertrueflag[0]falseflag[1]1turncritical sectionP0

P0's condition turns false and it enters. Had P1 asked again, it would set turn = 0 and wait: P0 waits for at most one entry.

Memory Model · Step through the code

Why Peterson Fails on a Modern CPU

/* P_i  (i = 0 or 1, j = 1 - i) */
flag[i] = true;
turn = j;
while (flag[j] && turn == j)
  ;
/* critical section */
flag[i] = false;
Core 0 · P0store bufferCore 1 · P1store buffershared memoryflag[0]falseflag[1]falseturn0

Initially both flags in memory are false. Each core's stores go first to its own store buffer.

Core 0 · P0store bufferflag[0] = trueturn = 1Core 1 · P1store buffershared memoryflag[0]falseflag[1]falseturn0

P0 performs both stores; they sit in the buffer, not yet visible in memory.

Core 0 · P0store bufferflag[0] = trueturn = 1Core 1 · P1store bufferflag[1] = trueturn = 0shared memoryflag[0]falseflag[1]falseturn0

P1 does the same. Even x86 (TSO) may let a store complete after a later load.

Core 0 · P0store bufferflag[0] = trueturn = 1CS · enteredCore 1 · P1store bufferflag[1] = trueturn = 0shared memoryflag[0]falseflag[1]falseturn0

P0 reads flag[1] from memory: false → leaves the loop.

Core 0 · P0store bufferflag[0] = trueturn = 1CS · enteredCore 1 · P1store bufferflag[1] = trueturn = 0CS · enteredshared memoryflag[0]falseflag[1]falseturn0

P1 also reads flag[0] as false and enters: both are in the critical section!

Core 0 · P0store bufferCSCore 1 · P1store bufferCSshared memoryflag[0]trueflag[1]trueturn0

The buffers drain later, but the damage is done. Fix: a full memory barrier between the store and the load, or sequentially consistent atomics.

The compiler may also hoist that load. C11 _Atomic with the default memory_order_seq_cst, or Java volatile fields, restore the order.

Memory Model

Memory Models and Reordering

/* x = y = 0 */
/* T1 */        /* T2 */
x = 1;          y = 1;
r1 = y;         r2 = x;

/* r1 == 0 && r2 == 0 ?  */

Intuitively at least one should see 1. Yet on x86, ARM, and RISC-V, r1 = r2 = 0 can be observed (the store-buffer litmus test).

T1 (core)buffer: x = 1r1 = y → 0T2 (core)buffer: y = 1r2 = x → 0memory: x = 0, y = 0
ModelReordering allowed
Sequential consistency (SC)None; all operations fit one global order that preserves each thread's program order (Lamport 1979).
x86-64 (TSO)Only store → later load; stores stay ordered among themselves.
ARMv8, RISC-V (weak)Loads and stores may pass each other when there is no dependency.
CompilerMay move, cache, or remove any access if the single-threaded result is unchanged.
In C, volatile gives threads neither atomicity nor ordering. The right tools: C11 <stdatomic.h>, Java volatile/java.util.concurrent, locks.

Memory Model

Memory Barriers

Thread 1 (reader)Thread 2 (writer)while (!flag) ;print(x);x = 100;flag = true;order in which memory becomes visible to the other coreflag = trueT1 reads x → 0x = 100stores reordered: prints 0

No barrier: on a weak model T2's stores may become visible in another order; T1 sees flag true while x is still 0.

Thread 1 (reader)Thread 2 (writer)while (!flag) ;print(x);x = 100;flag = true;order in which memory becomes visible to the other coreT1 reads x → 0x = 100flag = trueloads reordered: x read before flag

Or T1 itself may read x before flag (load–load reordering).

Thread 1 (reader)Thread 2 (writer)while (!flag) ;memory_barrier();print(x);x = 100;memory_barrier();flag = true;order in which memory becomes visible to the other corex = 100flag = trueT1 reads x → 100barriers enforce order: prints 100

Memory barrier (fence): no later load or store starts until all earlier ones complete. T2: x first; T1: flag first.

Thread 1 (reader)Thread 2 (writer)while (!flag) ;memory_barrier();print(x);x = 100;memory_barrier();flag = true;order in which memory becomes visible to the other corex = 100store-release flagload-acquire flag → x = 100C11 release/acquire or Java volatile gives the same guarantee

In real code: x86 mfence, ARM dmb, Linux smp_mb(), C11 atomic_thread_fence; release/acquire atomics usually suffice.

The example from Silberschatz 10e §6.4.1. Lock and semaphore implementations already contain these barriers: code protected by a lock needs no extra fence.

Memory Model

Peterson in Java: A Correct Implementation

public class Peterson {
  static volatile boolean flag0, flag1;
  static volatile int turn;
  static int counter = 0;

  static void lock(int i) {
    if (i == 0) { flag0 = true; turn = 1;
      while (flag1 && turn == 1) Thread.onSpinWait(); }
    else        { flag1 = true; turn = 0;
      while (flag0 && turn == 0) Thread.onSpinWait(); }
  }
  static void unlock(int i) {
    if (i == 0) flag0 = false; else flag1 = false;
  }
  /* main: two threads, i = 0 and i = 1,
     each 100_000 times: lock(i); counter++; unlock(i);
     join both, print counter */
}
  • Three common mistakes:
    • volatile boolean[] flag makes only the array reference volatile, not its elements.
    • getId() % N can give two threads the same i.
    • A print inside the critical section distorts the measurement.
  • In Java, volatile reads and writes are sequentially consistent; with separate volatile fields Peterson works correctly.
  • Thread.onSpinWait() (Java 9+) hints to the CPU that it is spinning (x86 pause).
Output (JDK 21, three runs)
counter = 200000
counter = 200000
counter = 200000

Memory Model

Synchronization Barrier

barrierABCDeach process computes its own phase

A parallel computation split into phases (e.g., a grid simulation): each process computes one slice.

barrierABCDA, B, D blocked at barrier · C running

A process that finishes calls barrier_wait() and blocks; everyone waits for the slowest (C).

barrierABCDlast process arrived: all move to next phase

When the Nth process arrives the barrier opens; all move to the next phase together.

var b = new CyclicBarrier(3,
  () -> System.out.println(
      "phase " + phase[0]++ + " done"));
/* 3 threads, each:
   for (f = 0; f < 2; f++) {
     compute(f); b.await();
   } */
Output (JDK 21)
phase 0 done
phase 1 done
"Barrier" names two different ideas: a synchronization barrier brings a group of processes together (pthread_barrier_wait, CyclicBarrier); a memory barrier enforces the order of one core's memory accesses.

Hardware Support

Atomic Hardware Instructions

/* runs as ONE atomic instruction */
bool test_and_set(bool *target) {
  bool old = *target;
  *target = true;
  return old;
}

/* usage: a simple spinlock */
bool lock = false;
while (test_and_set(&lock))
  ;            /* busy wait */
/* critical section */
lock = false;
  • The trouble with software solutions: read and write are separate steps. Hardware provides instructions that test and modify a memory word atomically.
  • Atomicity holds on multicore too: the cache-coherence protocol gives one core exclusive ownership of the line for the whole instruction (older systems locked the memory bus).
ArchitectureAtomic read-modify-write
x86-64XCHG, LOCK CMPXCHG, LOCK XADD, LOCK BTS
ARMv8LDXR/STXR (LL/SC); v8.1 LSE: CAS, LDADD, SWP
RISC-V “A”LR/SC, AMOSWAP, AMOADD
Tanenbaum's hypothetical CPUTSL REGISTER,LOCK

Hardware Support · Step through the code

Critical Section with TSL

enter_region:
  TSL REGISTER,LOCK  | R = LOCK; LOCK = 1
  CMP REGISTER,#0    | was LOCK 0?
  JNE enter_region   | no: try again
  RET                | entered critical section

leave_region:
  MOVE LOCK,#0       | LOCK = 0
  RET
CPU 0REGISTER = –outside critical sectionCPU 1REGISTER = –outside critical sectionLOCK (memory)0

Lock is free: LOCK = 0.

CPU 0REGISTER = 0ran TSLCPU 1REGISTER = –readyLOCK (memory)1

CPU 0 runs TSL: old value (0) into the register, 1 into LOCK, in one indivisible step.

CPU 0REGISTER = 0ran TSLCPU 1REGISTER = 1ran TSLLOCK (memory)1

CPU 1 also runs TSL: its register gets 1; LOCK is already 1.

CPU 0REGISTER = 0CMP: 0 → RET, in CSCPU 1REGISTER = 1JNE → loop (spin)LOCK (memory)1

CPU 0 saw zero: it is in its critical section. CPU 1 saw nonzero: it busy-waits.

CPU 0REGISTER = 0MOVE LOCK,#0 → leftCPU 1REGISTER = 1still tryingLOCK (memory)0

On exit CPU 0 sets LOCK to 0 with an ordinary store.

CPU 0REGISTER = 0remainder sectionCPU 1REGISTER = 0TSL → saw 0, enteringLOCK (memory)1

CPU 1's next TSL reads 0 and acquires the lock. The two CPUs are never inside together.

Tanenbaum MOS Figure 2-25. TSL works on multicore too: unlike disabling interrupts, it also blocks other CPUs from that word.

Hardware Support

The XCHG Instruction

enter_region:
  MOVE REGISTER,#1    | put 1 in register
  XCHG REGISTER,LOCK  | swap register and LOCK
  CMP REGISTER,#0     | was LOCK 0?
  JNE enter_region    | no: loop
  RET                 | entered critical section
leave_region:
  MOVE LOCK,#0        | LOCK = 0
  RET
  • Swaps the contents of two operands in one step; the effect is the same as TSL.
  • The instruction Intel x86 uses for locking: XCHG with a memory operand is implicitly LOCK-prefixed, hence atomic.
  • C11 equivalent: atomic_exchange(&lock, 1).
REGISTERbefore: 1LOCKbefore: 0 or 1atomic swapafter: REGISTER = old LOCKLOCK = 1 (always)old LOCK 0 means lock acquired

Hardware Support · Step through the code

A Lock with compare_and_swap

int compare_and_swap(int *v, int exp, int nv) {
  int old = *v;           /* these three lines */
  if (old == exp)         /* are ONE atomic op */
    *v = nv;
  return old;
}

int lock = 0;
void enter(void) {
  while (compare_and_swap(&lock, 0, 1) != 0)
    ;                     /* busy wait */
}
void leave(void) { lock = 0; }
T0outsideT1outside

The lock is free.

T0CAS(0→1): old 0 → wonT1ready

T0's CAS finds lock at 0, writes 1, and returns 0: it leaves the loop and is in its critical section.

T0in critical sectionT1CAS: old 1 ≠ 0 → no write

T1's CAS finds 1: not the expected value, so it writes nothing and returns 1.

T0in critical sectionT1retrying in the loop

T1 busy-waits; each attempt returns without changing the memory word.

T0leave(): lock = 0T1in loop

T0 sets the lock to 0 on exit (in real code, a store with release ordering).

T0remainder sectionT1CAS(0→1) won → entered

T1's next CAS succeeds. Note: this lock does not guarantee bounded waiting; an unlucky thread may lose every time.

Silberschatz 10e §6.4.2. x86: LOCK CMPXCHG; C11: atomic_compare_exchange_strong; Java: AtomicInteger.compareAndSet.

Hardware Support · Step through the code

Atomic Increment with CAS

void increment(int *v) {
  int temp;
  do {
    temp = *v;              /* read              */
  } while (temp != compare_and_swap(v, temp, temp + 1));
}                           /* write if unchanged */

/* C11: if the counter is atomic_int, one line: atomic_fetch_add(v, 1); */
T1increment(v) calledT2increment(v) calledv = 5

Two threads want to increment the same counter; initially v = 5.

T1temp = 5T2readyv = 5

T1 reads the value: temp = 5. It may be interrupted here; there is no lock.

T1temp = 5T2temp = 5v = 5

T2 also reads 5. With a plain v++ one increment would be lost.

T1temp = 5T2CAS(5→6) succeedsv = 6

T2's CAS finds v still 5, writes 6, and returns 5: equal to temp → done.

T1CAS(5→6) fails: v = 6T2donev = 6

T1's CAS finds v at 6, not the expected 5; it writes nothing and returns 6 → retry.

T1temp = 6 (reread)T2donev = 6

T1 reads the current value.

T1CAS(6→7) succeedsT2donev = 7

This time it succeeds: v = 7. No increment was lost and no one held a lock (a lock-free update).

This "read → compute → CAS → retry on failure" loop is the basic pattern of lock-free data structures.

Hardware Support

Bounded Waiting with CAS

while (true) {
  waiting[i] = true;
  key = 1;
  while (waiting[i] && key == 1)
    key = compare_and_swap(&lock, 0, 1);
  waiting[i] = false;
  /* critical section */
  j = (i + 1) % n;
  while ((j != i) && !waiting[j])
    j = (j + 1) % n;
  if (j == i)
    lock = 0;            /* no one waiting */
  else
    waiting[j] = false;  /* hand over turn */
  /* remainder section */
}
P0–P1CSP2–P3waitingP4waitinglock = 1at most n − 1 = 4 turns of waiting

P1 is in its critical section; P3 and P4 wait with their waiting flags set.

P0–P1CSP2–P3waitingP4waitingj = 2: not waitingj = 3: waiting ✓at most n − 1 = 4 turns of waiting

On exit P1 scans j = 2, 3, … in order; the first waiter is P3.

P0–P1–P2–P3CSP4waitingwaiting[3] = falselock still 1at most n − 1 = 4 turns of waiting

It does not release the lock; it sets waiting[3] = false: P3's loop ends and it enters. The cyclic scan ensures bounded waiting.

Hardware Support · Step through the code

Bounded Waiting with CAS: Code Trace

/* P_i; n = 4 processes */
waiting[i] = true; key = 1;
while (waiting[i] && key == 1)
  key = compare_and_swap(&lock, 0, 1);
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
  j = (j + 1) % n;
if (j == i)
  lock = 0;            /* no one waiting */
else
  waiting[j] = false;  /* hand over turn */
P0FP1FP2FP3Flock = 0T/F: waiting[i]

The lock is free (lock = 0) and no one is waiting. The four processes will be scanned in ring order.

P0FP1FP2TP3Flock = 1T/F: waiting[i]

P2 arrives: waiting[2] = true; CAS finds lock at 0, writes 1, returns 0 → key = 0.

P0FP1FP2FP3Flock = 1T/F: waiting[i]

The loop ends; P2 sets waiting[2] = false and enters its critical section.

P0TP1FP2FP3Flock = 1T/F: waiting[i]

P0 arrives: CAS returns 1 (lock held) → key = 1, it spins.

P0TP1FP2FP3Tlock = 1T/F: waiting[i]

P3 arrives and spins too. Waiting: P0 and P3.

P0TP1FP2FP3Tlock = 1j = 3T/F: waiting[i]

On exit P2 starts scanning at the next index, j = 3; waiting[3] is true → the scan stops.

P0TP1FP2FP3Flock = 1j = 3T/F: waiting[i]

P2 does not release the lock; it sets waiting[3] = false: P3's loop ends and P3 enters. lock is still 1.

P0TP1FP2FP3Flock = 1j = 0T/F: waiting[i]

P3 leaves: j = (3 + 1) % 4 = 0; waiting[0] is true → the scan stops.

P0FP1FP2FP3Flock = 1j = 0T/F: waiting[i]

The turn passes to P0. P0 arrived before P3, but ring order chose P3 first; still, P0 waits for at most n − 1 = 3 entries.

P0FP1FP2FP3Flock = 1j = 0T/F: waiting[i]

P0 leaves: none of j = 1, 2, 3 is waiting; j comes back to 0 = i.

P0FP1FP2FP3Flock = 0T/F: waiting[i]

No one waiting → lock = 0. Only now is the lock truly free.

Locks

Atomic Variables

C11 <stdatomic.h>

#include <stdatomic.h>
atomic_int counter = 0;   /* _Atomic int */

void *work(void *arg) {
  for (int n = 0; n < 100000; n++)
    atomic_fetch_add(&counter, 1);
  return NULL;
}
/* 2 threads + join → 200000 */

On an _Atomic type, counter++ is atomic too (default seq_cst). It compiles to lock xadd on x86 and ldadd on ARMv8.1.

Java AtomicInteger

static AtomicInteger atom = new AtomicInteger();
/* two threads, each 100_000 times: */
atom.incrementAndGet();

var v = new AtomicInteger(5);
v.compareAndSet(5, 6);   /* true,  v = 6 */
v.compareAndSet(5, 7);   /* false, v = 6 */
Output (JDK 21, same on every run)
AtomicInteger = 200000
synchronized  = 200000
true 6
false 6
An atomic variable protects updates to a single variable. Keeping several variables consistent together (e.g., a buffer array plus a count) still needs a lock or a monitor.
counter++T1 read 5T2 read 5T1 write 6T2 write 6→ one increment lost (should be 7)fetch_addT1: 5 → 6 (one step)T2: 6 → 7 (one step)→ cache line held for the whole operation

Locks

Spinlocks and Mutexes

On core 1, T2 waits while T1 holds the lock:spinlockT2 spins: while(test_and_set)…T2 CST1 releases

Spinlock: the waiter keeps the CPU; when the lock frees it enters with no context switch.

On core 1, T2 waits while T1 holds the lock:spinlockT2 spins: while(test_and_set)…T2 CSmutexT2sleepT3 runs (useful work)wakeT2 CST1 releases2 context switches

Sleeping mutex: the waiter blocks and the CPU switches to T3; the cost is two context switches.

SpinlockMutex (sleeping)
Best forMulticore, CS shorter than two context switchesLong critical sections, or ones that may block
Single corePointless: the holder can't run, the waiter burns its sliceRight choice
ExampleLinux spin_lock, pthread_spin_lockpthread_mutex_t, Java synchronized

Most mutex implementations spin briefly, then sleep.

Locks

Implementing a Mutex: yield and futex

mutex_lock:
  TSL REGISTER,MUTEX | get old, set 1
  CMP REGISTER,#0    | was it free?
  JZE ok             | yes: acquired
  CALL thread_yield  | no: give up CPU
  JMP mutex_lock     | then try again
ok: RET
mutex_unlock:
  MOVE MUTEX,#0
  RET
  • Tanenbaum Figure 2-29: unlike enter_region, it calls thread_yield instead of looping, which avoids busy waiting with user-level threads.
  • Linux futex (fast userspace mutex): no system call when there is no contention; glibc pthread_mutex, Java locks, and Go are built on it.
user spacekernellock word1T1T2futex wait queueCAS 0 → 1: no system callempty

No contention: T1 flips the word 0 → 1 with one CAS; the kernel is never entered.

user spacekernellock word2T1T2futex wait queueCAS fails → value 2futex_waitT2

T2 finds the lock taken, sets the value to 2 ("there are waiters"), and sleeps in futex_wait.

user spacekernellock word2 → 0 → 2T1T2futex wait queueT1 releases: sees 2 → wakefutex_wakeT2 woke: xchg(2) → 0, got lock

On release T1 sees the old value 2, so it sets the word to 0 and calls futex_wake. T2 wakes and conservatively sets the word to 2 again; the old value was 0, so it holds the lock.

Locks · Step through the code

A Scalable Counter: Per-CPU Counting

typedef struct {
  int global;      pthread_mutex_t glock;
  int local[NCPU]; pthread_mutex_t llock[NCPU];
  int S;                     /* transfer threshold */
} acount_t;

void add(acount_t *c, int cpu) {
  pthread_mutex_lock(&c->llock[cpu]);
  c->local[cpu]++;
  if (c->local[cpu] >= c->S) {        /* transfer */
    pthread_mutex_lock(&c->glock);
    c->global += c->local[cpu];
    pthread_mutex_unlock(&c->glock);
    c->local[cpu] = 0;
  }
  pthread_mutex_unlock(&c->llock[cpu]);
}
int get(acount_t *c) {             /* approximate */
  pthread_mutex_lock(&c->glock);
  int v = c->global;
  pthread_mutex_unlock(&c->glock);
  return v;
}
tL1L2L3L4Gactual0000000

Four cores, threshold S = 4. Each core has its own counter and lock; the global counter G is updated only now and then.

tL1L2L3L4Gactual00000001101002

Cores 1 and 3 call add at the same time. Their locks differ, so they don't wait for each other.

tL1L2L3L4Gactual000000011010022212005

Three more cores increment. The global lock still hasn't been touched.

tL1L2L3L4Gactual0000000110100222120053313108

The actual value is 8, but G is still 0: the global counter lags behind.

tL1L2L3L4Gactual000000011010022212005331310844→0231410

L1 hits the threshold: core 1 takes the global lock, adds 4 to G, and resets its local counter.

tL1L2L3L4Gactual0000000110100222120053313108402314105024→02812

L3 also hits the threshold and transfers: G = 8.

tL1L2L3L4Gactual00000001101002221200533131084023141050202812get() = 8, actual = 12: the rest sits in locals

get() returns 8; the actual value is 12. In the worst case the gap can reach NCPU × (S − 1) = 4 × 3 = 12.

With a single-lock counter every increment contends for one lock; adding cores makes it slower. Large S: rare trips to the global lock and good scaling, but get() lags further. Linux's percpu_counter uses the same idea.

Locks

Locking Strategies for Concurrent Data Structures

StructureLockingResult
Linked listSingle mutexSimple. Call malloc outside the lock; don't forget unlock on every return path.
Hand-over-hand lockingA lock per node: grab the next, then release the previousTraversals proceed in parallel, but the per-node locking cost often eats the gain.
QueueSeparate head and tail locks, a dummy nodeEnqueue and dequeue can run concurrently.
Hash tableA locked list per bucketOperations on different buckets don't wait for each other: scales very well.
dummyheadheadLocktailtailLock

Initially there is a single dummy node; head and tail both point to it.

dummyABheadheadLocktailtailLock

enqueue(A), enqueue(B): only tailLock is taken; the tail end advances.

dummyA · dummyBheadheadLocktailtailLock

dequeue(): only headLock. The old dummy is freed, A's value is returned, and A becomes the new dummy.

Write the code correctly with one lock first. Split the lock only if measurement shows it is the bottleneck. More locks do not always mean faster.

Semaphores

Semaphore

  • Dijkstra (1965): an integer accessed only through two atomic operations.
    • wait(S) = P = down: if the value is 0 the process blocks (sleeps); otherwise it is decremented and the process continues.
    • signal(S) = V = up: increment the value; if anyone is waiting, wake one.
  • Think of the value as the number of available resources.
  • In the Silberschatz implementation the value can go negative: −k means k processes are waiting.
  • wait/signal are themselves short critical sections: protected by disabling interrupts on one core, or by a spinlock on multicore. Busy waiting does not vanish, but shrinks to a few instructions.
typedef struct {
  int value;
  struct process *list;
} semaphore;
A process that calls down on a zero value sleeps in Dijkstra's semaphore and in real systems; the busy-waiting version is only the first conceptual definition.
value = −2k = 2 waitingT2T3list: sleeping processes (FIFO)

Semaphores · Step through the code

Semaphore Waiting Queue

void wait(semaphore *S) {     /* P / down */
  S->value--;
  if (S->value < 0) {
    add(S->list, this_proc);
    sleep();                   /* block */
  }
}
void signal(semaphore *S) {   /* V / up */
  S->value++;
  if (S->value <= 0) {
    P = remove(S->list);       /* FIFO */
    wakeup(P);
  }
}
S.value1S.list (waiting)emptycritical section

One resource: S starts at 1 (a binary semaphore, like a mutex).

S.value0S.list (waiting)emptycritical sectionT1

T1 wait: the value becomes 0, not negative → T1 continues.

S.value−1S.list (waiting)T2critical sectionT1

T2 wait: the value is −1 → T2 joins the queue and sleeps.

S.value−2S.list (waiting)T2T3critical sectionT1

T3 sleeps too. −2: two processes are waiting.

S.value−1S.list (waiting)T3critical sectionT2

T1 signal: value −1, still ≤ 0 → T2 at the head of the queue is woken and enters.

S.value0S.list (waiting)emptycritical sectionT3

T2 signal: value 0 → T3 is woken.

S.value1S.list (waiting)emptycritical section

T3 signal: value 1, no one waiting. The queue is FIFO, so waiting is bounded.

Silberschatz 10e §6.6.2. The definition does not fix the queue order; without FIFO, starvation is possible.

Semaphores

Semaphore Usage Patterns

Binary (mutex-like)Initial value 1: wait … CS … signal.
CountingInitial value N: N identical resources (a connection pool, buffer slots).
Signaling / orderingInitial value 0: wait until an event happens (figure on the right).

Common mistakes

signal(m) … CS … wait(m)Reversed: several processes in the CS
wait(m) … CS … wait(m)Waits on itself: blocked forever
A forgotten wait or signalBroken exclusion, or deadlock
Two semaphores taken in different ordersDeadlock

Semaphores are powerful but unstructured: correctness depends on every wait/signal pair. That is why monitors were developed.

P1P2S1;signal(synch);wait(synch);S2;synch starts at 0

Semaphores · Step through the code

Barrier with Semaphores: Code Trace

#define N 3
semaphore mutex = 1, turnstile = 0;
int count = 0;
void barrier(void) {
  down(&mutex);
  count = count + 1;
  if (count == N) up(&turnstile); /* last */
  up(&mutex);
  down(&turnstile);  /* pass through */
  up(&turnstile);    /* let next one in */
}
AcomputingBcomputingCcomputingmutex = 1count = 0turnstile = 0waiting:(none)

Three threads compute a phase. The turnstile is closed (0); count counts arrivals.

AactiveBcomputingCcomputingmutex = 0count = 1turnstile = 0waiting:(none)

A is done: it takes the mutex, count = 1.

AsleepingBcomputingCcomputingmutex = 1count = 1turnstile = 0waiting:A

1 ≠ 3: A releases the mutex and sleeps in down(&turnstile).

AsleepingBsleepingCcomputingmutex = 1count = 2turnstile = 0waiting:AB

B arrives too: count = 2, sleeps at the turnstile.

AsleepingBsleepingCactivemutex = 0count = 3turnstile = 0waiting:AB

The slowest, C, arrives: count = 3 = N.

AactiveBsleepingCactivemutex = 0count = 3turnstile = 0waiting:B

C, the last to arrive, calls up(&turnstile): since someone is waiting the value stays put and A is woken.

AactiveBsleepingCsleepingmutex = 1count = 3turnstile = 0waiting:BC

C releases the mutex and sleeps at the turnstile: it can pass only if someone lets it through.

ApassedBactiveCsleepingmutex = 1count = 3turnstile = 0waiting:C

A is through; with up(&turnstile) it wakes B behind it.

ApassedBpassedCactivemutex = 1count = 3turnstile = 0waiting:(none)

B passes and wakes C: the turnstile works like a one-person revolving door.

ApassedBpassedCpassedmutex = 1count = 3turnstile = 1waiting:(none)

C passes; with no one left waiting, turnstile = 1. All have moved to the next phase; to reuse the barrier the turnstile must be closed again.

Tanenbaum semaphore: if someone is sleeping, up wakes one without incrementing. This barrier is single-use; a reusable one needs two turnstiles.

Classic Problems

The Producer–Consumer (Bounded-Buffer) Problem

  • Two processes share a fixed-size buffer of N slots: the producer writes, the consumer reads.
  • Bounded buffer: the producer waits when it is full, the consumer when it is empty. Unbounded buffer: only the consumer waits.
01234567out (consumer)in (producer)circular: (i + 1) % Ncount = 0empty: consumer must wait

The buffer is empty (count = 0): the consumer cannot read and must wait.

A0B1C234567out (consumer)in (producer)circular: (i + 1) % Ncount = 3

The producer adds A, B, C: each insert does in = (in + 1) % N.

0B1C234567out (consumer)in (producer)circular: (i + 1) % Ncount = 2

The consumer takes A: out = 1, count = 2.

I0B1C2D3E4F5G6H7out (consumer)in (producer)circular: (i + 1) % Ncount = 8full: producer must wait

The producer adds D…I; in wraps around (1). count = N = 8: the buffer is full and the producer must wait.

I012D3E4F5G6H7out (consumer)in (producer)circular: (i + 1) % Ncount = 6

The consumer takes B and C: out = 3, count = 6; the producer can write again.

Classic Problems · Step through the code

A Fatal Race: The Lost Wakeup

#define N 100
int count = 0;
void producer(void) {
    while (TRUE) {
        item = produce_item();
        if (count == N) sleep();
        insert_item(item);
        count = count + 1;
        if (count == 1) wakeup(consumer);
    }
}
void consumer(void) {
    while (TRUE) {
        if (count == 0) sleep();
        item = remove_item();
        count = count - 1;
        if (count == N - 1) wakeup(producer);
        consume_item(item);
    }
}
producerreadyconsumerabout to sleepcount = 0

The buffer is empty. The consumer reads count: 0. Just before calling sleep() it is interrupted.

producerrunningconsumerinterruptedcount = 0

The producer produces an item and puts it in the buffer.

producerrunningconsumerinterruptedcount = 1

count becomes 1.

producerwakeup → lostconsumerinterruptedcount = 1

count == 1, so the producer assumes the consumer is asleep and sends wakeup(consumer). But the consumer isn't asleep yet: the signal is lost.

producerreadyconsumersleepingcount = 1

The consumer resumes: acting on its stale decision, it calls sleep() and goes to sleep.

producerrunningconsumersleepingcount = 100

The producer fills the buffer; the consumer is asleep, so no one reads.

producersleepingconsumersleepingcount = 100

count == N → the producer sleeps too. Both sleep forever. The problem: testing count and calling sleep are not one indivisible (atomic) step.

Tanenbaum MOS Figure 2-27. sleep() blocks the caller (no busy waiting); wakeup(p) makes a sleeping p ready. A semaphore stores wakeups as a count, so no signal is lost.

Classic Problems

Bounded Buffer with Semaphores

#define N 100
semaphore mutex = 1;  /* buffer access      */
semaphore empty = N;  /* empty slots        */
semaphore full  = 0;  /* full slots         */

void producer(void) {
  while (true) {
    item = produce_item();
    down(&empty);   /* wait for a slot  */
    down(&mutex);   /* enter CS         */
    insert_item(item);
    up(&mutex);     /* leave CS         */
    up(&full);      /* one more full    */
  }
}
void consumer(void) {
  while (true) {
    down(&full);    /* wait for an item */
    down(&mutex);
    item = remove_item();
    up(&mutex);
    up(&empty);     /* one more empty   */
    consume_item(item);
  }
}
mutex = 1Mutual exclusion. (Starting at 0, no one could ever enter.)
empty = NThe producer blocks when the buffer is full.
full = 0The consumer blocks when the buffer is empty: an ordering (synchronization) role.

Classic Problems · Step through the code

Bounded Buffer, Step by Step

semaphore mutex = 1, empty = 2, full = 0;

void producer(void) {
  while (true) {
    item = produce();
    wait(&empty);
    wait(&mutex);
    insert(item);
    signal(&mutex);
    signal(&full);
  }
}
void consumer(void) {
  while (true) {
    wait(&full);
    wait(&mutex);
    item = remove();
    signal(&mutex);
    signal(&empty);
    consume(item);
  }
}
PstartingCstartingbuffer (N = 2)

The buffer is empty: 2 empty slots, 0 full.

PreadyCwait(full) → blockedbuffer (N = 2)

The consumer runs first: full = 0 → it sleeps. No busy waiting.

Pinserted A (CS)Cblockedbuffer (N = 2)A

Producer: empty 2→1, mutex 1→0, inserts A.

Psignal(full)Cwoke (ready)buffer (N = 2)A

signal(full) wakes the waiting consumer; the woken process uses up that "1", so full stays 0.

Pinserted BCreadybuffer (N = 2)AB

The consumer hasn't run yet; the producer inserts B too: empty = 0, full = 1.

Pwait(empty) → blockedCreadybuffer (N = 2)AB

No empty slot for C: the producer sleeps. The buffer does not overflow.

PwokeCtook Abuffer (N = 2)B

The consumer takes A; signal(empty) wakes the sleeping producer.

Pinserted CCconsume(A)buffer (N = 2)CB

The producer puts C in the freed slot. At all times: empty + full + operations in progress = N.

Classic Problems

If the downs Are Swapped

void producer(void) {
  while (true) {
    item = produce_item();
    down(&mutex);  /* ← first */
    down(&empty);  /* ← then  */
    insert_item(item);
    up(&mutex);
    up(&full);
  }
}
ProducerConsumermutexempty = 0buffer full

The producer's two down calls are swapped. The buffer is completely full: empty = 0.

ProducerConsumermutexempty = 0buffer fullholdswaits

The producer takes mutex, then sleeps in down(&empty), still holding the mutex.

ProducerConsumermutexempty = 0buffer fullholdswaitswaitsup(empty)would do

The consumer passes down(&full) but sleeps in down(&mutex). It was the one that would call up(&empty).

ProducerConsumermutexempty = 0buffer fullholdswaitswaitsup(empty)would docircular wait= deadlock

Each waits for the other: deadlock. Rule: wait on the counting semaphore first, then the mutex; never sleep while holding the mutex.

Classic Problems

The Readers–Writers Problem

semaphore mutex = 1;  /* guards rc          */
semaphore db = 1;     /* guards database    */
int rc = 0;           /* reader count       */

void writer(void) {
  while (true) {
    think_up_data();
    down(&db);  write_data_base();  up(&db);
  }
}
void reader(void) {
  while (true) {
    down(&mutex);
    if (++rc == 1) down(&db);  /* first reader */
    up(&mutex);
    read_data_base();
    down(&mutex);
    if (--rc == 0) up(&db);    /* last reader  */
    up(&mutex);
    use_data_read();
  }
}
databaseR1rc = 1 · db heldfirst reader locks db

Readers don't exclude each other; only the first reader takes db.

databaseR1rc = 1 · db heldWdown(db)writer waits

A writer arrives and waits in down(&db).

databaseR1R2rc = 2 · db heldWdown(db)new reader enters at once

A new reader, R2, enters without touching db: rc = 2.

databaseR2R3rc = 2 · db heldWdown(db)R1 left, R3 arrivedrc never reaches 0

R3 arrives as R1 leaves: rc drops to 1 and climbs back to 2; it never hits 0.

databaseR3R4rc = 2 · db heldWdown(db)R2 left, R4 arrived …writer starves

As long as readers keep coming the writer waits indefinitely: this solution favors readers and is prone to writer starvation.

Classic Problems

Readers–Writers: Variants and APIs

VariantBehavior
First readers–writers (reader priority)A writer enters only when there are no readers; writers may starve.
Second readers–writers (writer priority)No new reader may enter while a writer waits; readers may starve.
Fair (turnstile)Everyone passes through the same turnstile first; a waiting writer holds the turnstile.
pthread_rwlock_tThe glibc default favors readers; writer preference is configurable.
Java ReentrantReadWriteLocknew ReentrantReadWriteLock(true) gives fair (near-FIFO) ordering.
Linux seqlock, RCURead-mostly kernel structures in which readers take no lock at all.
/* Downey: starvation-free */
/* writer */
wait(turnstile);
wait(roomEmpty);
  write();
signal(turnstile);
signal(roomEmpty);

/* reader */
wait(turnstile); signal(turnstile);
/* first reader takes roomEmpty,
   last reader releases it (via rc) */
  read();

Classic Problems

Dining Philosophers: The Naive Solution

semaphore fork[5];         /* all 1 */

void philosopher(int i) {
  while (true) {
    think();
    wait(&fork[i]);              /* left */
    wait(&fork[(i + 1) % 5]);    /* right */
    eat();
    signal(&fork[(i + 1) % 5]);
    signal(&fork[i]);
  }
}
  • Dijkstra (1965): each philosopher thinks, and when hungry picks up the two neighboring forks and eats.
  • A model for sharing limited resources among processes without deadlock or starvation.
allthinkingF0F1F2F3F4

Forks are free; everyone is thinking.

left forksbeing takenF0F1F2F3F4

All get hungry at once and pick up their left fork…

all wait rightwaitsF0F1F2F3F4

…the fifth does too. Now everyone waits for the right fork.

DEADLOCKcircular waitF0F1F2F3F4

No one lets go: deadlock. The solution is wrong even though every fork is guarded by a semaphore.

Classic Problems

Philosophers: Ways Out

At most four at the table

A room semaphore initialized to 4: at least one philosopher gets both forks. Circular wait is broken.

Resource ordering (asymmetry)

Each philosopher picks up the lower-numbered fork first; F4 asks for fork 0 first. General rule: always acquire locks in one global order.

Pick up both forks together

Tanenbaum: a state[] array and a semaphore per philosopher; start eating only if neither neighbor is eating.

Monitor

Silberschatz: pickup(i)/putdown(i) and a condition variable self[i] per philosopher; the same test() logic.

Deadlock-free does not mean starvation-free: a philosopher whose two neighbors take turns eating may wait forever. Preventing starvation needs an extra rule (ticket numbers, aging).

Classic Problems · Step through the code

Tanenbaum's Solution, Step by Step

void take_forks(int i) {
  down(&mutex);
  state[i] = HUNGRY;
  test(i);
  up(&mutex);
  down(&s[i]);    /* block if no forks */
}
void put_forks(int i) {
  down(&mutex);
  state[i] = THINKING;
  test(LEFT);     /* (i+4) % 5 */
  test(RIGHT);    /* (i+1) % 5 */
  up(&mutex);
}
void test(int i) {
  if (state[i] == HUNGRY && state[LEFT] != EATING
                         && state[RIGHT] != EATING) {
    state[i] = EATING;
    up(&s[i]);
  }
}
all TF0F1F2F3F4

Initially everyone is THINKING (T). The s[i] semaphores are 0.

F1 eatingF0F1F2F3F4

F1 gets hungry; test(1): neighbors F0 and F2 aren't eating → EATING, up(&s[1]); then down(&s[1]) passes at once.

F2 blockedF0F1F2F3F4

F2 gets hungry; its left neighbor F1 is eating → the test fails and it sleeps in down(&s[2]).

F1 and F3 eatingF0F1F2F3F4

F3 gets hungry; F2 is hungry but not eating, F4 is thinking → F3 eats. Two philosophers can eat at once.

F2 still waitingF0F1F2F3F4

F1 puts down its forks and tests its neighbors: test(2) fails because F3 on the right is still eating.

F2 woke, eatingF0F1F2F3F4

When F3 puts its forks down, test(2) succeeds: F2 becomes EATING and up(&s[2]) wakes it.

Tanenbaum MOS Figure 2-47. There is no deadlock, and at most ⌊5/2⌋ = 2 of the five eat at once; but if F1 and F3 keep taking turns, F2 could in theory starve.

Monitors

Monitor

  • Hoare (1974) and Brinch Hansen (1975): a language-level abstract data type that bundles data, procedures, and initialization code.
  • Processes may call a monitor's procedures but cannot access its internal data directly.
  • Only one process may be active inside the monitor at a time; the compiler inserts the entry lock, so the programmer cannot forget it.
  • If the monitor is busy, the caller waits in the entry queue. To wait for a condition, processes use condition variables.
  • Getting the down order wrong with semaphores caused deadlock; monitors shrink this class of bugs.
monitor example
  integer i;
  condition c;
  procedure producer(); … end;
  procedure consumer(); … end;
end monitor;
monitorshared dataoperations (procedures)initialization codeinside (at most 1)Pentry queueQRxyconditionqueues

Monitors

Condition Variables

monitorshared dataoperations (procedures)initialization codeinside (at most 1)Pentry queueQxyconditionqueues

P is in the monitor but cannot continue because the buffer is empty. Q is in the entry queue.

monitorshared dataoperations (procedures)initialization codeinside (at most 1)Qentry queuexPyconditionqueues

P calls x.wait(): it releases the monitor and sleeps on x's queue, so Q can enter.

monitorshared dataoperations (procedures)initialization codeinside (at most 1)Qentry queuePxyconditionqueues

Q makes the condition true and calls x.signal(): one process on x's queue wakes. Who runs next is a semantic choice.

monitorshared dataoperations (procedures)initialization codeinside (at most 1)Qentry queuexyconditionqueues

When no one waits on x, x.signal() has no effect and leaves no trace. A semaphore's signal, in contrast, increments the value and is remembered.

OperationMeaning
x.wait()Suspend the caller and release the monitor.
x.signal()Resume one process waiting on x (if any).
broadcast / notifyAllWake every process waiting on x.
A condition variable has no value; it is just a waiting queue. The condition being waited for (e.g., count > 0) is always kept separately in shared data.

Monitors

Hoare and Mesa Semantics

Hoare: signal-and-waitQ (signaler)P (waiter)Q waits (urgent queue)Q in monitorP runs at once: condition trueQ continuessignal(x)

Signal-and-wait (Hoare): the awakened P runs immediately and Q waits. P can be sure the condition holds: an if suffices.

Mesa: signal-and-continueQ (signaler)P (waiter)Q continues in the monitorP ready, waiting for the monitorP enters: is the condition still true?signal(x)

Signal-and-continue (Mesa, 1980): Q continues; P merely becomes ready. Someone else may falsify the condition before P runs: it must recheck.

SemanticsWho runs next?Used by
Signal-and-wait (Hoare)The awakened processTextbook monitors
Signal-and-exit (Brinch Hansen)The awakened process; signal must be the procedure's last statementConcurrent Pascal
Signal-and-continue (Mesa)The signalerPthreads, Java, C#, Go sync.Cond, the Linux kernel

Monitors

Why while, Not if?

/* WRONG (under Mesa) */
lock(&m);
if (count == 0)
  cond_wait(&full, &m);
x = take();      /* may be empty */
unlock(&m);
/* RIGHT */
lock(&m);
while (count == 0)
  cond_wait(&full, &m);
x = take();      /* count > 0 for sure */
unlock(&m);
T1PT2count == 0 → waittime →

The buffer is empty: consumer T1 waits on the condition variable.

T1PT2count == 0 → waitput; count = 1; signalT1 only becomes readytime →

The producer puts an item and calls signal. T1 wakes, but must first reacquire the lock.

T1PT2count == 0 → waitput; count = 1; signalgot in first; take; count = 0time →

Meanwhile a newcomer, T2, grabs the lock first (barging) and takes the item: count = 0.

T1PT2count == 0 → waitput; count = 1; signalgot in first; take; count = 0if: take() → empty buffer!time →

T1 finally runs. With if it doesn't retest the condition and reads from an empty buffer: a bug.

T1PT2count == 0 → waitput; count = 1; signalgot in first; take; count = 0while: count == 0 → yine waittime →

With while, T1 retests the condition and waits again. POSIX also allows spurious wakeups: while is required in every case.

Monitors · Step by step

One Condition Variable Is Not Enough

/* WRONG: one condition c */
C: while (count == 0) wait(&c, &m);
   x = get(); signal(&c);
P: while (count == 1) wait(&c, &m);
   put(x);    signal(&c);
/* RIGHT: two conditions */
C: while (count == 0) wait(&full, &m);
   x = get(); signal(&empty);
P: while (count == 1) wait(&empty, &m);
   put(x);    signal(&full);
T1PT2time →empty → wait(c)empty → wait(c)

The buffer (size 1) is empty: both consumers sleep on the same condition variable c.

T1PT2time →empty → wait(c)empty → wait(c)put; signal(c)

The producer puts an item and wakes T1 with signal(c).

T1PT2time →empty → wait(c)empty → wait(c)put; signal(c)full → wait(c)

The producer retries; the buffer is full, so it also sleeps on c. Now T2 and P wait on c.

T1PT2time →empty → wait(c)empty → wait(c)put; signal(c)full → wait(c)get; signal(c)wakes T2!

T1 takes the item and calls signal(c). Which waiter wakes is unspecified: here, T2.

T1PT2time →empty → wait(c)empty → wait(c)put; signal(c)full → wait(c)get; signal(c)empty → wait(c)empty → wait(c)wakes T2!all three sleep on c

T2 finds the buffer empty and sleeps, as does T1 next round. No one can wake the producer: the program hangs.

Both versions hold lock m and use while. The bug: all threads wait on the same queue, yet a consumer should wake only the producer, and vice versa.

Monitors · Step by step

Whom to Wake? signal and broadcast

int avail = 0;          /* free bytes */

void mem_alloc(int n) {
  lock(&m);
  while (avail < n)
    wait(&c, &m);
  avail -= n;  /* ... allocate */
  unlock(&m);
}
void mem_free(int n) {
  lock(&m);
  avail += n;
  broadcast(&c); /* signal won't do */
  unlock(&m);
}
avail =0initiallyTawants 100 byteswaitsTbwants 10 byteswaitsBoth wait on c.c queue: Ta, Tb

Memory is exhausted: Ta wants 100 bytes, Tb wants 10; both sleep in their while loops.

avail =50free(50) + signalTawants 100 byteswoke: 50 < 100 → yine beklerTbwants 10 bytesnot woken, still waitingTb could proceed but is asleep.c queue: Ta, Tb

Tc frees 50 bytes. signal wakes only one; if that is Ta, it sleeps again and Tb's chance is wasted.

avail =40free(50) + broadcastTawants 100 byteswoke: 40 < 100 → yine beklerTbwants 10 byteswoke: 50 ≥ 10 → takes itTb proceeded; Ta woke for nothing.c queue: Ta

broadcast wakes everyone, and each retests its condition. The cost is needless wakeups.

Covering condition: when you can't tell which waiter can proceed, use pthread_cond_broadcast (Java: notifyAll). If waiters can be split by condition, signal is cheaper. Either way, wait inside a while.

Monitors

Producer–Consumer with a Monitor

monitor ProducerConsumer
  condition full, empty;
  integer count;

  procedure insert(item: integer);
  begin
    if count = N then wait(full);
    insert_item(item);
    count := count + 1;
    if count = 1 then signal(empty)
  end;

  function remove: integer;
  begin
    if count = 0 then wait(empty);
    remove := remove_item;
    count := count - 1;
    if count = N - 1 then signal(full)
  end;

  count := 0;
end monitor;
  • Tanenbaum Figure 2-34 (Pidgin Pascal). The monitor already guarantees mutual exclusion on count.
  • If the buffer is full, the producer blocks in wait(full) and releases the monitor; once a consumer enters and takes an item, signal(full) wakes the producer.
  • It is wait that blocks the producer; signal only wakes it.
  • An if suffices here because Tanenbaum assumes Brinch Hansen semantics (signal is the last statement). In Pthreads/Java the same code must use while.
  • No lost wakeup: the test and the wait happen inside the same monitor, without interruption.

Monitors · Step through the code

Producer–Consumer with a Monitor: Code Trace

monitor ProducerConsumer      { N = 2 }
  condition full, empty; integer count;
  procedure insert(item: integer);
  begin
    if count = N then wait(full);
    insert_item(item); count := count + 1;
    if count = 1 then signal(empty)
  end;
  function remove: integer;
  begin
    if count = 0 then wait(empty);
    remove := remove_item; count := count - 1;
    if count = N - 1 then signal(full)
  end;
monitorcount = 0inside:(empty)full:(empty)empty:(empty)

The buffer is empty (count = 0), N = 2. Producer P and consumer C enter the monitor in turn.

monitorcount = 0inside:(empty)full:(empty)empty:C

C calls remove: count = 0 → wait(empty). C sleeps and releases the monitor.

monitoracount = 1inside:Pfull:(empty)empty:C

P calls insert(a): the buffer isn't full, so it inserts the item; count = 1.

monitoracount = 1inside:Cfull:(empty)empty:(empty)

count = 1 → signal(empty): C wakes. Since signal is the last statement, P leaves at once; there are never two processes in the monitor.

monitorcount = 0inside:(empty)full:(empty)empty:(empty)

C resumes: takes a, count = 0. 0 ≠ N − 1, so no signal.

monitorbcount = 1inside:(empty)full:(empty)empty:(empty)

P calls insert(b): count = 1 → signal(empty), but no one is waiting: the signal is lost, harmlessly.

monitorbccount = 2inside:(empty)full:(empty)empty:(empty)

P calls insert(c): count = 2. The buffer is full.

monitorbccount = 2inside:(empty)full:Pempty:(empty)

P calls insert(d): count = N → wait(full). P sleeps and the monitor is free.

monitorccount = 1inside:Pfull:(empty)empty:(empty)

C calls remove: takes b, count = 1 = N − 1 → signal(full) wakes P.

monitorcdcount = 2inside:(empty)full:(empty)empty:(empty)

P returns from wait and inserts d: count = 2. No data was lost and no process slept forever.

POSIX and Java

Pthreads: Mutexes and Condition Variables

Mutex callDescriptionCondition callDescription
pthread_mutex_initCreates a mutexpthread_cond_initCreates a condition variable
pthread_mutex_destroyDestroys itpthread_cond_destroyDestroys it
pthread_mutex_lockAcquires the lock or blockspthread_cond_waitAtomically unlocks and sleeps; relocks on return
pthread_mutex_trylockAcquires or returns EBUSY at oncepthread_cond_signalWakes at least one waiter
pthread_mutex_unlockReleases it (owner only)pthread_cond_broadcastWakes all waiters
RuleCall cond_wait only while holding the mutex, inside a while loop.
Mutex typesNORMAL, ERRORCHECK, RECURSIVE; PTHREAD_PRIO_INHERIT for priority inheritance.
More APIspthread_rwlock_*, pthread_spin_*, pthread_barrier_*, C11 mtx_t/cnd_t.

Static init: PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER. cond_signal affects only current waiters; with none, the signal is lost.

POSIX and Java · Step through the code

Pthreads Producer–Consumer, Step by Step

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condc = PTHREAD_COND_INITIALIZER,
               condp = PTHREAD_COND_INITIALIZER;
int buffer = 0;                    /* 0 = empty */

void *producer(void *p) {
  for (int i = 1; i <= MAX; i++) {  /* MAX = 2 */
    pthread_mutex_lock(&m);
    while (buffer != 0) pthread_cond_wait(&condp, &m);
    buffer = i;
    pthread_cond_signal(&condc);
    pthread_mutex_unlock(&m);
  } return NULL;
}
void *consumer(void *p) {
  for (int i = 1; i <= MAX; i++) {
    pthread_mutex_lock(&m);
    while (buffer == 0) pthread_cond_wait(&condc, &m);
    printf("%d\n", buffer); buffer = 0;
    pthread_cond_signal(&condp);
    pthread_mutex_unlock(&m);
  } return NULL;
}
Pnot startedClock(m)mutex m: C

The consumer runs first and takes the mutex.

PreadyCcond_wait(condc) → sleepsmutex m: free

The buffer is empty: cond_wait atomically releases the mutex and sleeps. There is no lost-wakeup window.

Plock, buffer = 1Csleepingmutex m: P

The producer takes the mutex; the buffer is empty, so it skips the loop and writes 1.

Psignal(condc), unlockCwoke, waiting for mutexmutex m: free

signal wakes the consumer, but it cannot return until it reacquires the mutex (Mesa).

Pi = 2: cond_wait(condp)Cwaiting for mutexmutex m: P

The producer may win the race and retake the mutex: the buffer is full → it waits and releases the mutex.

PsleepingCpassed while, printed, buffer = 0mutex m: C

The consumer takes the mutex, retests the while condition (1 ≠ 0), and consumes the item.

Pwoke, buffer = 2Csignal(condp), unlockmutex m: P

The consumer signals condp; the producer wakes, finds the buffer empty, and writes 2.

PdoneCprinted 2mutex m: C

Second round: the consumer takes 2. The output is the same in any order.

Adapted from Tanenbaum & Bos, Figure 2-32.

POSIX and Java

POSIX Semaphores

#include <semaphore.h>

/* unnamed: threads of one process */
sem_t empty, full;
sem_init(&empty, 0, N);  /* 0: in-process */
sem_init(&full,  0, 0);
sem_wait(&empty); /* … put … */  sem_post(&full);
sem_destroy(&empty);

/* named: between unrelated processes */
sem_t *s = sem_open("/os06", O_CREAT, 0644, 1);
sem_wait(s);  /* CS */  sem_post(s);
sem_close(s);
sem_unlink("/os06");
sem_waitP / down: blocks if 0
sem_trywait, sem_timedwaitTry without waiting or with a timeout
sem_postV / up; safe in a signal handler
sem_getvalueCurrent value (informational only)
  • If the second argument of sem_init is 1, the semaphore must be in shared memory.
  • macOS lacks unnamed semaphores (sem_init is deprecated); use a named one or dispatch_semaphore.
  • Java: java.util.concurrent.Semaphore: acquire(), release(), optional fair mode.

POSIX and Java

Java: synchronized, wait, and notify

public class OneSlot {
  private int slot = 0;            /* 0 = empty */

  synchronized void put(int x) throws InterruptedException {
    while (slot != 0) wait();      /* releases lock */
    slot = x;
    notifyAll();
  }
  synchronized int take() throws InterruptedException {
    while (slot == 0) wait();
    int x = slot;
    slot = 0;
    notifyAll();
    return x;
  }
  /* main: a producer puts 10, 20, 30, 40;
     main calls take() four times and prints */
}
  • Every Java object is a monitor: an intrinsic lock plus a single wait set.
  • A synchronized method or block takes the lock and releases it on exit (even on an exception); the lock is reentrant.
  • wait() releases the lock and sleeps; notify() wakes one, notifyAll() wakes all. The semantics are Mesa: while is required.
  • Producer and consumer share one wait set, so notifyAll is safer.
Output (JDK 21, same on every run)
took 10
took 20
took 30
took 40

POSIX and Java

Java: ReentrantLock and Condition

class Buffer {
  private final int[] buf = new int[2];
  private int head = 0, tail = 0, count = 0;
  private final ReentrantLock lock = new ReentrantLock();
  private final Condition notFull  = lock.newCondition();
  private final Condition notEmpty = lock.newCondition();

  void put(int x) throws InterruptedException {
    lock.lock();
    try {
      while (count == buf.length) notFull.await();
      buf[tail] = x; tail = (tail + 1) % buf.length; count++;
      notEmpty.signal();
    } finally { lock.unlock(); }
  }
  /* take(): while (count == 0) notEmpty.await(); …
            notFull.signal();                      */
}
/* producer puts 1..5; main calls take() 5 times */
  • java.util.concurrent.locks: one lock can have several conditions; signal wakes the right group.
  • tryLock(), timed and interruptible locking, and an optional fair mode.
  • Always call unlock() in a finally block.
  • Java 21 virtual threads blocking inside synchronized "pinned" their carrier thread; JDK 24 (JEP 491) fixed this.
Output (JDK 21, three runs)
1 2 3 4 5 | sum = 15
1 2 3 4 5 | sum = 15
1 2 3 4 5 | sum = 15
lock (owner: T)notFull.await()P1P2notEmpty.await()T2

Liveness

Priority Inversion

H (high)M (medium)L (low)L: lock(R)02time (ms) →

t = 0: low-priority L acquires the lock on shared resource R.

H (high)M (medium)L (low)L: lock(R)HH: lock(R) → blockedL continues023time (ms) →

t = 2: high-priority H arrives and preempts L; at t = 3 it requests R and blocks. A short wait is expected: when L leaves, H enters.

H (high)M (medium)L (low)L: lock(R)HH: lock(R) → blocked (8 ms)LM runs (doesn't need R)LH02341011H's wait depends on M: unboundedtime (ms) →

But medium-priority M, which has nothing to do with R, preempts L and runs for a long time. H is indirectly waiting for M: priority inversion. If M runs from t = 4 to 10, L releases R only at t = 11: H waits 8 ms, and the longer M runs, the longer H waits, without bound.

H (high)M (medium)L (low)L: lock(R)Hwaits (2 ms)L (H's priority)HM runs02356L reverts to its old priority after releasing Rtime (ms) →

Priority inheritance: while H waits for R, L temporarily inherits H's priority, so M cannot cut in. L leaves and H runs at once: in the same example H waits only 2 ms.

Priority inversion is the problem, not a solution; the remedies are priority inheritance and the priority ceiling. On a single core with strict preemptive priority scheduling, an H spinning on a spinlock never yields the CPU; lower-priority L never runs and never releases the lock: the system hangs.

Liveness

Mars Pathfinder (1997)

  • July 1997: on the surface of Mars, Pathfinder, running the VxWorks real-time OS, kept resetting itself.
  • While a low-priority meteorology task held the bus mutex, the high-priority bus management task blocked; a medium-priority communications task got in between.
  • When the management task missed its deadline, a watchdog timer concluded the system had hung and forced a full reset.
  • JPL engineers reproduced the bug on a ground replica; the mutex's priority inheritance option was turned on by a remotely uploaded patch.
  • Today: PTHREAD_PRIO_INHERIT, Linux PI-futexes, and rt_mutex; PREEMPT_RT, merged into mainline in Linux 6.12, relies on this mechanism.
information bus(guarded by a mutex)bus managementhigh · blockedcommunications taskmedium · runs longmeteorology datalow · holds the mutexwatchdog→ reset the system

Liveness

Liveness Problems

deadlocklivelockstarvationT1T2both blocked, CPU idlewaiting on each othertrylock → back off → retryrunning but no progressT1 ✓T2 ✓T1 ✓T3 …T3 never gets a turn(readers–writers, priority)
DeadlockEvery process in a set waits for a resource held by another process in the set. It arises when the four necessary conditions hold at once.
LivelockProcesses are not blocked; they keep reacting to each other and repeat the same steps endlessly. Remedy: random backoff (e.g., Ethernet).
StarvationA process keeps yielding to others. Remedies: FIFO queues, fair locks, aging. The bounded-waiting requirement rules it out.

Lock-Free Programming

An Introduction to Lock-Free Structures

void push(node_t *n) {
  node_t *old = atomic_load(&top);
  do {
    n->next = old;
  } while (!atomic_compare_exchange_weak(
               &top, &old, n));
}  /* on failure old = current top */
BlockingIf the lock holder stalls, everyone stalls
Lock-freeThe system as a whole always makes progress
Wait-freeEvery thread finishes in a bounded number of steps
topAB

Stack: top → A → B. A single atomic pointer instead of a lock.

topABXX.next = Aold = AT1

T1 prepares to push X: old = A, X.next = A.

topYABXX.next = Aold = AT1T2: CAS(top, A, Y)succeeds

Meanwhile T2 pushes Y: CAS(top, A, Y) succeeds.

topYABXX.next = Yold = YT1T1: CAS(top, A, X)fails: top = Yold ← Y, retry

T1's CAS fails (top is now Y). C11 compare_exchange writes the current value into old; T1 retries with X.next = Y.

topXYABT1: CAS(top, Y, X) succeedsno lock; even if one thread stalls,the others keep making progress

The second CAS succeeds. The ABA problem: if top goes A→B→A during a pop, CAS cannot tell; you need a version counter or safe memory reclamation (hazard pointers, epochs).

Summary

Summary

ToolWhat it providesWhen to use
Peterson, strict alternationExclusion using only loads and stores (assuming SC)Conceptual; not used in real code
Memory barrierOrdering and visibility of loads and storesInside lock and atomic implementations
TAS, XCHG, CASAtomic read-modify-writeThe foundation of locks and lock-free structures
Atomic variableIndivisible update of one variableCounters, flags, reference counts
SpinlockExclusion without sleepingMulticore, very short CS, in the kernel
MutexExclusion; waiters sleepGeneral-purpose critical sections
SemaphoreCounting, exclusion, and orderingN identical resources, event notification
Monitor + condition variableStructured exclusion + condition waitingComplex conditions (Java, pthreads)
Reader–writer lockMany readers, one writerRead-mostly data

Every tool provides mutual exclusion; progress and bounded waiting come from correct use and fair queues. Priority inversion and deadlock need separate thought.

Exercises

Exercises

The parent calls thr_join(); the child thread calls thr_exit() when it is done.

/* (a) no state variable */
void thr_exit() { lock(&m);
  signal(&c); unlock(&m); }
void thr_join() { lock(&m);
  wait(&c, &m); unlock(&m); }
/* (b) no lock, uses if */
void thr_exit() { done = 1;
  signal(&c); }
void thr_join() {
  if (done == 0) wait(&c); }
  1. In (a), what happens if the child finishes before the parent enters thr_join()?
  2. In (b), give an interleaving in which the parent sleeps forever.
  3. Implement sem_down / sem_up using only one mutex and one condition variable.
  4. In an 8-core approximate counter with S = 1024, how far can get() lag behind the actual value? What if S = 1?
Answers
  1. With no waiter, the signal is lost; the parent then sleeps and never wakes. You need a done variable that records the event, and while (done == 0).
  2. The parent sees done == 0 and is interrupted before wait; the child sets done = 1 and signals into the void; the parent resumes and sleeps. The test and the sleep must be under the same lock.
  3. sem_down: lock; while (value <= 0) wait(&c,&m); value--; unlock. sem_up: lock; value++; signal(&c); unlock. In this version the value never goes negative.
  4. 8 × 1023 = 8,184. With S = 1 every increment goes to the global counter: the result is exact, but as slow as a single-lock counter.

Summary

Review Questions (1/2)

  1. State the three requirements for a critical-section solution. Which one does strict alternation violate?
  2. Why can Peterson's solution fail on x86? How is it fixed?
  3. Which requirement does a simple test_and_set spinlock fail to guarantee?
  4. In a CAS-based increment, what happens if two threads read the same value? Why is the final value correct?
Answers
  1. Mutual exclusion, progress, bounded waiting; strict alternation violates progress.
  2. The store buffer lets a store complete after a later load, so both read false; use a full barrier or seq_cst atomics.
  3. Bounded waiting: the same thread may lose every time.
  4. One succeeds; the other's CAS fails and it rereads; no increment is lost.

Summary

Review Questions (2/2)

  1. A semaphore starts at 2 and three processes call wait in a row. What are the value and the queue (Silberschatz implementation)?
  2. In the bounded-buffer solution, what happens if the producer's two down calls are swapped?
  3. Why must a condition-variable wait under Mesa semantics be inside a while?
  4. Define priority inversion. How does priority inheritance solve it?
Answers
  1. Value −1; the first two pass, the third waits in the queue.
  2. With the buffer full, the producer sleeps holding the mutex and the consumer sleeps on the mutex: deadlock.
  3. The awakened thread doesn't run at once; the condition may be falsified meanwhile, and spurious wakeups can occur.
  4. While a high-priority task waits for a lock held by a low-priority task, medium-priority tasks cut in; with inheritance the lock holder temporarily takes the waiter's priority.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 (Chapter 6: Synchronization Tools, Chapter 7: Synchronization Examples).
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson (§2.3 Interprocess Communication, §2.5 Classical IPC Problems).
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org: "Locks", "Condition Variables", "Semaphores", "Common Concurrency Problems".
  • A. B. Downey, The Little Book of Semaphores, 2nd ed., Green Tea Press (free): readers–writers and philosophers solutions.
  • POSIX.1-2017 (IEEE Std 1003.1): pthread_mutex_lock, pthread_cond_wait, sem_overview(7); the Linux futex(2) man page.
  • B. Goetz et al., Java Concurrency in Practice, 2006; Java SE 21 API: java.util.concurrent; JEP 491.
  • G. L. Peterson, “Myths About the Mutual Exclusion Problem”, IPL, 1981; M. Jones, “What really happened on Mars?”, 1997.

The End

Chapter 6: Synchronization
Previous: Chapter 5: CPU Scheduling · Next: Chapter 7: Deadlocks
1 / 1