0% found this document useful (0 votes)
9 views

Producer-Consumer Problem

The document describes a program to implement the producer-consumer problem using semaphores. The program defines producer and consumer functions that use semaphores to access a shared buffer and add or remove items. Threads are created for the producer and consumer and joined at the end of the program.

Uploaded by

deepanshu
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)
9 views

Producer-Consumer Problem

The document describes a program to implement the producer-consumer problem using semaphores. The program defines producer and consumer functions that use semaphores to access a shared buffer and add or remove items. Threads are created for the producer and consumer and joined at the end of the program.

Uploaded by

deepanshu
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/ 4

Producer-Consumer Problem

1 AIM
Program to implement Producer-Consumer Problem.

2 CODE
#include <iostream>
#include <thread>
#include <semaphore.h>
#include <cstdlib>
#include <chrono>
#include <iomanip>
using namespace std;

// Define the semaphore variables


sem_t mutex;
sem_t empty;
sem_t full;
// Define the buffer
int buffer[10];
// Define the producer function
void producer() {
int item;
while (true) {
// Produce an item
item = rand() % 100;

// Wait for the buffer to be empty


// auto start_time = chrono::steady_clock::now();
sem_wait(&empty);
// auto end_time = chrono::steady_clock::now();
auto start_time = chrono::steady_clock::now();
sem_wait(&mutex);
auto end_time = chrono::steady_clock::now();
cout << "Producer waited for " <<
chrono::duration_cast<chrono::microseconds>(end_time - start_time).count()
<< " ms to acquire mutex semaphore" << endl;

// Add the item to the buffer


for (int i = 0; i < 10; i++) {
if (buffer[i] == 0) {
buffer[i] = item;
break;
}
}
cout << "Item produced: " << item << endl;
// Signal that the buffer is full
sem_post(&mutex);
sem_post(&full);
}
}
// Define the consumer function
void consumer() {
int item;
while (true) {
// Wait for the buffer to be full
// auto start_time = chrono::steady_clock::now();
sem_wait(&full);
// auto end_time = chrono::steady_clock::now();
// cout << "Consumer waited for " <<
chrono::duration_cast<chrono::microseconds>(end_time - start_time).count()
<< " ms to acquire full semaphore" << endl;
auto start_time = chrono::steady_clock::now();
sem_wait(&mutex);
auto end_time = chrono::steady_clock::now();
cout << "Consumer waited for " <<
chrono::duration_cast<chrono::microseconds>(end_time - start_time).count()
<< " ms to acquire mutex semaphore" << endl;
// Remove the item from the buffer
for (int i = 0; i < 10; i++) {
if (buffer[i] != 0) {
item = buffer[i];
buffer[i] = 0;
break;
}
}

// Signal that the buffer is empty


cout << "Consumed item: " << item << endl;
sem_post(&mutex);
sem_post(&empty);
}
}
int main() {
// Initialize the semaphores
sem_init(&empty, 0, 10);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);

// Create the producer and consumer threads


thread producer_thread(producer);
thread consumer_thread(consumer);

// Join the threads


producer_thread.join();
consumer_thread.join();

// Clean up the semaphores


sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);

return 0;
}
3 OUTPUT

You might also like