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

Question 1: a) Briefly explain each of the following terms: (i) Distributed systems. (ii) Real time systems. (iii) Multiprocessor. b) What is the impo

Gang Scheduling : A set of related process is scheduled to execute on a set of processors at the similar time, on a 1-to-1 basis. Closely related processes or threads may be sched

Present your own fully documented and tested programming example illustrating the prevention of a data race in a parallelised program. This is an example where total number of p

Identifying parallelism A formal way to identify parallelism in an activity is to draw a task dependence graph in a directed graph in which each vertex represents a task to

List the task regions in the Spiral model.  Customer communication - In this region it is suggested to set up customer communication.  Planning - All planning activities

What are the benefits of multiprogramming? Responsiveness : Multithreading is an interactive application may permit a program to continue running even if part of it is block

Define LRU Page Replacement Algorithm LRU policy: LRU expands to least currently use. This policy suggests which we re- move a page whose last usage is farthest from current ti

Question 1 What is Micro-kernel? What are the benefits of Micro-kernel? Question 2 Explain seven state process models used for OS with necessary diagram. Differentiate betwee