Operating Systems : Producer Consumer Problem ( in C++ ) . In this assignment, y
ID: 3691121 • Letter: O
Question
Operating Systems: Producer Consumer Problem (in C++).
In this assignment, you are going to implement a solution to the producer-consumer problem. The problem consists of many (minimum of 4) producer threads and many (minimum of 3) consumer threads sharing a (memory) buffer with multiple (minimum of 20) slots (storage cells); each producer deposits one element (of whatever item each produces) into one slot of the buffer each time it "produces", and each consumer extracts one element from the buffer each time it "consumes". The buffer is treated as a circular queue, with insertions into one end of the queue, and extractions from the queue from the other end.Explanation / Answer
Algorithm for producer-consumer problem:
#define N 100 // slots in buffer
#define count=0 //items in buffer
void producer(void)
{
Int temp;
While(TRUE) {
Item= produce_item();
If(count==N) Sleep();
Insert_item(item);
Count=count+1;
If(count=1) wakeup(consumer);
void consumer (void)
{
Int item;
While(TRUE) {
If(count==0) sleep();
Item=remove_item();
count=count-1;
if(count==N-1)
wakeup(producer);
consume_item(item);
}
}
program code:
#define N 100 // slots in buffer
#define count=0 //items in buffer
void producer(void)
{
Int temp;
While(TRUE) {
Item= produce_item();
If(count==N) Sleep();
Insert_item(item);
Count=count+1;
If(count=1) wakeup(consumer);
void consumer (void)
{
Int item;
While(TRUE) {
If(count==0) sleep();
Item=remove_item();
count=count-1;
if(count==N-1)
wakeup(producer);
consume_item(item);
}
}
C++ code:
#include <iostream>
#include <thread>
#include <deque>
#include <mutex>
#include <chrono>
#include <condition_variable>
using std::deque;
std::mutex mu,cout_mu;
std::condition_variable cond;
class Buffer
{
public:
void add(int num) {
while (true) {
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [this](){return buffer_.size() < size_;});
buffer_.push_back(num);
locker.unlock();
cond.notify_all();
return;
}
}
int remove() {
while (true)
{
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [this](){return buffer_.size() > 0;});
int back = buffer_.back();
buffer_.pop_back();
locker.unlock();
cond.notify_all();
return back;
}
}
Buffer() {}
private:
deque<int> buffer_;
const unsigned int size_ = 10;
};
class Producer
{
public: Producer(Buffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = std::rand() % 100;
buffer_->add(num);
cout_mu.lock();
std::cout << "Produced: " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mu.unlock();
}
}
private: Buffer *buffer_;
};
class Consumer
{
public:Consumer(Buffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = buffer_->remove();
cout_mu.lock();
std::cout << "Consumed: " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mu.unlock();
}
}
private:
Buffer *buffer_;
};
int main() {
Buffer b;
Producer p(&b);
Consumer c(&b);
std::thread producer_thread(&Producer::run, &p);
std::thread consumer_thread(&Consumer::run, &c);
producer_thread.join();
consumer_thread.join();
getchar();
return 0;
}
Note: minute correction might be needed. but code is correct.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.