VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/producer-consumer-problem-using-semaphores-set-1/

⇱ Producer Consumer Solution using Semaphores - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Producer Consumer Solution using Semaphores

Last Updated : 3 Sep, 2025

The Producer-Consumer problem is a classic example of a synchronization problem in operating systems. It demonstrates how processes or threads can safely share resources without conflicts. This problem belongs to the process synchronization domain, specifically dealing with coordination between multiple processes sharing a common buffer.

In this problem, we have:

  • Producers: Generate data items and place them in a shared buffer.
  • Consumers: Remove and process data items from the buffer.

The main challenge is to ensure:

  1. A producer does not add data to a full buffer.
  2. A consumer does not remove data from an empty buffer.
  3. Multiple producers and consumers do not access the buffer simultaneously, preventing race conditions.

Semaphore: The Synchronization Tool

A semaphore is an integer-based signaling mechanism used to coordinate access to shared resources. It supports two atomic operations:

  • wait(S): Decreases the semaphore value by 1. If the value is ≤0, the process waits.
  • signal(S): Increases the semaphore value by 1, potentially unblocking waiting processes.

wait(S){

while(S <= 0); // busy waiting
S--;

}

signal(S){
S++;

}

Problem Statement

Consider a fixed-size buffer shared between a producer and a consumer.

  • The producer generates an item and places it in the buffer.
  • The consumer removes an item from the buffer.

The buffer is the critical section. At any moment:

  • A producer cannot place an item if the buffer is full.
  • A consumer cannot remove an item if the buffer is empty.

To manage this, we use three semaphores:

  • mutex – ensures mutual exclusion when accessing the buffer.
  • full – counts the number of filled slots in the buffer.
  • empty – counts the number of empty slots in the buffer.

Semaphore Initialization

mutex = 1; // binary semaphore for mutual exclusion
full = 0; // initially no filled slots
empty = n; // buffer size

Producer

do {

// Produce an item
wait(empty); // Check for empty slot
wait(mutex); // Enter critical section

// Place item in buffer

signal(mutex); // Exit critical section
signal(full); // Increase number of full slots
} while (true);

Consumer

do {
wait(full); // Check for filled slot
wait(mutex); // Enter critical section

// Remove item from buffer

signal(mutex); // Exit critical section
signal(empty); // Increase number of empty slots
} while (true);

Explanation:

  • Empty ensures that producers don’t overfill the buffer.
  • Full ensures that consumers don’t consume from an empty buffer.
  • Mutex ensures mutual exclusion, so only one process accesses the buffer at a time.

Code Example Using POSIX Semaphores

The Producer-Consumer problem is a fundamental example of process synchronization in operating systems. Using semaphores and mutexes, we can ensure:

  • Producers don’t overfill the buffer.
  • Consumers don’t consume from an empty buffer.
  • Mutual exclusion is maintained when accessing the buffer.

This approach prevents race conditions, data inconsistency, and deadlocks, making it a reliable solution for coordinating multiple processes.

Comment
Article Tags:
Article Tags: