Part 1 of 7 · 1 min read
Why this page comes sixth
In this module you will
- State the producer-consumer problem and its three requirements.
- Implement a bounded buffer with semaphores, in the right order.
- Implement the same buffer with a mutex and two condition variables.
- Show how a wrong order of lock and wait causes a deadlock, and why.
- Stop producers and consumers cleanly with stop markers.
Pages 3 to 5 gave three tools: the mutex, the condition variable and the semaphore. This page uses all of them on the pattern that appears in nearly every concurrent program: one group of threads makes items, another group uses them, and a queue sits between them. Pipes (Level 1, page 7), the thread pool of Level 2, page 5 and the work queue of the server in page 7 are all instances. The sources are "Operating Systems: Three Easy Pieces" (OSTEP), chapters 30 and 31, "Operating System Concepts" (OSC), chapter 7, and "Modern Operating Systems" (MOS), chapter 2. All programs were compiled with gcc -O2 -Wall -Wextra -pthread on a two-processor Linux machine.
About this example About this example
Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).
Scripted: The worked traces use small numbers chosen so they can be followed by hand.
Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.