Search code examples
mutexsemaphoreproducer-consumer

Why are two semaphores and one mutex required in solving bounded buffer instance of producer-consumer?


Why must one use a mutex in addition to a semaphore when using a bounded buffer in producer consumer problem?


Solution

  • empty:semaphore(n)
    full: semaphore(0)
    mutex: semaphore(1)
    

    "mutex" is used to lock on Buffer.

    "full" is used to block consumer if buffer is empty.

    "empty" is used to block producer if buffer is full.

    That's why you need 3 semaphores.

    You can easily google the code so I don't paste it here.