edited by
160 views
4 votes
4 votes

Consider the following Producer-Consumer implementation using a buffer of size $N.$ Assume that initially the buffer is empty.

Semaphore mutex = __; 
Semaphore empty = __; 
Semaphore full = __;

 

producer {
  while (1) {
    Produce new resource;
    wait(empty);
    wait(mutex); 
    Add resource to an empty buffer;
    signal(mutex); 
    signal(full); 
  }
}
consumer {
  while (1) {
    wait(full);
    wait(mutex); 
    Remove resource from a full buffer;
    signal(mutex); 
    signal(empty);
    Consume resource;
  }
}


For the correct working of the above code the initialization of the semaphores mutex, empty and full must be as

  1. $\text{mutex = 0, empty = N, full = 1}$
  2. $\text{mutex = 1, empty = 1, full = N}$
  3. $\text{mutex = 1, empty = N, full = 0}$
  4. $\text{mutex = 0, empty = N, full = N}$
edited by

1 Answer

Best answer
1 votes
1 votes
Both producer and consumer are waiting on mutex. So, unless it is $1,$ neither can progress. So, $\text{mutex = 1.}$
Producer is waiting for empty and consumer is waiting for full. A producer can produce until buffer is full and a consumer can consume until buffer is not empty. Initially buffer being empty, this means full must be $0$ and empty must be $N.$ Option C.
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1