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
That's the solution given in http://en.wikipedia.org/wiki/Producer-consumer_problem (modulo naming). Of course, that also states that you don't need the mutex for the 1P/1C version (which would be valid if the buffer had only 1 slot). But its also the same solution as given in http://cs.gmu.edu/cne/modules/ipc/purple/prodsem.html.
Bottom line: I think you're getting this just fine, and whomever gave you this 2-mutex "solution" isn't.