0% found this document useful (0 votes)
4 views5 pages

Lab 13

The document is a lab manual for the Producer-Consumer Problem using semaphores, detailing the synchronization of producer and consumer processes with a shared buffer. It explains the use of semaphores to manage buffer slots and mutex for thread safety, along with providing C++ code examples for implementation. Additionally, it includes practice questions for students to reinforce their understanding of the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Lab 13

The document is a lab manual for the Producer-Consumer Problem using semaphores, detailing the synchronization of producer and consumer processes with a shared buffer. It explains the use of semaphores to manage buffer slots and mutex for thread safety, along with providing C++ code examples for implementation. Additionally, it includes practice questions for students to reinforce their understanding of the concepts presented.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operating System

Lab Manual

Topic: Producer-Consumer Problem


using Semaphore
Session: Fall 2024
School of Systems and Technology
UMT Lahore Pakistan
Page 1 of 5
LAB 10: Producer-Consumer Problem using Semaphore
The Producer-Consumer Problem is a classic synchronization problem that describes two processes: the
producer and the consumer. The producer generates data and places it in a buffer, and the consumer
removes the data from the buffer. The buffer has limited space, so synchronization is needed to ensure that:

● The producer doesn't add data when the buffer is full.


● The consumer doesn't remove data when the buffer is empty.

Semaphores are used to control access to the shared buffer. We typically use two types of semaphores:

1. Empty: Tracks how many empty slots are available in the buffer.
2. Full: Tracks how many filled slots are available for consumption.
3. Mutex: Ensures mutual exclusion while accessing the buffer.

Page 2 of 5
#include <iostream>
#include <thread>
#include <queue>
#include <semaphore> // C++20 semaphore support
#include <chrono>

using namespace std;

// Shared buffer and semaphores


queue<int> buffer;
const int bufferSize = 5; // Max size of buffer

semaphore emptySlots(bufferSize); // Initially, bufferSize


empty slots
semaphore fullSlots(0); // Initially, no full slots
mutex bufferMutex; // Mutex for protecting
shared buffer

void producer(int id) {


int item = 0; // Initial item value
while (true) {
// Produce an item (in this case, simply an
incrementing integer)
this_thread::sleep_for(chrono::milliseconds(500)); //
Simulate production delay
item++;

// Wait for an empty slot


emptySlots.acquire();

// Lock the buffer for mutual exclusion


lock_guard<mutex> lock(bufferMutex);
buffer.push(item);
cout << "Producer " << id << " produced item " << item
<< endl;

// Signal that a full slot is available


fullSlots.release();
}
}

void consumer(int id) {


while (true) {
// Wait for a full slot (an item to consume)
fullSlots.acquire();

// Lock the buffer for mutual exclusion


lock_guard<mutex> lock(bufferMutex);
int item = buffer.front();
buffer.pop();
cout << "Consumer " << id << " consumed item " << item
<< endl; Page 3 of 5

// Signal that an empty slot is available


emptySlots.release();
1. Semaphores:
○ emptySlots: Tracks how many empty slots are available in the buffer. Initially, it's set to
bufferSize, meaning all slots are available.
○ fullSlots: Tracks how many filled slots (produced items) are in the buffer. Initially, it's 0
since no items have been produced yet.
○ These semaphores control when the producer can add an item and when the consumer can
remove an item from the buffer.
2. Mutex: The bufferMutex ensures that only one thread (either a producer or a consumer) can
access the buffer at a time to avoid race conditions.
3. Producer:
○ The producer waits (acquire()) for an empty slot (emptySlots semaphore). Once an
empty slot is available, it locks the buffer using bufferMutex, adds the item, and then
signals that a new item is available by releasing the fullSlots semaphore.
4. Consumer:
○ The consumer waits (acquire()) for a full slot (fullSlots semaphore). Once an item is
available, it locks the buffer using bufferMutex, removes the item from the buffer, and
signals that an empty slot is available by releasing the emptySlots semaphore.
5. Threads: Multiple producer and consumer threads are created to simulate concurrent production and
consumption.
6. Delays: this_thread::sleep_for is used to simulate the time taken to produce and consume items.

Producer 1 produced item 1


Consumer 1 consumed item 1
Producer 2 produced item 2
Consumer 2 consumed item 2
Producer 1 produced item 3
Consumer 1 consumed item 3
Producer 2 produced item 4
Consumer 2 consumed item 4

● Semaphore Mechanism: The semaphores ensure that the producer does not overfill the buffer and
that the consumer does not try to consume from an empty buffer.
● Thread Safety: The mutex ensures that only one thread accesses the shared buffer at a time,
preventing race conditions.
● Multiple Producers and Consumers: The code handles multiple producers and consumers
simultaneously by using threads.

This solution assumes that you are using a C++20 compiler that supports the std::semaphore. If you're
using an older version of C++, you can replace std::semaphore with POSIX semaphores (sem_t) or
implement your own counting semaphore using mutexes and condition variables.

Practice Question:

Page 4 of 5
Please write, compile, and execute the provided program, using different input parameters as
outlined in your course. Ensure you submit the output to your instructor.

Q1: Recall Producer-Consumer Problem and answer the following CLO1 [10]

1. Semaphores + Semaphores mechanisms


2. Mutex
3. Producer
4. Consumer
5. Threads + Thread Safety

Page 5 of 5

You might also like