Question

Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is NN. Three semaphores empty, full and mutex are defined with respective initial values of 0, NN 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{                                 do{
  wait(P);                            wait(R);
  wait(mutex);                        wait(mutex);
  //Add item to buffer                //Consume item from buffer
  signal(mutex);                      signal(mutex);
  signal(Q);                          signal(S);
}while(1);                          }while(1);

Which one of the following assignments to P, Q, R and S will yield the correct solution?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2018 answer key

Solution

Match the synchronization operations to the problem-defined semantics and initial values of semaphores.

Carefully read the semaphore definitions given in the stem:

  • empty has initial value 0 and denotes available slots for the consumer to read from (i.e. filled buffer slots).
  • full has initial value NN and denotes available slots for the producer to write to (i.e. empty buffer slots).

Determine the operations:

  1. Producer:
    • Must wait for a writable slot before producing: wait(P) requires P=fullP = \text{full}.
    • After adding an item, must signal that a readable item is available: signal(Q) requires Q=emptyQ = \text{empty}.
  2. Consumer:
    • Must wait for a readable item before consuming: wait(R) requires R=emptyR = \text{empty}.
    • After consuming an item, must signal that a free slot is available: signal(S) requires S=fullS = \text{full}.

Therefore, the correct assignments are: P:full,Q:empty,R:empty,S:fullP: \text{full}, Q: \text{empty}, R: \text{empty}, S: \text{full}, matching option C.

Final answer

C

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2018 Computer Science and Information Technology (CS)
Question
50 of 65, printed as CS Q.40
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.