Write two programs to demonstration the Implemenation of Producer-Consumer Probl
ID: 3643222 • Letter: W
Question
Write two programs to demonstration the Implemenation of Producer-Consumer Problem in C++ language. Use 1 producer thread and 5 consumer threads.
One program should not use semaphore.
The other program should use sempahores.
Look at http://en.wikipedia.org/wiki/Producer-consumer_problem to see implementations for both.
Add sleep commands where appropriate.
Explanation / Answer
#include #include #include #define FULL 1 #define EMPTY 0 HANDLE hmutex; HANDLE hproducer,hconsumer; int temp,prod=0,cons=0; struct node1 { int info1; struct node1 *next1; }; struct node2 { int info2; struct node2 *next2; }; typedef struct node1 *NODEPTR1; NODEPTR1 makeNode1(int); typedef struct node2 *NODEPTR2; NODEPTR2 makeNode2(int); class list1 { public: list1(); ~list1(); int emptylist1(); void insertend1(int); void printlist1(); void addFront1(int); void deleteFront1(); void deletespecific1(int); private: NODEPTR1 listptr1; }; class list2 { public: list2(); ~list2(); int emptylist2(); void insertend2(int); void printlist2(); void addFront2(int); void deleteFront2(); private: NODEPTR2 listptr2; }; list1::list1() { listptr1=0; } list2::list2() { listptr2=0; } list1::~list1() { NODEPTR1 CurrPtr1; if(emptylist1()) return; while(listptr1!=NULL) { CurrPtr1=listptr1; listptr1 =listptr1->next1; delete CurrPtr1; } } list2::~list2() { NODEPTR2 CurrPtr2; if(emptylist2()) return; while(listptr2!=NULL) { CurrPtr2=listptr2; listptr2 =listptr2->next2; delete CurrPtr2; } } void list1::addFront1(int value) { NODEPTR1 CurrPtr1, newNode1; newNode1 = makeNode1(value); CurrPtr1= listptr1; newNode1->info1=value; if(emptylist1()) { newNode1->next1 = listptr1; listptr1= newNode1; } else { for(CurrPtr1=listptr1;CurrPtr1->next1!=NULL;CurrPtr1=CurrPtr1->next1); newNode1->next1= listptr1; listptr1 = newNode1; } } void list2::addFront2(int value) { NODEPTR2 CurrPtr2, newNode2; newNode2 = makeNode2(value); CurrPtr2= listptr2; newNode2->info2=value; if(emptylist2()) { newNode2->next2 = listptr2; listptr2= newNode2; } else { for(CurrPtr2=listptr2;CurrPtr2->next2!=NULL;CurrPtr2=CurrPtr2->next2); newNode2->next2= listptr2; listptr2 = newNode2; } } NODEPTR1 makeNode1(int number) { NODEPTR1 newNode1; if(!(newNode1 = new node1)) { coutnext1=NULL; return newNode1; } NODEPTR2 makeNode2(int number) { NODEPTR2 newNode2; if(!(newNode2 = new node2)) { coutnext2=NULL; return newNode2; } void list1::printlist1() { NODEPTR1 CurrPtr1; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.