Question

In: Computer Science

Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes,...

Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes, what is achieved and how? If not, what is the problem? Explain your answer in either case. (Note: Assume no syntax or compilation errors. You’re asked about the concurrency logic/correctness and the use of semaphores for this.)

#define N 100                 /* number of slots in the buffer */
semaphore mutex = 1;          /* controls access to critical */
semaphore empty = N;          /* counts empty buffer slots */
semaphore full = 0;          /* counts full buffer slots */


void producer(void)
{
   int item;
   while (TRUE){
              item = produce_item();   /* generate something to put in buffer */
              wait(&mutex);            /* enter critical region */
              wait(&empty);            /* decrement empty count */
              insert_item(item);       /* put new item in buffer */
              signal(&mutex);          /* leave critical region */
              signal(&full);           /* increment count of full slots
              }
}


void consumer(void)
{
int item;
while (TRUE){
              wait(&mutex);           /* enter critical region */
              wait(&full);            /* decrement full count */
              item = remove_item();   /* take item from buffer */
              signal(&empty);         /* increment count of empty slots */
              signal(&mutex);         /* leave critical region */
              consume_item(item);     /* do something with the item */
             }
}

Solutions

Expert Solution

This implementation can cause a deadlock. Here is how:
At time 0, empty is N, full is 0. Lets assume consumer is able to get the mutex and enter the critical section first - this is likely since the producer has to produce the first item. The sequence of events is:
wait (&mutex) // the consumer waits on the mutex and gets in to the critical section
wait (&full) // the consumer reaches here and is blocked since full is 0, the producer has not produced anything yet
...
After a little while, the producer finishes producing and executes:
wait (&mutex) // since the consumer is in the critical section, the producer has to wait.

This will result in both waiting indefinitely, causing a deadlock. The consumer is waiting for an item to consume and the producer is waiting to get into the critical section, neither can move forward.

In order to correct this, use of only one semaphone (the mutex) is adequate; update of shared variables (such as full and empty) is inside the critical section and therefore it is guaranteed that only one process will be accessing and / or updating these.


Related Solutions

Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in the lecture notes. The solution presented in the notes uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this assignment, standard counting semaphores will be...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to a shared buffer and a consumer process consumes items from that buffer (in the first-in-first-out (FIFO) order). When a producer adds a new item, the buffer size grows by one, and whenever a consumer consumes an item from the buffer, the size is reduced by one. The producer process must wait when the buffer is full likewise the consumer process must wait when the...
Can you please solve this problem. The correct answer that should be found is below. Thank...
Can you please solve this problem. The correct answer that should be found is below. Thank You Early in 2015, Logan Corporation engaged Reese, Inc. to design and construct a complete modernization of Logan's manufacturing facility. Construction was begun on January 1, 2015 and was completed on December 31, 2015. Logan made the following payments to Reese, Inc. during 2015: Date Payment June 1, 2015 $2,400,000 August 31, 2015 3,600,000 December 31, 2015 3,000,000 In order to help finance the...
What is the problem of evil? In what way does Augustine attempt to solve it?
What is the problem of evil? In what way does Augustine attempt to solve it?
Solve for​ Y(s), the Laplace transform of the solution​ y(t) to the initial value problem below....
Solve for​ Y(s), the Laplace transform of the solution​ y(t) to the initial value problem below. y'''+7y''+4y'-12y= -24, y(0) = 11, y'(0)= 5, y''(0) = -43
Solve for​ Y(s), the Laplace transform of the solution​ y(t) to the initial value problem below....
Solve for​ Y(s), the Laplace transform of the solution​ y(t) to the initial value problem below. y"-6y'+9y=cos 2t- sin 2t , y(0)=6, y'(0)=3
I am down to one last attempt. Please make sure both are correct. 1) Solve the...
I am down to one last attempt. Please make sure both are correct. 1) Solve the given system of differential equations by systematic elimination. (D + 1)x + (D − 1)y = 2 3x + (D + 2)y = −1 (x(t),y(t))=? 2) Solve the given system of differential equations by systematic elimination. D2x − Dy = t (D + 6)x + (D + 6)y = 5 (x(t),y(t))=?
. Is the following software solution to the critical section problem correct?                      {          &nbsp
. Is the following software solution to the critical section problem correct?                      {              int   lock = 0;                ………….                if (lock == 0) then lock = 1                 else                         while (lock == 1) no-op;                   enter_CS;                          CS                    exit_CS;                 ……….             }
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below. This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer...
2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus...
2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus and total surplus change when the minimum wage is removed. Assume the minimum wage is above the free market price. In your explanation please interpret the components of the changes in consumer surplus, producer surplus and total surplus; i.e. what each component represents. For additional points, what happens if the minimum wage is set below the free market price? please graph
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT