in Operating System edited by
14,387 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$
in Operating System edited by
by
14.4k views

4 Comments

Yes , In my question paper too, it was 36
1
1
Empty= 0 and Full =N means here buffer is empty ? ,Because empty means no of slots in buffer for consumer to read from ,here it is 0 means buffer will be empty ? Can anyone confirm ?
0
0

6 Answers

34 votes
34 votes
Best answer
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

4 Comments

Consumer decreases empty by 1 before entering.

0
0

@Prashant.

what if initial value of empty,full are N,0

will it affect the answer?

0
0

@Devesh_Kumar

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.

empty=N means there are 'N' slots in the buffer, for the consumer to read from.

full=0 means there are 0 available slots in the buffer, for the producer to write to.

The answer is still the same. (producer must be blocked when buffer is completely filled i;e full=0 and consumer must be free to consume items when buffer is completely filled)

1
1
option B and D can straighaway be eliminated because if we take a lock in empty then producer will go in block state .
0
0
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).

2 Comments

Thanks it helps a lot..ur answer should be the final one
1
1
0
0
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