Producer–Consumer Problem Using Semaphores (Bounded Buffer)
Producer–Consumer Problem Using Semaphores (Bounded Buffer)
This introduces multiple producers, multiple consumers, and uses three semaphores:
-
empty,full,mutexIt is a classic synchronization problem demonstrating mutual exclusion + synchronization.
1. AIM
To implement the Producer–Consumer Problem using semaphores, ensuring proper synchronization between producers and consumers in a bounded buffer.
2. OBJECTIVE
-
To understand inter-process communication and synchronization.
-
To manage a shared circular buffer with multiple producers and consumers.
-
To use semaphores to prevent race conditions, buffer overflow, and buffer underflow.
3. THEORY
The Producer–Consumer Problem
-
Producer generates data and puts it into a buffer.
-
Consumer removes data from the buffer.
-
If the buffer is full, producers must wait.
-
If the buffer is empty, consumers must wait.
-
A mutex semaphore ensures only one process modifies the buffer at a time.
Semaphores Used
| Semaphore | Purpose |
|---|---|
mutex | Mutual exclusion on buffer |
empty | Counts empty slots (producer waits when 0) |
full | Counts full slots (consumer waits when 0) |
Concepts introduced in this experiment
✔ Bounded buffer
✔ Circular queue
✔ Producer and Consumer threads
✔ Counting semaphores
✔ Mutual exclusion + synchronization
✔ Handling race conditions
4. ALGORITHM
Producer Algorithm
Consumer Algorithm
5. C PROGRAM (Producer–Consumer with Bounded Buffer)
6. SAMPLE OUTPUT
binuvp@debian-workstation:~$ gcc producer.c
binuvp@debian-workstation:~$ ./a.out
Producer 1 produced 83 at buffer[0]
Producer 2 produced 86 at buffer[1]
Consumer 2 consumed 83 from buffer[0]
Producer 3 produced 77 at buffer[2]
Consumer 3 consumed 86 from buffer[1]
Consumer 1 consumed 77 from buffer[2]
Producer 1 produced 15 at buffer[3]
Producer 2 produced 93 at buffer[4]
Producer 3 produced 35 at buffer[0]
Consumer 2 consumed 15 from buffer[3]
Consumer 3 consumed 93 from buffer[4]
Consumer 1 consumed 35 from buffer[0]
Producer 1 produced 86 at buffer[1]
Producer 2 produced 92 at buffer[2]
Producer 3 produced 49 at buffer[3]
Producer 1 produced 21 at buffer[4]
Producer 3 produced 62 at buffer[0]
7. RESULT
The producer-consumer problem using semaphores was successfully implemented.
The program ensures proper synchronization between producer and consumer threads and prevents buffer underflow and overflow.
Comments
Post a Comment