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

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...
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))=?
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...
Solve the LP problem. If no optimal solution exists because there is no Solution Set, enter...
Solve the LP problem. If no optimal solution exists because there is no Solution Set, enter EMPTY. If no optimal solution exists because the region is unbounded, enter UNBOUNDED. Note that an unbounded region can still have an optimal solution while a bounded region is guaranteed to have optimal solutions. HINT [See Example 1.] Maximize and minimize p = x + 2y subject to x + y ≥ 4 x + y ≤ 10 x − y ≤ 4 x...
Solve the LP problem. If no optimal solution exists because there is no Solution Set, enter...
Solve the LP problem. If no optimal solution exists because there is no Solution Set, enter EMPTY. If no optimal solution exists because the region is unbounded, enter UNBOUNDED. Note that an unbounded region can still have an optimal solution while a bounded region is guaranteed to have optimal solutions. HINT [See Example 1.] Minimize c = 2x − 2y subject to x 6 ≤ y y ≤ 2x 3 x + y ≥ 10 x + 2y ≤ 35...
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
Are the following subshell designations correct? Answer with "yes" or "no".
Are the following subshell designations correct? Answer with "yes" or "no". a. 3p b. 1s C. 2d d. 4s e. 2p f. 5f
Please turn in the solution for this problem. (No need to solve the LP) The MSS...
Please turn in the solution for this problem. (No need to solve the LP) The MSS Company, which manufacturers a new instant salad machine, has $350,000 to spend on advertising. The product is only to be test marketed initially in the Dallas area. The money is to be spent on a television advertising campaign for the Super Bowl weekend (Friday, Saturday, and Sunday.) The company has three options of advertisement available: daytime advertising, evening news advertising, and the Super Bowl....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT