OS Notes
The classic producer-consumer (bounded buffer) synchronization problem — solutions using semaphores, mutex locks, and monitors with complete code examples.
Introduction
The producer-consumer problem is one of the most important and frequently occurring synchronization problems in computing. A producer process generates data and places it in a buffer. A consumer process takes data from the buffer and uses it. They must synchronize so that the producer does not add data to a full buffer and the consumer does not try to remove data from an empty buffer.
This pattern appears everywhere: a web server producing log entries while a logger consumes them, a keyboard driver producing keystrokes while the terminal application consumes them, a video decoder producing frames while the display driver consumes them.
Problem Statement
- A shared buffer of size N exists between producer and consumer processes
- The producer generates items and places them in the buffer
- The consumer removes items from the buffer and processes them
- The producer must wait if the buffer is full
- The consumer must wait if the buffer is empty
- Access to the buffer must be mutually exclusive (only one modifies at a time)
Solution Using Semaphores
Why the Order of sem_wait Matters
CRITICAL: The order sem_wait(&empty) then sem_wait(&mutex) is essential. If reversed:
// WRONG ORDER — causes deadlock!
sem_wait(&mutex); // Got mutex
sem_wait(&empty); // Buffer full! Block... but still hold mutex!
// Consumer cannot acquire mutex to remove items
// → DEADLOCKAlways wait on the counting semaphore (condition check) BEFORE the mutex (critical section entry).
Solution Using Mutex + Condition Variables
Multiple Producers and Consumers
The semaphore solution naturally supports multiple producers and multiple consumers. The mutex ensures only one thread modifies the buffer at a time, while the counting semaphores track available slots regardless of how many producers or consumers exist.
Real-World Applications
| Application | Producer | Consumer | Buffer |
|---|---|---|---|
| Print spooling | Applications | Printer driver | Print queue |
| Web server | Network listener | Worker threads | Request queue |
| Video streaming | Decoder | Display driver | Frame buffer |
| Logging | Application threads | Log writer | Log buffer |
| Keyboard input | Keyboard driver | Application | Key buffer |
Real-World Analogy
The producer-consumer problem is like a sushi conveyor belt restaurant. The chef (producer) places sushi plates on the belt (buffer). Customers (consumers) take plates off. If the belt is full, the chef waits. If the belt is empty, customers wait. Only one person can place or take a plate at each position at a time (mutual exclusion). The belt has finite capacity (bounded buffer).
Key Takeaways
- The producer-consumer problem requires three types of synchronization: mutual exclusion, full-buffer blocking, and empty-buffer blocking
- The semaphore solution uses three semaphores: mutex (mutual exclusion), empty (available slots), full (occupied slots)
- Order of semaphore operations is critical — always wait on counting semaphore before mutex
- Condition variables with mutex provide an equivalent modern solution
- The pattern appears universally in systems: queues, buffers, pipelines
- Multiple producers and consumers are handled naturally by the same synchronization structure
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Producer-Consumer Problem.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, synchronization, producer, consumer
Related Operating Systems Topics