Highlights

Contents

Part IV
04

Concurrency and synchronization

Two workers can read the same counter and erase one another’s update. Synchronization gives shared state an ordering contract, but each primitive has ownership, wakeup and fairness costs. We will build the critical-section requirements from atomic operations through mutexes, semaphores, condition variables, monitors and classic problems.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1OS fundamentals2Processes3Fork, exec and threads4Synchronization5Deadlocks6CPU scheduling7Paging and memory8Copy-on-write9Filesystems10I/O and IPC11Protection and Linux12Complete server trace

#Section 13Concurrency and parallelism

Finch’s 2 cores can execute 2 runnable threads at one instant, while a single core can still make several tasks progress by switching between them. Concurrency means tasks have overlapping lifetimes or make interleaved progress. Parallelism means work executes simultaneously on separate execution resources. The distinction predicts whether a lock protects a true simultaneous collision or an interleaving on one core.

A scheduler can preempt a thread between two instructions. An interrupt can also change the point at which another task runs. Therefore correctness cannot rely on a particular observed order, even on one core. Parallel hardware adds cache coherence and memory-order concerns, but the lost-update bug exists before two cores are involved.

TAKING TURNS AND RUNNING TOGETHER HAVE DIFFERENT SHAPESTAKING TURNS AND RUNNING TOGETHER HAVE DIFFERENT SHAPEScoreconcurrency on one coreABABABcorecoreparallelism on separate coresAAABBB
Figure 3.15Illustrative execution strips. One core interleaves A and B, while separate cores can execute them together. The strips show the distinction rather than measured task durations.

The right question is not “does this code have threads?” It is “which state can be observed between operations, and what order must readers and writers obey?” That question leads directly to races and critical sections.

#Section 14Race conditions and critical sections

The expression counter++ looks small but may load, add and store. With an initial value of 10, two threads can both load 10 and both store 11, even though the intended result is 12. A race condition occurs when the result depends on an uncontrolled timing relationship. A critical section is the code that must be protected from conflicting interleavings.

A solution must provide mutual exclusion, progress and bounded waiting. Mutual exclusion keeps conflicting entries apart. Progress says a choice can be made when no one is inside. Bounded waiting prevents one thread from being bypassed without limit. Atomicity describes an operation that observers cannot see halfway through; it is a property of the operation and memory model, not merely a short source line.

TWO THREADS READ 10, BOTH WRITE 11, AND ONE INCREMENT VANISHESTWO THREADS READ 10, BOTH WRITE 11, AND ONE INCREMENT VANISHESshared counter in RAM1011thread Aregister1011readwritethread Bregister1011readwriteexpected 12got 11read, add, write is three steps, not one
Figure 3.16Illustrative trace shows both reads of 10 and the two writes of 11. Orange marks the wrong final value, 11 instead of the computed 12.

The fix must cover the whole invariant, not only the final store. A lock around the read-change-write sequence works if every access uses the same lock. A lock around only one writer leaves readers or sibling writers outside the contract.

#Section 15Atomic operations

Hardware supplies small operations that cannot be interleaved by another observer. Compare-and-swap, or CAS, reads a word, compares it with an expected value and stores a desired value only if the comparison succeeds. A failed CAS tells the caller another writer changed the word, so the caller can reread and retry.

Test-and-set can acquire a one-bit lock. Fetch-and-add can reserve a distinct counter value. Atomic read and write are enough only for single-word state under the architecture’s guarantees. A multi-field invariant still needs a lock, a transaction protocol or a carefully proven lock-free algorithm.

COMPARE-AND-SWAP: WRITE 11 ONLY IF THE WORD STILL HOLDS 10COMPARE-AND-SWAP: WRITE 11 ONLY IF THE WORD STILL HOLDS 10thread Aexpected = 10desired = 11CAS(&x, 10, 11)x == 10 ?yes11stored, returns trueno11B already wrote 11reload 11, retry with expected 11, desired 12thread B
Figure 3.17Illustrative CAS makes the expected old value explicit and turns interference into a retry. Orange marks the compare step that rejects a stale writer.

