edited by
13,593 views
50 votes
50 votes

Consider the procedure below for the Producer-Consumer problem which uses semaphores: 

semaphore n = 0; 
semaphore s = 1; 
void producer() 
{ 
    while(true) 
    { 
        produce(); 
        semWait(s); 
        addToBuffer(); 
        semSignal(s); 
        semSignal(n); 
    } 
} 
void consumer() 
{ 
    while(true) 
    { 
        semWait(s); 
        semWait(n); 
        removeFromBuffer(); 
        semSignal(s); 
        consume(); 
    } 
} 

Which one of the following is TRUE?

  1. The producer will be able to add an item to the buffer, but the consumer can never consume it.
  2. The consumer will remove no more than one item from the buffer.
  3. Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.
  4. The starting value for the semaphore $n$ must be $1$ and not $0$ for deadlock-free operation.
edited by

3 Answers

Best answer
90 votes
90 votes
  1. False : Producer $=P$ (let), consumer $= C$(let)  , once producer produce the item and put into the buffer. It will up the $s$ and $n$ to $1$, so consumer can easily consume the item. So, option (A) Is false.
    Code can be execute in this way: $P :1 \ 2 \ 3 \ 4 \ 5| \ C: 1 \ 2 \ 3 \ 4 \ 5$. So, consumer can consume item after adding the item to buffer.
     
  2. Is also False, because whenever item is added to buffer means after producing the item, consumer can consume the item or we can say remove the item, if here statement is like the consumer will remove no more than one item from the buffer just after the removing one then it will be true (due $n=0$ then, it will be blocked ) but here only asking about the consumer will remove no more than one item from the buffer so, its false.
     
  3. is true , statement says if consumer execute first means buffer is empty. Then execution will be like this.
    $C:1$ (wait on s, $s=0$ now) $2$$($BLOCK $n =-1$$)$ |P : $1$ $2$ (wait on $s$ which is already $0$ so, it now block). So, $c$ wants $n$  which is held by producer or we can say up by only producer and $P$ wants $s$, which will be up by only consumer. (circular wait ) surely there is deadlock
     
  4. is false,  if $n=1$ then, also it will not free from deadlock.
    For the given execution: $C: 1 \ 2 \ 3 \ 4 \ 5 \ 1 \ 2$(BLOCK) $| P: 1 \ 2$(BLOCK) so, deadlock.
    (here, $1 \ 2 \ 3 \ 4 \ 5$ are the lines of the given code)

Hence, answer is (C)

edited by
10 votes
10 votes
answer is C as when consumer  first acess the semaphore S it will down (s)  but for semaphore(n) , it has to wait for  producer to make it 1 but as for producer it can't acess the critical section b'coz semWait(s) is zero .
so it will cause deadlock
6 votes
6 votes
Initially, there is no element in the buffer.
Semaphore s = 1 and semaphore n = 0.
We assume that initially control goes to the consumer when buffer is empty.
semWait(s) decrements the value of semaphore ‘s’ . Now, s = 0 and semWait(n) decrements the value of semaphore ‘n’. Since, the value of semaphore ‘n’ becomes less than 0 , the control stucks in while loop of function semWait() and a deadlock arises.
 
Thus, deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.
Answer:

Related questions