Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
117 views
in Computer by (108k points)
closed by

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.

1 Answer

0 votes
by (101k points)
selected by
 
Best answer
Correct Answer - Option 3 :

Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.


Explanation:

semaphore n = 0;

semaphore s = 1;

 

void producer()

{

        1   while(true)

        2        {

        3     produce();

        4     semWait(s);

        5    addToBuffer();

        6    semSignal(s);

        7    semSignal(n);

}

}

 

 

 

void consumer()

{

      1     while(true)

      2     {

      3     semWait(s);

      4     semWait(n);

      5    removeFromBuffer();

      6     semSignal(s);

      7     consume();

             }

}

 

Method 1

  • If consumer executes first and execute line number 1, 2, 3 and now after 3 it has been pre-empted and now producer start executing line number 1, 2 , 3 after line 3 producer will go into waiting because s is 0 it is decremented by consumer earlier, now we pre-empted producer.
  • Now again consumer start executing from line 4 but it cannot execute further because n=0 and it will for n to become 1, now we pre-empted consumer process.
  • Now we can see that both are waiting for each other, producer it waiting to become s=1 from

Consumer and consumer is waiting to become n=1 from producer.

  • This is nothing but deadlock.


Method 2:

Consumer: 1, 2 3 |producer 1,2, 3,4 (waiting for s to become 1)| consumer : 4 (waiting for n to become 1 )  So both are in waiting state so deadlock .

Hence option 3 is the correct answer.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...