Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Implement Producer/Consumer problem using a queue as shared data with m

ID: 3688237 • Letter: P

Question

Problem:

Implement Producer/Consumer problem using a queue as shared data with mutual exclusion, synchronizing Producer/Consumer activities using threads in Python3 or C++.

Directions:

1. Producer and Consumer are two threads that will run concurrently

2. Producer starts a loop but goes to sleep right away for a random number of seconds

3. When producer wakes up it will print the first item it produces (ex. 1) and then puts it in a queue and then repeats step 2 and 3 until all items are produced in order and placed in the queue

4. Consumer also starts a loop to take one item at a time from the queue but it goes to sleep right away for a random number of seconds

5. When consumer wakes up it will print which item it is attempting to take then it will try to take it (if generated) from the queue

6. If item is not in queue yet consumer gets blocked waiting for that item to be added to the queu

7. When item added, it will be taken and printed by consumer

8. This process continues until all items are produced and consumed

9. Implementing a fixed size queue. For example a queue of n elements

Algorithm:

Producer

(indent once) Loop

(indent twice) Go to sleep

(indent twice) Print item i

(indent twice) Put item i in queue

Consumer

(indent once) Loop

(indent twice) Go to sleep

(indent twice) Print attempting to take item i from queue

(indent twice) Block consumer if item i is not in queue

(indent twice) Take item i from queue

(indent twice) Print item i

(Produced items are consumed in the same order)

Explanation / Answer

Algorithm:

#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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote