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
Problem 1. List out the conditions that result in Deadlock situations. Illustrate deadlock situation with a simple graphical notation Listing conditions for deadlock occu

Q. Once more considering the RPC mechanism consider the exactly once semantic. Does the algorithm for implementing this semantic implement correctly even if the ACK message back to

In this project, you will implement the Chandy and Misra's (CM) algorithm using POSIX Threads (Pthreads).   The algorithm  is a distributed algorithm to solve a generalized dining

Question 1: a) Distinguish between "Long-Term Scheduler" and "Short-Term Scheduler". b) Describe the benefits of "Threads". c) Name three types of CPU Scheduling. Que

why we say" OS is a resource allocator" and “OS is a control program"?

Q. Remapping of bad blocks by sector sparing or else sector slipping could influence performance. Presume that the drive in Subsequent Exercise has a total of 100 bad sectors at r

Compare 2 different operating systems.

What are scheduling queues? Since the processes enter the system they are put into job queue. This queue contains all processes in the system. The processes that are exist in m

Explain Load Balancing Client Server Components When migration functionality from the client - only model to the client - server model, care must be taken not over-or underutil

Define the Global vs. local variables It is also important to discuss the scope of resources being granted to the application program.  Variable scoping is defined as the acces