Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Operating system, process synchronization Q3 (30): The classical producer/consum

ID: 3874351 • Letter: O

Question

Operating system, process synchronization

Q3 (30): The classical producer/consumer problem with bounded buffer of size Ncan be solved by using semaphore as follows: semaphore mutex=1; Semaphore ful1-0 Semaphore empty N consumer () producer do f do wait (full); wait (mutex) :4 // consume an item from buffer signal (mutex) i signal (empty) wait (empty) wait (mutex) // add the item to the buffer signal (mutex) signal (full) ) while (TRUE) ) while (TRUE): (10 marks) What problems would appear if we invoke wait (mutex)s betore wait (full) (20 marks) Consider a slightly modified version of producer/consumer problem (with bounded buffer). In this case, the producer does not produce any item unless the buffer is completely empty. When puffer is completely empty, the producer fills up the entire buffer. The consumer does not consume any tem unless the buffer is full. When buffer is full consumer consumes all the items. low would you modify the above solution to handle this problem? Please provide a psuedocode similar the above.

Explanation / Answer

Problem if we invoke wait(mutex) before wait(full) is as follows:

Initial condition:

mutex = 1
full = 0
empty = N

Now currently buffer is empty so producer has to put items in buffer

If we invoke wait(mutex) before wait(full) then if consumer comes up
early it will block on wait(full) after consuming mutex

After consumer, producer gets up and will block at wait(mutex). It can't proceed
and put item in buffer and if it does not put item in buffer it will not execute
signal(full) and consumer has to be woken up on "full" so it is a deadlock.

Any way this change in operation will disturb the basic logic. Empty and Full are
for checking the items in buffer and mutex ensures that buffere should not be accesssed
by both of them at the same time.So the original logic is correct because we need to ensure that buffer is accessed either by consumer or producer and not both.

The second part of the question is as follows:

Till produced fills the buffer upto N items consumer will not access the buffer and till
consumer consumes all the items producer should not put any item in the buffer

The code is as follows:

Consumer() {
    
    do {
      wait(full)
      wait(mutex)
      for(int i = 0 i < N; i++) {
         consume item
         wait(full)
         signal(empty)
      }
      signal(mutex)
    } while(TRUE)
}

Producer() {
    
    do {
      wait(empty)
      wait(mutex)
      for(int i = 0 i < N; i++) {
         produce item
         wait(empty)
         signal(full)
      }
      signal(mutex)
    } while(TRUE)
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote