edited by
14,561 views
30 votes
30 votes

Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is $N$. Three semaphores $empty$, $full$ and $mutex$ are defined with respective initial values of $0, N$ and $1$. Semaphore $empty$ denotes the number of available slots in the buffer, for the consumer to read from. Semaphore $full$ 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 $empty$ or $full$. The valid semaphore operations are: $wait()$ and $sigmal()$.
$$\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 $P$, $Q$, $R$ and $S$ will yield the correct solution?

  1. $P: full,  \ \ \ Q:full, \ \ \ R:empty, \ \ \ S:empty$
  2. $P: empty, \ \ \ Q: empty, \ \ \ R:full, \ \ \ S:full$
  3. $P: full, \ \ \ Q:empty, \ \ \ R:empty, \ \ \ S:full$
  4. $P: empty, \ \ \ Q:full, \ \ \ R:full, \ \ \ S:empty$
edited by

6 Answers

Best answer
34 votes
34 votes
Empty denotes number of Filled slots.

Full number of empty slots.

So, Producer must dealing with Empty and Consumer deals with Full

Producer must checks Full i,e. decrease Full by $1$ before entering and Consumer check with Empty decrease Full by $1$ before entering

So, (C) must be answer.
edited by
21 votes
21 votes

Lets see according to the codes given,

full denotes the number of available slots in the buffer, for the producer to write to

and, empty denotes the number of available slots in the buffer, for the consumer to read from. 

So, P must be full, as on wait(P), the available slots must decrease for the producer to write to, when it is producing an item to the buffer.

Q must be empty as signal(Q) increases the number of available slots in the buffer, for the consumer to read from.

R must be empty as on wait(R) decreases the no of avaliable slots in the buffer to read from (for the consumer)

S must be full as on signal(S) increases a slot for the producer to write, as the consumer has consumed an item from the buffer.

So ans is (C).

6 votes
6 votes
The producer should not produce if the buffer is full, (full) is initialized to N means the buffer is empty, and the consumer should not consume if the buffer is empty and empty is initialized to 0 means buffer is empty. So according to this C should be the right answer.

Firstly the producer is producing an item and then signals the consumer to consume it.
2 votes
2 votes

Semaphore “full‟ denotes the no. of available slots in the buffer for the producer to write to.

Semaphore “empty” denotes the no. of available slots in the Buffer for the consumer to read

From.

Producer: Producer first checks whether any free slots available in the buffer of not.

Hence, wait (P) wait (full)

The producer should not produce if the buffer is full, (full) is initialized to N.

After producing an item, the item is added to buffer, hence at the end producer increases number of

Produced items.

Hence, Signal (Q)  signal (empty)

Consumer: Before the consumer should take an item from buffer, it should check how many items

are present on the buffer for consumption and it should be greater than 0.

Hence, wait (R)wait (empty)

After consuming an item, on free slot is increased on buffer.

Hence, signal (S)  Signal (full)

Hence ,option (C) is correct

Answer:

Related questions