Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question about critical section (condition variable, mutex lock) in producer and counter (8pt) As taught in the class, consumer and producer is a classic synchronization
Question about critical section (condition variable, mutex lock) in producer and counter
(8pt) As taught in the class, consumer and producer is a classic synchronization problem where a consumer process and a producer process communicate through a bounded shared memory buffer. Modify the code using mutex lock and condition variable to ensure the correctness of the code, and let the producer to sleep when the buffer is full, then wake-up when it can resume writing items to the buffer. Similarly, the consumer will sleep when the buffer is mpty, and wake-up when it can resume reading items from the buffer. Use data type cond_var to declare a condition variable, and use signal() and cond.wait) as the methods to operate a condition variable. Use data type mutex_var to declare a mutex lock variable, and use lock() and unloek() as the methods to operate a mutex lock variable. You must indicate the variable are declared as a local variable in the producer/consumer function or as a global variable. void producer() { while (1) { void consumer() { while (1) { while (counter =0); item = buffer[out]; nextitem = getltem(); while (counter=BUFFER_SIZE); buffer[in] = nextItem; in = (in + 1) % BUFFER_SIZE; out = (out + 1) % BUFFER_SIZE; counter-; counter++; }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