Log In
25 votes

The semaphore variables full, empty and mutex are initialized to $0$, $n$ and $1$, respectively. 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.

while (1) {
    Add an item to the buffer;
while (1) {
    Remove an item from the buffer;

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

  1. P(full), V(empty), P(full), V(empty)
  2. P(full), V(empty), P(empty), V(full)
  3. P(empty), V(full), P(empty), V(full)
  4. P(empty), V(full), P(full), V(empty)
in Operating System
edited by

SIr, I understood that option d is the solution. But, what if the question would have been full, empty and mutex are initialized to n, 0 and 1, then what would be the answer ?

@ Sukhbir Singh, according to you, initially buffer is full. So, FULL=n and EMPTY=0. Now, if process P1 want to execute then due to P(EMPTY), semaphore EMPTY value becomes "-1" so, process P1 got blocked until process P2 perform V(EMPTY). So, in this scenario only P2 process can execute first.

5 Answers

44 votes
Best answer
$P_1$ is the producer. So, it must wait for full condition. But semaphore $\text{ full }$ is initialized to 0 and semaphore $\text{ empty }$ is initialized to $n$, meaning $full = 0$ implies no item and $empty=n$ implies space for $n$ items is available. So, $P_1$ must wait for semaphore $\text{ empty }$ - $K - P( \text{ empty })$ and similarly $P_2$ must wait for semaphore $\text{ full }$- $M - P( \text{ full })$. After accessing the critical section (producing/consuming item) they do their respective $V$ operation. Thus option D.

selected by

Incase if the question would have been full, empty and mutex are initialized to n, 0 and 1, then Option B) is the answer ...

whether it is fixed that full is for producer and empty is for consumer ;
@adarsh_1997 based in same thing?
15 votes

Since P(Empty) at M then P2 remove an item from an Empty buffer, which fail the synchronisation problem that is P2 wants to remove the item which is not present inside the buffer. So option B and C are not the answer.

If P(Full) then P1 cant enter an item into the buffer, so option A is not the answer.

Hence option D ensures that P2 never remove an item from an empty buffer, while enter the item easily.

7 votes
Answer: D

It ensures that P2 never removes an item from an empty buffer.
3 votes

In any PRODUCER-CONSUMER problem this 4 criteria must satisfy

(1)When a reader is reading, no writer must be allowed. 
(2)Multiple readers should be allowed. 
(3)When a writer is writing, no reader must be allowed. 
(4)Multiple writers(more than 1) should not be allowed.

here 2 counting semaphores are used to keep track how much the buffer if empty or full they are b_full=0 ( means initially nothing in the buffer) & b_empty=100 (means initially empty cells in buffer is 100 or buffer is all empty)

now for K - Producer produced an item & he wants to insert into buffer but reader should not read meanwhile producer adding item to buffer for that (P(mutex)) now it downs the semaphore P(b_empty) means now buffer_empty=99 or there is 1 item inserted.

L - after inserting, it ups the semaphore V(b_full) means Buffer_full=1 or there is 1 item present in buffer.

now for M - it downs the "P(b_full)" semaphore means it has taken out 1 item from buffer and consumes item and

N - up the "V(b_empty)" to state that one more cell of buffer is empty now.

Nice Explanation

2 votes
it shud be d)

Related questions

17 votes
1 answer
In a virtual memory system, size of the virtual address is $32$-bit, size of the physical address is $30$-bit, page size is $4$ Kbyte and size of each page table entry is $32$-bit. The main memory is byte addressable. Which one of the following is the maximum number of bits that can be used for storing protection and other information in each page table entry? $2$ $10$ $12$ $14$
asked Nov 2, 2014 in Operating System Ishrat Jahan 3.9k views
22 votes
4 answers
A disk has $200$ tracks (numbered $0$ through $199$). At a given time, it was servicing the request of reading data from track $120$, and at the previous request, service was for track $90$ ... (Shortest Seek Time First) and FCFS (First Come First Serve)? $2$ and $3$ $3$ and $3$ $3$ and $4$ $4$ and $4$
asked Nov 2, 2014 in Operating System Ishrat Jahan 3.9k views
28 votes
5 answers
A process executes the following segment of code : for(i = 1; i <= n; i++) fork (); The number of new processes created is $n$ $((n(n + 1))/2)$ $2^n - 1$ $3^n - 1$
asked Nov 2, 2014 in Operating System Ishrat Jahan 4.2k views