Menu

Question Critical Section Condition Variable Mutex Lock Producer Counter Q43788646

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 produ

(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++; } Show transcribed image text (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++; }

Expert Answer


Answer to Question about critical section (condition variable, mutex lock) in producer and counter…

OR