1,289 views
3 votes
3 votes

The code for producer consumer prob​lem using semaphore is

producer's code
 

do { *

// produce an item in nextproduced

wait(empty) ;
wait(mutex) ;

buffer[in] = nextProduced;
in = (in + 1) % BUFFER-SIZE;
signal(mutex) ;
signal(full) ;
}
while (TRUE) ,-


consumer's code


do {

wait(full);
wait(mutex);

nextConsumed = buffer[out] ;
out = (out + 1) % BUFFEFLSIZE;
signal(mutex) ;
signal(empty) ;
}
while (TRUE) ;


My question is why do we use same mutex for both producer and consumer. Since producer does not affect 'out' and
consumer does not affect 'in', so we could have used mutex1 and mutex2.

1 Answer

3 votes
3 votes
the problem are independent they are interrelated. the mutax is the variable which tells the consumer is whether a item is present to consume . or how many item are pressent. if it is not the case. if i use mutax 1. and mutax 2. suppose mutax one for producer. it will go on producing while mutax1  is full. then the consumer will not be able to start as mutax 2 will be zero ( if initally taken as false.) its value has not been changed. else if u start with max value of mutax 2 cnsumer will consume all the data of mutax 2 and the sytem will go in live lock. neither producer will produce anything nor the consumer will be able to consume because it will not be knowing that item is present.
simple exaple. u are playing counter strike . u took another server and another team took another server. even i start with 5 -5 player initally . suppose u killed their one player  now how will second team will come to know that their one player is dead. it is not possible till a common interface is used.

Related questions

0 votes
0 votes
1 answer
4
ajayraho asked Oct 23, 2022
909 views
What is the significance of infinite loop that is written in every example of process synchronisation? What would happen if there wasn't any infinite loop?