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
Resolution of externally defined symbols is carried out by    Resolution of externally defined symbols is carried out by Linker

Q.  To build a vigorous distributed system you must know what kinds of failures can occur. a. List three possible kinds of failure in a distributed system. b. State which of

Processes and threads each have their place in multi-programming, generally to hide latency and to maximize CPU utilization. With the continuing spread of multi-core processors in

The optimal replacement policy, called OPT, is to evict the page which will be accessed farthest into the future. Since we can't predict the future precisely, we can't implement OP

Q. Explain some of the ways an application can use memory via the Win32 API. Answer: (1) Virtual memory offers several functions that allow an application to reserve and rele

what is the use of valid invalid bits in paging

SEGMENTATION In a memory management system that is properly a programmer views a program to be collection of program components called as segments . Every segment is a logical

Ask question Project Details PROJECT OVERVIEW The ABC Hospital is upgrading their version of Windows. As part of the upgrade, 100 new computers will be purchased. Your task is to

Explain Processing Predefined Application Protocols Implementing and swift processing of predefined application protocols is sure to improve performance of server - side applic

TII stands for The abbreviation of TII is Table of incomplete instructions.