0

Hello =) I am trying to come up with a pseudocode for the following problem, but I'm a bit confused with the semaphores' sychronization, so I hope someone can help!

We have M consumers and M producers and 2 buffers of the same N size. The consumers consume from one buffer, and the producers produce on the other buffer. When the consumer buffer is empty and the producer buffer is full, the buffers swap and the processes continue.

Any ideas on how to sychronize those processes using semaphores? Thank you!

1 Answers1

0

You can use 2 semaphores: One for the producer buffer, one for the consumer buffer. Producer is locked for each write operation, consumer is locked for each read operation. Switching needs to lock both buffers.

Btw., what should happen if producer buffer is full and consumer buffer is not empty?

JohnB
  • 13,315
  • 4
  • 38
  • 65