Producer_consumer - #include #include #include u using namespace std HANDLE mutex H HANDLE prod,cons#define

Info iconThis preview shows pages 1–2. 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 DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/24/2010 for the course COS 331 taught by Professor Galletly during the Spring '10 term at American University in Bulgaria.

Page1 / 3

Producer_consumer - #include #include #include u using namespace std HANDLE mutex H HANDLE prod,cons#define

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online