| Producer: | Consumer: |
|---|---|
| do{ wait(P); wait(mutex); //Add item to buffer signal(mutex); signal(Q); }while(1); | 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?
GATE CS - 2018,
Answer: C
The correct assignment is P: full, Q: empty, R: empty, S: full.
Note the unusual naming in this question: full counts the free slots (it starts at N) and empty counts the filled slots (it starts at 0). The producer must wait for a free slot, so P is full, and after adding an item it announces a filled slot, so Q is empty. The consumer waits for a filled slot, so R is empty, and after removing an item it frees a slot, so S is full.
In option B and option D the producer waits on empty, which is 0 at the start, so it blocks before producing anything; in D the consumer also waits on full and could consume from an empty buffer. In option A the producer signals full after adding, creating free slots that do not exist, while the consumer waits forever on empty.
Please log in or register to report this question.