Answered step by step
Verified Expert Solution
Link Copied!

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?image text in transcribed

#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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions

Question

How could the company have avoided this situation?

Answered: 1 week ago

Question

5. How many no responses did you make?_________?

Answered: 1 week ago