Skip to content

Producer-Consumer Collaboration

An introduction to synchronization between a producer and a consumer using a one-message buffer and two semaphores.

In concurrent programming:

  • 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.

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.

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.

The loops run in parallel; wait and signal are atomic semaphore operations.

buffer = None
deposited = 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.

sequenceDiagram participant P as Producer participant B as Buffer (1 message) participant C as Consumer P->>P: produce message P->>P: wait(consumed) P->>B: deposit message P->>C: signal(deposited) C->>C: wait(deposited) C->>B: retrieve message C->>P: signal(consumed) C->>C: consume message

Diagram created with Mermaid

  1. The producer waits for the buffer to be free, deposits a message, and signals deposited.
  2. The consumer, waiting on deposited, unblocks and retrieves the message.
  3. The consumer signals consumed: the producer can deposit the next message.

The semaphores represent two different conditions:

  • deposited controls the Producer → Consumer handoff;
  • consumed controls 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.

Use the simulator to see how synchronization protects the shared buffer between the producer and consumer.

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 = 1 and consumed = 0.
  • deposited = 0 and consumed = 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: deposited coordinates 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 for consumed, then write to the buffer.
  • Wait for consumed, write to the buffer, then signal deposited.

    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 for deposited.

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 consumed after reading the message from the buffer.

  • To prevent the consumer from reading the message.

Which statement correctly describes the two semaphores?

  • deposited tells the consumer that a message is available; consumed tells 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.
  • deposited counts producers and consumed counts consumers.

Quiz progress

0 / 0 questions answered (0%)

0 correct answers