Question: CGS3763 - Semaphore / Mutex Code Explanation You will need to explain step by step what is happening in the program. Please use paragraph writing
CGS3763 - Semaphore / Mutex Code Explanation You will need to explain step by step what is happening in the program. Please use paragraph writing in a Word document.
//HW1
semaphore full; semaphore empty; binarysem mutex; int bucket[5]; int i=0; int j=0; int count=0;
void producer() { int k; for(k=0;k<20;k++) { p(full); //decrement 1 p(mutex); bucket[i]=count++; //produces item cout<<"Item "<< bucket[i]<<" produced."< i=(i+1)%5; v(mutex);//increment v(empty); } }
void consumer() { int x; for(x=0;x<20;x++) { p(empty);//decrement 1 p(mutex);
cout<<"Item "<
v(mutex); v(full);//increment 1 } }
void main() { //initialize initialsem(mutex,1); initialsem(full,5); initialsem(empty,0);
cobegin { producer(); consumer(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
