Question: #include #include #include #include #define BUFFSIZE 5 sem _ t empty; / / semaphore empty sem _ t full; / / semaphore full int buffer
#include #include #include #include #define BUFFSIZE semt empty; semaphore empty semt full; semaphore full int bufferBUFFSIZE; pthreadmutext mutex; mutex lock int in ; int out ; int item ; void producervoid p for int i ; i ; i semwait∅ wait operation on semaphore empty pthreadmutexlock&mutex; lock mutex bufferin item; insert the item to the buffer item item ; printfproducer d: insert item d attbufferdintp bufferin in; in in BUFFSIZE; pthreadmutexunlock&mutex; unlock mutex sempost&full; signal operation on emaphore full void consumervoid c for int i ; i ; i semwait&full; pthreadmutexlock&mutex; int itemremoved bufferout; printfconsumer d: remove item d from bufferdintc itemremoved, out; out out BUFFSIZE; pthreadmutexunlock&mutex; sempost∅ int main pthreadt p c; array p and c are used to label producers and consumers, respectively pthreadmutexinit&mutex, NULL; initialize mutex to unlocked seminit&empty, BUFFSIZE; initialize semaphore empty to value of BUFFSIZE, which is seminit&full, ; initialize semaphore full to zero int a; keep track of the producer and consumer threads numbers for int i ; i ; icreate producers pthreadcreate&pi NULL, voidproducervoid&ai; for int i ; i ; icreate consumers pthreadcreate&ci NULL, voidconsumervoid&ai; for int i ; i ; imain thread waits for the newly created thread to finish pthreadjoinpi NULL; for int i ; i ; imain thread waits for the newly created thread to finish pthreadjoinci NULL; pthreadmutexdestroy&mutex; semdestroy∅ semdestroy&full; return ; a What do Lines and try to accomplish? b Why variables in out and item are defined as global variables? c How does the program ensure that a consumer does not grab an item from the buffer if the buffer is empty?
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
