In a system there are multiple producer process which produc
In a system, there are multiple producer process which produce number to a buffer and multiple consumer process which consume number from the buffer, where the buffer is shared among all producers and consumers. The following variables are shared among all processes:
int nextc= 0, nexttp = 0. buf[8];
semaphore full; empty; mutex;
Producer and Consumer process are given in the following C++ like pseudo programs:
Producer Process int itemp; Consumer Process int itemC; 1 itemp = rand (O); 2 wait (empty); 3 wait (mutex) ; 4 buf [nextp]=i temp; 5 nextp= (nextp+1) 88. ; signal (mutex) ; 7 signal (full) ; while true; 1 1 wait (full); I 2 wait (mutex) ; 3 itemC -buf (nextC); | 4 nextc= (nextC+1) 88 ; I 5 signal (mutex) ; I 6 signal (empty); T7 CoutSolution
a)
critical section in producer is 4 and 5th line
critical section in consumer is 3 and 4th line
b) empty =8 , full =0, mutex=1
c) yes mutual exclusion is satisfied. Whenever producer is in critical section then the
value of mutex will be 0, because of this value consumer can not enter into the critical section.
d) If we switch the given order then it can cause deadlock.
Let\'s consider a following situation:
Producer Consumer
wait(mutex) wait(mutex)
wait(empty) wait(full)
Let\'s assume consumer first execut wait(mutex), then mutex will become 0.
Full=0, hence consumer can\'t proceed.
mutex =0 , hence producer can\'t produce anything.
Hence, from the above situation system will be in deadlock.
