Process P1 repeatedly adds one item at a time to a buffer of size n, and…

2004

Process P1 repeatedly adds one item at a time to a buffer of size n, and process P2 repeatedly removes one item at a time from the same buffer using the programs given below. In the programs, K, L, M, and N are unspecified statements.

P1​(i)

P2​(j)

while(T) {

while(T) {

K;

M;

P(mutex);

P(mutex);

Add an item to the buffer;

Remove an item to the buffer;

V(mutex);

V(mutex);

L;

N;

}

}

The statements K, L, M, and N are respectively:

  1. A.

    P(full), V(empty), P(full), V(empty)

  2. B.

    P(full), V(empty), P(empty), V(full)

  3. C.

    P(empty), V(full), P(empty), V(full)

  4. D.

    P(empty), V(full), P(full), V(empty)

Attempted by 54 students.

Show answer & explanation

Correct answer: D

This problem describes the classic Producer-Consumer synchronization scenario using semaphores. Process P1 must wait for an empty slot via P(empty) before adding data and signal availability with V(full). Process P2 must wait for an item via P(full) before removing data and signal a free slot with V(empty). The mutex variable ensures mutual exclusion during critical section access.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir