Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The bounded-buffer problems (aka the producer-consumer problem) is a classic example of concurrent access to a shared resource. A bounded buffer lets multiple producer threads
The bounded-buffer problems (aka the producer-consumer problem) is a classic example of concurrent access to a shared resource. A bounded buffer lets multiple producer threads and multiple consumer threads share a single shared buffer. Producers write data to the buffer by invoking put() method and consumers read data from the buffer by calling get() method. The following implementation for producer and consumer correctly works for a single producer and a single consumer thread for the bounded buffer of the size one integer variable. However, the implementation does not properly work for two producer threads and a single consumer thread. Suppose put() and get() methods are correctly implemented, 1. Explain why the following implementation is broken for more than one producer threads, and 2. Fix the problem to make the corrected implementation work for two producer threads and a single consumer thread. If you need, you can refer to P thread reference link. int buffer; int count =0; void put(int value) \{ count =1; buffer = value; \} int get() \{ count =0; return buffer; \} pthread_mutex_t m= PTHREAD_MUTEX_INITIALIZER; pthread_cond_t c= PTHREAD_COND_INITIALIZER; void *producer (void *arg) \{ int i; int loops = (int) arg; for (i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started