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

What if we want to do a unbounded buffer, where there is no upper limit on the b

ID: 3739357 • Letter: W

Question

What if we want to do a unbounded buffer, where there is no upper limit on the buffer size, but you just have to make sure that the consumers cannot underflow the buffer (i.e., cannot consume when the buffer is empty). Write the code for this bounded buffer. Use a C++ STL queue for the underlying data structure. No spamming please! What if we want to do a unbounded buffer, where there is no upper limit on the buffer size, but you just have to make sure that the consumers cannot underflow the buffer (i.e., cannot consume when the buffer is empty). Write the code for this bounded buffer. Use a C++ STL queue for the underlying data structure. No spamming please!

Explanation / Answer

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <cmath>

#include <queue>

#include <thread>

using namespace std;

typedef int Semaphore;

Semaphore

P(Semaphore s)

{

s = s - 1;

while(s < 0) {

}

}

V(Semaphore s)

{

s = s + 1;

}

Semaphore nItem=0; // number of items in the buffer

Semaphore mutex=1;

void producer() {

P( mutex );

V( mutex );

cout<<"produced item"<<endl;

V( nItem );

}

void consumer() {

P( nItem );

P( mutex );

V( mutex );

}

int main(){

printf(" 1.Producer 2.Consumer 3.Exit");

while(1)

{

printf(" Enter your choice:");

scanf("%d",&n);

switch(n)

{

case 1:

std::thread producer();

case 2:

std::thread consumer();

case 3:

exit(0);

break;

}

}

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