Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The producer-consumer problem can be extended to a system with multiple producers and consumers that write (or read) to (from) one shared buffer. Assume that
The producer-consumer problem can be extended to a system with multiple producers and consumers that write (or read) to (from) one shared buffer. Assume that each pro- ducer and consumer runs in its own thread. Will the solution presented in Fig. 2-28, using semaphores, work for this system?
#define N 100 typedef int semaphore; semaphore mutex = 1; semaphore empty N; semaphore full 0; / number of slots in the buffer / /* semaphores are a special kind of int*/ /*controls access to critical region/ /*counts empty buffer slots*/ /*counts full buffer slots/ void producer(void) int item; while (TRUE) item produce_item(); down(&empty); down(&mutex); insert item(item); up(&mutex); up(&full); TRUE is the constant 1/ /* generate something to put in buffer*/ /* decrement empty count*/ /* enter critical region */ /* put new item in buffer / /* leave critical region/ /increment count of full slots void consumer(void) int item; while (TRUE) down(&full); down(&mutex); item remove_item(); up(&mutex); up(&empty,); consume item(item); /*infinite loop */ /*decrement full count/ /*enter critical region*/ /take item from buffer /* leave critical region */ /* increment count of empty slots*/ /* do something with the item */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