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 UFD and MFD. In the two-level directory structure, every user has her own user file directory (UFD). Every UFD has a similar structure, but lists only the files of a one

Explain about file allocation methods? The main problem in direct-access nature is how to assign space to these files so that disk space if utilized effectively and files can b

Question : (a) In the demand paging system, when a process tries to access a page that was not brought into memory, a page fault occurs that involves a series of actions. The

Your CTO liked your proposal from Unit 2 so much he only trusts you to explain complex concepts to his managers. He now wants you to write him a four page paper explaining the CPU,

What are the typical elements of a process image? User data: Modifiable part of user space. May have program data, user stack area, and programs that might be modified. User

Briefly explain about file system implementation? Several on-disk and in-memory structures are used to execute a file system. On disk, the file system may have information abou

Q. Explain about Spooling? Acronym for simultaneous peripheral operations on line. Spooling denotes to putting jobs in a buffer, a special area in memory or on a disk where

What are the services provided by the operating systems?  Program Development It provides a variety of services and facilities, such as debuggers and editor, to help the

What is Single-level Directory All files are constrained in the similar directory, which is simple to support and understood. One limitation is when the number of files enhance

Explain the Network protocols There are a wide variety of network protocols to choose from traditionally client - server developers were required to choose a communications pro