Example of producer consumer - semaphore, Operating System

Now let's implement bounded producer-consumer with semaphores, a Coke machine which is initially empty:

semaphore sem_mutex = 1 // binary semaphore for mutual exclusion, 1==unlocked

semaphore slots_left = N
semaphore cokes_left = 0
producer()
down(slots_left)
down(sem_mutex)
insert Coke into machine
up(cokes_left)
up(sem_mutex)
consumer()
down(cokes_left)
down(sem_mutex)
buy Coke
up(slots_left)
up(sem_mutex)

Note that the down(slots left) needs to go before the down(sem mutex), and similarly for down(cokes left) and down(sem mutex), because otherwise the system can end up waiting forever. For example, imagine an empty machine with the consumer arriving ?rst, and calling down(sem mutex) before down(cokes left).

In some sense, the consumer calling down(cokes left) is a reservation to get a Coke in the future. Even if there are no Cokes currently in the machine, the consumer is reserving a claim for a future Cokewith this action. Similarly, the producer calling down(slots left) is a reservation to insert a new Coke in the future.

Posted Date: 3/12/2013 5:24:48 AM | Location : United States







Related Discussions:- Example of producer consumer - semaphore, Assignment Help, Ask Question on Example of producer consumer - semaphore, Get Answer, Expert's Help, Example of producer consumer - semaphore Discussions

Write discussion on Example of producer consumer - semaphore
Your posts are moderated
Related Questions
Ask using a laballed diagram explaining the architecture of windows operating ststem

How a virtual memory can be implemented? What are the benefits of using virtual memory?

Q. Can a system detect that some of its processes are starving? If you answer "yes," explain how it can. If you answer "no," explain how the system can deal with the starvation pro

Question: a) Define the term ‘Operating System' and identify two goals of an operating system. b) What do you understand by Symmetric multiprocessing (SMP) and Asymmetr

Explain the Resource Ownership and Control Operating systems are required to handle many different types of resources. CPU , memory, and I/O devices  are some of the varied sys


Q. Presume that a distributed system is susceptible to server failure. What mechanisms would be needed to guarantee the exactly once semantics for execution of RPCs? Ans

Sharing segments between processes without requiring the same segment number is possible in a dynamically linked segmentation system. a. Define a system that permits sta


We will brie?y introduce paging to ?nish off this lecture. When a process is loaded, not all of the pages are immediately loaded, since it's possible that they will not all be need