IAE Academy

ISRO CS 2023 Q.53: solution and answer

ISRO 2023 · CSQ.53 · 1 mark · MCQ

In a producer-consumer scenario also known as Bounded-Buffer Problem, what would be the most appropriate synchronization primitive to ensure that the consumer waits when the buffer is empty?

  1. (A)

    Spinlock

  2. (B)

    Mutex lock

  3. (C)

    Semaphore

  4. (D)

    Monitors

See the question in the paper

Answer and solution

ANSWER

The most appropriate primitive is a semaphore (option C).

Official answer: CSource: ISRO's revised and final answer key of 16 February 2024

  1. In the bounded-buffer problem the consumer must block (sleep) when there is nothing to consume, and be woken by the producer after it inserts an item. This is a signalling requirement between two threads, not only mutual exclusion.

  2. A counting semaphore full, initialised to 0, counts the filled slots. The consumer executes wait(full) before removing an item, so it blocks when the buffer is empty. The producer executes signal(full) after inserting an item, which wakes a waiting consumer.

  3. Symmetrically, a semaphore empty initialised to makes the producer wait when the buffer is full, and a binary semaphore (or mutex) protects the buffer itself during insertion and removal.

  4. Spinlock: gives mutual exclusion only, and a waiting thread busy-waits instead of sleeping. It has no mechanism to signal that an item has arrived, so it does not fit.

  5. Mutex lock: enforces mutual exclusion only and must be released by the thread that acquired it, so it cannot let the consumer wait for an action by the producer. It does not fit.

  6. Monitors: a monitor can solve the problem, but only together with condition variables. The primitive that by itself makes the consumer wait on an empty buffer and be woken by the producer is the semaphore, so the semaphore is the most appropriate choice.

KEY IDEA

A counting semaphore both counts filled slots and blocks or wakes threads, so wait(full) makes the consumer sleep on an empty buffer and signal(full) from the producer wakes it.

WATCH OUT FOR

Choosing a mutex lock because it is the familiar synchronization tool. A mutex only gives mutual exclusion and is released by its owner, so it cannot make one thread wait for another thread's action.

From the ISRO CS 2023 paper. Every question of this paper · The full paper (PDF, ISRO) · All 3 ISRO Synchronisation questions, with notes

More ISRO Synchronisation questions