Skip to content
GATEiit

GATE CSE 2018 Q50: Placing empty/full semaphores in a producer-consumer solution

Syllabus topic: Inter-process communication, concurrency and synchronization

Question

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:
do{
     wait(P);
     wait(mutex);
     //Add item to buffer
     signal(mutex);
     signal(Q);
}while(1);

Consumer:
do{
     wait(R);
     wait(mutex);
     //Consume item from buffer
     signal(mutex);
     signal(S);
}while(1);

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

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2018 final answer key)

Concept used: In the bounded-buffer solution, each side waits on the semaphore counting what it needs (free slots for the producer, filled slots for the consumer) and signals the semaphore the other side waits on.

Concept notes

Loading notes…

First step: Note the unusual naming here: full (initially N) counts free slots, so the producer must wait on it; then decide what the producer should signal for the consumer.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Inter-process communication, concurrency and synchronization

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key