Question
The following is the complete Bounded Buffer program for multiple producers and consumers based on the semaphores. Rewrite the program to solve the bounded-buffer problem
The following is the complete Bounded Buffer program for multiple producers and consumers based on the semaphores. Rewrite the program to solve the bounded-buffer problem with a monitor (condition variables). Reminder: the bounded buffer problem is to allow concurrent access to the Buffer by producers and consumers, while ensuring that * The shared Buffer data structure is not screwed up by race conditions in accessing it. * Consumers don't try to remove objects from Buffer when it is empty. * Producers don't try to add objects to the Buffer when it is full.
typeT buf[n]; // an array of some type int front = 0; int rear = 0; semaphore empty = n, full = 0, mutex = 1; void produce(typeT data) { P(empty) P(mutex); buf[rear] = data; rear = (rear + 1) % n; V(mutex); V(full); } void consume(typeT &result) { P(full); P(mutex); result = buf[front]; front = (front + 1) % n; V(mutex); V(empty); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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