Producer / Consumer with Bounded Buffer, multiple producers/consumers with Semaphores
-
12-11-2019 - |
Question
I'm a bit stuck on the multiple consumer/producer problem. It appears in my lecture notes, but I simply cannot understand why the single consumer/producer approach won't work.
The typical approach for 1 consumer and 1 producer looks like this :
Producer :
while(true)
emptyBuffers.P();
mutex.P();
buffer.insert(produced item);
mutex.V();
fullBuffers.V();
Consumer :
while(true)
fullBuffers.P();
mutex.P();
buffer.consume(consumed item);
mutex.V();
emptyBuffers.V();
Why will this not work if I have more than 1 producer and/or more than 1 consumer? I've looked everywhere, but I can't find an answer I understand :s.
The mutex semaphore makes sure there are no 2 processes working in the buffer at the same time, so I don't see how this property can possibly change if you have more processes...
The 'solution' is that you change the mutex into a ProducerMutex and a ConsumerMutex. But now that would mean a producer and a consumer CAN be in the buffer at the same time, which should not be allowed, right?
I'm really not getting this :s
No correct solution