Producer-Consumer Collaboration
An introduction to synchronization between a producer and a consumer using a one-message buffer and two semaphores.
Competition and collaboration
Section titled “Competition and collaboration”- competition occurs when multiple processes or threads contend for a shared resource;
- collaboration occurs when they coordinate their activities, for example by exchanging data through a buffer.
The producer and consumer collaborate: the producer prepares messages, and the consumer uses them.
A collaboration problem
Section titled “A collaboration problem”Imagine two processes working together:
- the producer creates a message and deposits it in a shared buffer;
- the consumer retrieves that message and uses it.
The buffer can hold only one message. The processes run concurrently:
- the consumer might arrive before the producer has deposited anything;
- the producer might want to deposit a message while the buffer is still full.
Two forms of coordination are needed: the consumer waits for new data, and the producer waits for the previous data to be retrieved.
A solution using two semaphores
Section titled “A solution using two semaphores”We use two binary semaphores, with atomic wait and signal operations:
| Semaphore | Meaning | Initial value |
|---|---|---|
deposited |
is 1 when a message is ready to be retrieved |
0 |
consumed |
is 1 when the buffer is available for a new deposit |
1 |
Initially, the buffer is empty: the consumer waits, while the producer can deposit a message.
When a process calls wait on a semaphore whose value is zero, it blocks until the other process calls the corresponding signal.
Solution pseudocode
Section titled “Solution pseudocode”The loops run in parallel; wait and signal are atomic semaphore operations.
buffer = Nonedeposited = Semaphore(0)consumed = Semaphore(1)
def producer(): while True: message = produce() consumed.wait() # wait until the buffer is free buffer = message # deposit the message deposited.signal() # notify the consumer
def consumer(): while True: deposited.wait() # wait for a message message = buffer # retrieve the message consumed.signal() # make the buffer available consume(message)Each deposit makes a message available; each retrieval frees the buffer.
The collaboration cycle
Section titled “The collaboration cycle”Diagram created with Mermaid
- The producer waits for the buffer to be free, deposits a message, and signals
deposited. - The consumer, waiting on
deposited, unblocks and retrieves the message. - The consumer signals
consumed: the producer can deposit the next message.
Why are two semaphores needed?
Section titled “Why are two semaphores needed?”The semaphores represent two different conditions:
depositedcontrols the Producer → Consumer handoff;consumedcontrols the Consumer → Producer handoff.
Each wait is paired with a signal from the other process. Waiting prevents both reading nonexistent data and overwriting data that has not yet been retrieved.
Interactive simulator
Section titled “Interactive simulator”Use the simulator to see how synchronization protects the shared buffer between the producer and consumer.
Summary
Section titled “Summary”Knowledge check
Section titled “Knowledge check”Quiz answers on this page are saved in your browser's local storage and remain available between visits.
What are the initial semaphore values in this solution?
-
deposited = 1andconsumed = 0. -
deposited = 0andconsumed = 1.Correct: initially, there are no messages to retrieve, and the empty buffer is available for the first deposit.
- Both semaphores are
1.
What happens when the consumer calls deposited.wait() while deposited is 0?
- The consumer blocks until the producer signals that a message has been deposited.
Correct:
depositedcoordinates the message handoff from the producer to the consumer. - The consumer reads the empty buffer and continues.
- The producer is blocked until the consumer consumes the message.
What is the correct order of the producer’s operations when depositing a message?
- Signal
deposited, wait forconsumed, then write to the buffer. - Wait for
consumed, write to the buffer, then signaldeposited.Correct: the producer waits for the buffer to be free, deposits the message, and then notifies the consumer.
- Write to the buffer, signal
consumed, then wait fordeposited.
Why does the producer call consumed.wait() before depositing?
- To check whether the consumer has started processing the message.
- To wait until the buffer is free, avoiding overwriting a message that has not yet been retrieved.
Correct: the consumer signals
consumedafter reading the message from the buffer. - To prevent the consumer from reading the message.
Which statement correctly describes the two semaphores?
-
depositedtells the consumer that a message is available;consumedtells the producer that the buffer is free.Correct: the semaphores coordinate two distinct handoffs between the producer and consumer.
- Both only indicate whether the buffer is empty.
-
depositedcounts producers andconsumedcounts consumers.
Quiz progress
0 / 0 questions answered (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0