Skip to content
Official PYQ · 2018

GATE CS 2018 Q40 — Concurrency and Synchronization

Explain with AI

Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is NN. Three semaphores emptyempty, fullfull and mutexmutex are defined with respective initial values of 0,N0, N and 11. Semaphore emptyempty denotes the number of available slots in the buffer, for the consumer to read from. Semaphore fullfull denotes the number of available slots in the buffer, for the producer to write to. The placeholder variables, denoted by PP, QQ, RR and SS, in the code below can be assigned either emptyempty or fullfull. The valid semaphore operations are: wait()wait() and sigmal()sigmal().
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);\begin{array}{|l|l|}\hline \text{Producer:} & \text{Consumer:} \\ \hline \text{do \{} & \text{do \{} \\ \quad\text{ wait (P);} & \quad\text{ wait (R);} \\ \quad\text{ wait (mutex);} & \quad\text{ wait (mutex);} \\ \quad \text{ //Add item to buffer} & \quad \text{ //consume item from buffer} \\ \quad\text{ signal (mutex);} & \quad\text{ signal (mutex);} \\ \quad \text{ signal (Q);} & \quad\text{ signal (S);} \\ \text{\}while (1); }& \text{\}while (1);} \\ \hline \end{array}
Which one of the following assignments tp PP, QQ, RR and SS will yield the correct solution?

Choose one option
View worked solution

Answer: C

To solve this problem, we must carefully read the non-standard definitions of the semaphores given in the question:

  • empty: Initialized to 00. It denotes the number of available slots in the buffer for the consumer to read from (i.e., the number of filled slots containing items).
  • full: Initialized to NN. It denotes the number of available slots in the buffer for the producer to write to (i.e., the number of empty slots).

1. Producer Analysis

  • Before adding an item to the buffer, the producer must check if there is an empty slot available to write. This corresponds to waiting on the semaphore that tracks write slots: wait(P)  ⟹  P=full\text{wait}(P) \implies P = \text{full}
  • After adding the item, the producer must signal that a new item is now available for the consumer to read: signal(Q)  ⟹  Q=empty\text{signal}(Q) \implies Q = \text{empty}

2. Consumer Analysis

  • Before consuming an item, the consumer must check if there is at least one filled slot available to read. This corresponds to waiting on the semaphore that tracks read slots: wait(R)  ⟹  R=empty\text{wait}(R) \implies R = \text{empty}
  • After consuming the item, the consumer must signal that a slot has been freed up, making it available for the producer to write: signal(S)  ⟹  S=full\text{signal}(S) \implies S = \text{full}

Conclusion

Combining these assignments, we get:

P=fullQ=emptyR=emptyS=full\begin{aligned} P &= \text{full} \\ Q &= \text{empty} \\ R &= \text{empty} \\ S &= \text{full} \end{aligned}

This matches Option C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions