IAE Academy

GATE 2018 CS Q.40: solution and answer

GATE 2018 · CSQ.40 · 2 marks · MCQ

Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is . Three semaphores , and mutex are defined with respective initial values of 0, and 1. Semaphore denotes the number of available slots in the buffer, for the consumer to read from. Semaphore 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 or . 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?

  1. (A)

    P: full, Q: full, R: empty, S: empty

  2. (B)

    P: empty, Q: empty, R: full, S: full

  3. (C)

    P: full, Q: empty, R: empty, S: full

  4. (D)

    P: empty, Q: full, R: full, S: empty

See the question in the paper

Answer and solution

ANSWER

The correct assignment is P: full, Q: empty, R: empty, S: full, which is option (C).

Official answer: CSource: Official answer key, GATE 2018 CS (IIT Guwahati)

  1. Read the meanings from the question: (initial value ) counts the slots the producer may write into, and (initial value ) counts the filled slots the consumer may read from. mutex (initial value 1) gives mutual exclusion on the buffer.

  2. The producer must first wait for a writable slot, so it waits on the semaphore that counts writable slots: P .

  3. After adding an item, the producer makes one more item available for reading, so it signals the semaphore that counts readable items: Q .

  4. The consumer must first wait for an item to read, so it waits on the readable-items semaphore: R .

  5. After consuming, the consumer frees a slot for the producer, so it signals the writable-slots semaphore: S .

  6. Check with the initial values. makes the consumer block while the buffer is empty. lets the producer write items and then block when the buffer is full.

  7. Check the other options. In (A) the producer both waits on and signals , so nothing ever signals and the consumer blocks forever on . In (B) the producer waits on and blocks, while the consumer waits on and reads items from an empty buffer. In (D) the producer waits on and blocks, while the consumer waits on and reads from an empty buffer. So only (C) is correct: P: , Q: , R: , S: .

KEY IDEA

Each side waits on the semaphore that counts what it needs (producer: free slots; consumer: filled slots) and signals the semaphore that counts what it has just created for the other side.

WATCH OUT FOR

Reading the names literally and taking to be the producer's semaphore. Here the question defines as the count of filled slots for the consumer and as the count of free slots for the producer, so the names are swapped from the usual textbook version.

From the GATE 2018 CS paper. Every question of this paper · Download the full paper · All 7 Synchronisation questions, with notes

More Synchronisation questions