Concurrency, Scheduling & Bounded Work
Concurrency is useful only when safety, liveness, ownership and capacity remain explicit. This section makes timing failures reproducible and overload behavior finite before those problems cross a network.
Section 2 of 7 · 9 units · about 29 hours
By the end you can Force a safety failure and a liveness failure with a deterministic schedule, repair both, then drive a bounded worker queue through saturation while preserving cancellation, finite memory and predictable recovery.
Units
- Threads, interleavings and memory consistency3h
- Mutexes, condition variables, semaphores and barriers4h
- Atomic operations, compare-and-swap and memory ordering3h
- Data races, lost updates and lost wakeups3h
- Deadlock, livelock, starvation and priority inversion3h
- Bounded queues, admission and backpressure4h
- Cancellation, deadlines and shutdown state machines3h
- Deterministic schedules, stress tests and contention4h
- Event loops, work stealing and structured concurrency2h
Build it
Mini project · about 10 to 14 hours
Bounded admission and clean shutdown for the service slice, with forced schedules
Port a closable bounded queue and drain states into the Python worker pool that will serve later requests. Force schedule-driven safety and liveness failures, repair them, then prove admission stays bounded and shutdown stays clean under overload.
- 1Keep the C programs from the units in
lab/concurrency: racy and clean counters, lost wakeup, deadlock and repair, closable queue and drain demo, rebuilt and asserted bymake checkwith and without ThreadSanitizer. - 2Write
kv/pool.pywithClosableQueueand aWorkerPoolowningOPEN,CLOSINGandCLOSED, where close stops admission, drains admitted jobs within a deadline, then cancels and reports the remainder, tested against the ported C queue cases. - 3Give every job a request identity and an absolute deadline on
time.monotonic(), drop expired jobs before their effect while counting them, and let running jobs observe cancellation between steps, with tests for both. - 4Force a double-apply failure by interleaving duplicate requests through check-then-act with a schedule shim, then claim each identity under a lock before its effect and keep the shim as a regression test.
- 5Force a lock-order deadlock with a shim that stages opposite acquisitions, repair it with one global order, and add a randomized stress test that compares final state against an exact oracle.
- 6Predict capacity from workers over service time, drive the pool past it across rising offered rates, and show held work stays bounded, offered work is accounted for, and latency recovers after the flood.
- 7Close the pool mid-flood so admission stops and admitted jobs finish or cancel within the deadline, wire
close()toSIGTERMthroughlab/supervise.py, and compare one lock-granularity choice with pinned repeats, naming what the interpreter lock permits the test to show.
What you hand in
kv/pool.pywith bounded admission, deadline cancellation and drain accounting, plus tests ported from the C queue cases.- The two forced-schedule regressions with their repairs, plus a randomized stress test against an exact oracle.
- Saturation, shutdown and contention reports with predictions, raw samples and recovery evidence, and the C programs under
make check.
How to check it
- Self-grade with the rubric below
How it's graded
Forced failures
Pass: Each shim reproduces its failure before the repair and passes after, with the shim kept as a regression test.
Great: The write-up explains why randomized stress alone could miss each failure, and kept runs show the miss.
Bounded work
Pass: Held work stays bounded at every offered rate, every offered job is accounted for, and expired jobs never start.
Great: The capacity prediction precedes the sweep, and the measured knee is explained against it.
Shutdown and evidence
Pass: Close stops admission at once with admitted work finished or counted, and the signal path works end to end.
Great: The contention comparison states what the interpreter lock can show, and raw times with pinning are kept.
Resources
- Operating Systems: Three Easy Pieces - concurrencyRemzi and Andrea Arpaci-Dusseau
Read Chapters 26-33 in order: threads, thread API, locks, locked data structures, condition variables, semaphores, bugs and event-based concurrency. Write the safety and liveness invariant before reading each worked pattern.
- OSTEP concurrency homework and codeOSTEP authors
Use x86.py and the main, vector, condition-variable and semaphore exercises to enumerate schedules. Preserve one lost update, one deadlock and one lost wakeup as minimized tests, then repair each invariant.
- AlgoMaster Concurrency - four bounded contractsAlgoMaster
Attempt Design a Closable Bounded Queue, Design Thread-Safe Rate Limiter, Design a Deadlock-Free Two-Key Executor, Design an Atomic Maximum With Compare-and-Swap, Design Deferred Callback Executor and Design Task Scheduler with Dependencies. For each, add a changed input, a deterministic failure schedule and an independent stress test outside the platform.
- AlgoMaster Concurrency lessonsAlgoMaster
Use the named lessons only: Processes vs Threads, Race Conditions and Critical Sections, Mutex, Condition Variables, Compare-And-Swap, Atomic Operations, Semaphores, Producer-Consumer Pattern, Bounded Buffer, Design Thread-Safe Blocking Queue, Barriers and Latches, Read-Write Locks, Coarse-grained vs Fine-grained Locking, Deadlock, Livelock, Starvation, Priority Inversion, Lost Signal/Wakeup, Spurious Wakeup, Concurrency Models and Thread Pool Pattern. Each unit names its lessons; read no other chapter for this section.
- The Little Book of SemaphoresGreen Tea Press
Use the rendezvous, barrier and reader-writer puzzles as extra semaphore practice after OSTEP Chapter 31. Skip the Python event material; this section works in C with pthreads.
- MIT 6.1810 - locks and parallelismMIT PDOS
Use Lectures 12-14 and one lock-lab exercise only after the user-space bounded queue passes. Measure the contention change and explain the invariant preserved by the revised locking scheme.
- Linux man-pages: pthreads and futexLinux man-pages project
Look up the exact pthread mutex, condition variable, barrier, cancellation and timed-wait contracts plus futex(2) and pthreads(7). Read the stated pages; do not browse the whole manual.
- cppreference: C atomic operationscppreference
Read
atomic_compare_exchangefor success and failure semantics and the expected-pointer update rule, andmemory_orderfor the six orderings and what relaxed permits. Stop before lock-free-property macros. - GCC __atomic builtinsGNU Compiler Collection
Read
__atomic_compare_exchange,__atomic_loadand__atomic_storefor the expected-pointer update rule and the__ATOMIC_RELAXEDconstant used by the CAS reproduce step.