Atomics also need a memory-ordering story. A successful publication must make the protected data visible in the intended order, not merely change a flag. The exact ordering names vary by language and architecture, but the interview answer should connect atomicity with visibility and ordering.

#Section 16Mutexes, semaphores and condition variables

A mutex is an ownership-based exclusion primitive. A thread locks it, executes the critical section and unlocks it; another contender may sleep instead of burning a core. A semaphore is a counter of permits or signals. A counting semaphore initialized to 2 allows two users, while a binary semaphore resembles a permit but does not necessarily encode owner identity.

A condition variable lets a thread sleep until shared state may satisfy a predicate. The waiter must hold the associated mutex, check the predicate in a loop, then wait atomically while releasing the mutex. A signal wakes a waiter, but the waiter must reacquire the mutex and check again because another thread may consume the condition first.

A MUTEX HAS ONE OWNER; WAITERS SLEEP INSTEAD OF SPINNINGA MUTEX HAS ONE OWNER; WAITERS SLEEP INSTEAD OF SPINNINGprotected dataowner T1T1 holds itlock()wait queuez zz zasleep, using no CPUunlock() wakes T2ownonly the owner may unlock
Figure 3.18Illustrative sequence shows mutex ownership, critical work, release and a sleeping waiter. Orange marks lock acquisition before protected work.
WAIT ON A CONDITION IN A LOOP: WAKING UP IS NOT PROOF THE QUEUE HAS WORKWAIT ON A CONDITION IN A LOOP: WAKING UP IS NOT PROOF THE QUEUE HAS WORKlock(m);while (queue.empty()) wait(cv, m);item = queue.pop();unlock(m);recheckshared queueconsumerz zproducersignalwait() releases the lock and sleeps in one atomic step,then takes the lock again before returninganother consumer may grab the item first
Figure 3.19Illustrative condition trace shows predicate checking, atomic release and wakeup. Orange marks the retry because wakeup is not proof.

Spinning can be cheaper for a very short wait, while blocking is better when the wait may last. A monitor packages shared state, mutual exclusion and condition operations into one abstraction. The primitive does not choose the invariant for you; the program must state what the protected state means.

Semaphores

The permit counter changes atomically. A producer waits for an empty-slot permit before inserting into a bounded queue; a consumer releases that permit after removing an item. The mutex still protects the queue links. Permit ownership is not mutex ownership, so replacing every mutex with a binary semaphore can admit the wrong thread’s release.

TWO PERMITS ADMIT TWO USERS; THE NEXT USER WAITSTWO PERMITS ADMIT TWO USERS; THE NEXT USER WAITSresource slotspermit heldpermit heldwaiterAB2 permits − 1 − 1 = 0 available
Figure 3.20Illustrative two initial permits are consumed to zero before another caller waits. Orange marks exhaustion of available permits.

Monitors

A monitor combines protected state, mutually exclusive methods and condition waiting. It makes the access contract visible at the object boundary. Under a wake-and-recheck convention, a notified caller must reacquire ownership and examine the predicate again. A notification carries no stored queue item by itself, so the queue state remains the durable fact within this in-memory protocol.

A MONITOR IS A ROOM: ONE THREAD INSIDE, A LINE AT THE DOOR, A BENCH FOR WAITERSA MONITOR IS A ROOM: ONE THREAD INSIDE, A LINE AT THE DOOR, A BENCH FOR WAITERSmonitor BoundedQueuemutex doorentry queueactivestateitems[4]count = 0put() take()conditionnot_emptyz zcallers just call put(); the exclusion is built in
Figure 3.21Illustrative protected state, methods and condition waiting share one access contract. Orange marks exclusion around the monitor’s state.