Q52: Producer Consumer Problem

Question #52: Following is a code for Producer Consumer problem.

producer_consumer

Which of the following statements is true?

  1. There is no race condition in the above code.
  2. It is possible that producer sleeps, but consumer never wakes it up.
  3. There is a race condition because variable count is being read/written in both the consumer & producer without mutual exclusion.
  4. Both B & C.

Solution: Clearly, count is a shared variable and is being in both producer and consumer, hence there is a race condition because of violation of mutual exclusion. Now, take the situation when producer sees that buffer is full and before going to sleep, a context switch occurs. Then, consumer consumes an item and tries to wake up producer, but producer is not yet sleeping. Then, again context switch occurs and now producer goes to sleep! Now, there is no one to wake up the producer. This is called lost wakeup problem. Hence, the correct answer is option 4.