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
Define the “Throughput” of a system  Throughput is Number of programs processed by it per unit time

Q. Describe the concept of transaction atomicity? Answer: A transaction is a sequence of read as well as writes operations upon some data followed by a commit operation. If the

Producer-Consumer Problem Using Semaphores The Solution to producer-consumer problem use three semaphores namely- full, empty and mutex. The semaphore 'full' is utilized for

how the system call is work?

Optimal page replacement An optimal page replacement algorithm has the least page fault rate of all algorithms. The algorithm states that a 'replace the page that will not be

Q. What kinds of networking does Windows XP support? How does Windows XP execute transport protocols? Describe two networking protocols. Answer: Support is offer for bo

Discuss difference between symmetric and asymmetric multiprocessing  Symmetric multiprocessing (SMP), in which every processor runs an identical copy of the operating system an

Process States A total of nine states are acknowledged by the UNIX system. They are like as follow: User running                                                   Executing

explain about design issues of network operating system

Objectives 1.    Login to UNIX system and logout 2.    Learn features of the vi editor and to use online help (main pages).  Gain experience using them to create a document