{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

producer_consumer - #include #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <stdlib.h> #include<windows.h> # using namespace std; u HANDLE mutex; HANDLE prod,cons; H #define BUFFER_SIZE 25 int runFlag = 1; typedef struct { int number; int buffer[BUFFER_SIZE]; }Items; int isBufferFull( Items* ); int isBufferEmpty( Items* ); int produce( Items*, int ); int consume( Items* ); Items it; Items* buffer = &it; I void producer() { cout << "\nProducer Started"; int produced, i; while( runFlag ) { while( isBufferFull(buffer) ) { ReleaseMutex(mutex); //release mutex when buffer is full WaitForSingleObject(prod,INFINITE); //wait to acquire mutex } //wait till buffer is empty //beginning of critical section cout << "\nProducer entering critical section"; produced = rand()%50 + 1; cout << "\nProduced: " << produced; produce( buffer,produced ); cout << "\nProducer exiting critical section"; //end of critical section PulseEvent(cons); Sleep( 100 ); } cout << "\nProducer Terminated!"; } void consumer() { cout << "\nConsumer Started"; int i;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
while( runFlag ) { while( isBufferEmpty(buffer) ) { ReleaseMutex(mutex); //release mutex when buffer is empty WaitForSingleObject(cons,INFINITE); //wait to acquire mutex }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}