Question: please can someone fix this problem for me and run the code to see the output thank you #include #include #include #include #define MAX _
please can someone fix this problem for me and run the code to see the output thank you
#include
#include
#include
#include
#define MAXTHREADS # of threads
using namespace std;
initialize the variables
int counterLimit ;
int counter ;
int bufferSize;
int buffer;
int in ;
int out ;
function prototypes
DWORD WINAPI producerThreadLPVOID;
DWORD WINAPI consumerThreadLPVOID;
HANDLE hThreadsMAXTHREADS; # of threads
DWORD idMAXTHREADS; array of thread ids
DWORD waiter;
main function
int main
srandtime; initialize randomization
read the buffer size
cout "Enter the buffer size: ;
cin bufferSize;
allocate memory fo the buffer
buffer int mallocbufferSize sizeofint;
read counter limit
cout "Enter the counter limit: ;
cin counterLimit;
cout endl;
create the threads
hThreads CreateThreadNULL producerThread, LPVOID NULL, &id; thread for random producer
hThreads CreateThreadNULL consumerThread, LPVOID NULL, &id; thread for random consumer
waiter WaitForMultipleObjectsMAXTHREADS, hThreads, TRUE, INFINITE;
forint i ; i MAXTHREADS; i
CloseHandlehThreadsi;
print status of threads
cout
Thread ended producerThread endl;
cout "Thread started consumerThread
endl;
systempause;
return ;
producer thread
DWORD WINAPI producerThreadLPVOID n
cout "Thread started producerThread
endl;
whilecounter counterLimit && counter bufferSize
whilein bufferSize out;
int nextProduced rand;
SleepnextProduced ;
bufferin nextProduced;
cout "producerThread has produced nextProduced endl;
in in bufferSize;
counter;
return DWORDn;
consumer thread
DWORD WINAPI consumerThreadLPVOID n
cout "Thread started consumerThread endl;
int counter ;
while counter counterLimit && counter bufferSize
whilein out;
Sleeprand;
int nextConsumed bufferout;
cout "consumerThread has consumed nextConsumed endl;
out out bufferSize;
counter;
if counterLimit bufferSize
cout "The buffer is full." endl;
return DWORDn;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
