Gate2018 cs Q56

0. Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is N. Three semaphores empty, full and mutex are defined with respective initial values of 0, N and 1. Semaphore empty denotes the number of available slots in the buffer, for the consumer to read from. Semaphore full denotes the number of available slots in the buffer, for the producer to write to. The placeholder variables, denoted by P, Q, R and S, in the code below can be assigned either empty or full. The valid semaphore operations are: wait ( ) and signal ( ).
 Producer Consumer
  do{
  wait(p);
  wait(mutex);
  //Add item to buffer
  signal(mutex);
  signal(Q);
}while (1);
 do{
  wait(R);
  wait(mutex);
  //Consume item from buffer
  signal (mutex);
  signal ();
}while (1);

  • Option : C
  • Explanation :
    Full = N, empty = 0, mutex = 1
    Initially buffer will be empty, so consumer should not start first, so options B, D are eliminated.
    With option A consumer will never consume the item, so it is wrong.
    Option ‘C’ is the correct answer which proper functionality of produce and consumer.
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *