Question
Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is . Three semaphores empty, full and mutex are defined with respective initial values of 0, 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?
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:
emptyhas initial value 0 and denotes available slots for the consumer to read from (i.e. filled buffer slots).fullhas initial value and denotes available slots for the producer to write to (i.e. empty buffer slots).
Determine the operations:
- Producer:
- Must wait for a writable slot before producing:
wait(P)requires . - After adding an item, must signal that a readable item is available:
signal(Q)requires .
- Must wait for a writable slot before producing:
- Consumer:
- Must wait for a readable item before consuming:
wait(R)requires . - After consuming an item, must signal that a free slot is available:
signal(S)requires .
- Must wait for a readable item before consuming:
Therefore, the correct assignments are: , matching option C.
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.