A producer generates data and puts it into a buffer, while a consumer takes data from the buffer. The buffer has limited size — producer must wait if full, consumer must wait if empty.
A classic synchronization problem solved using three semaphores: empty (counts free slots), full (counts filled slots), and mutex (protects buffer access).
- Producer waits on
emptysemaphore (decrements) - Producer waits on
mutex(enters critical section) - Producer adds item to buffer
- Producer signals
mutex(exits critical section) - Producer signals
full(increments) - Consumer does the reverse
- Uses 3 semaphores: empty (N), full (0), mutex (1)
- Producer waits on empty, signals full
- Consumer waits on full, signals empty
- Mutex protects buffer data structure
- Built from: Semaphore, Counting Semaphore
- Builds into: Reader-Writer Problem
- Related: Wait Operation, Signal Operation
- Contrasts with: Reader-Writer (producer-consumer vs readers-writers)
- Wrong semaphore order can cause deadlock (always mutex last in, first out)
- Buffer must be protected by mutex during access
- Can be extended to multiple producers/consumers