The code for producer consumer problem 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.