Anonymous user menu

Consider the procedure below for : GATE 2014 - Paper 2

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

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

Which one of the following is TRUE?

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

Answer

(A) The producer will be able to add an item to the buffer, but the consumer can never consume if given statement is false, because once producer produces an item and places in buffer, the next turn to execute can be given to consumer function. [The value of s = 1 and n = 1]. So, consumer will definitely be able to consume it by performing successful down operations on s and n.

(B) The consumer will remove no more than one item from the buffer. Given statement is false as if producer produces and adds to buffer, consumer will consume the added item, this sequence of alteration (producer and consumer) will always make consumer to remove items from buffer. This statement would have been true if it was said that the consumer will remove no more than one item from the buffer one after the other (at a time).

(C) Dead lock occurs if the consumer succeeds in acquiring semaphore(s) when the buffer is empty. Given statement is true as when buffer is empty initially if consumer gets the turn to execute as follows:

s = 1, n = 0;
consumer()
{
    while(True)
    {
        semWait(s); [s = 1 -> 0]
        semWait(n); [n = 0; it blocks consumer]
    }
}
producer()
{
    while(True)
    {
        produce();
        semWait(s); [s = 0; it blocks producer]
    }
}

So, from the above execution both producer and consumer goes in block state waiting for each other to wake them up and hence dead lock occurs.

(D) Even if the starting value for the semaphore ‘n’ becomes 1, there will be invalid execution of consumer on buffer empty  condition, which should not happen. So, statement is false.

So, the correct answer is option (C).

0Comment