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
What is preallocation? Why do it? Allocating space for a file before making the file to allow for expansion. This reserves space for a certain file so that other files can't gr

Explain about disk scheduling with neat diagram? FCFS Scheduling SCAN scheduling C-SCAN scheduling SSTF scheduling LOOK Scheduling

#include #include #include #include #include int main(void) { int pchan[2], pid, rval, cvar; char buf[20]; if (pipe(pchan) == -1){

Determine what the “blocking factor” of a file is Blocking factor is the number of logical records in one physical record

Deadlock Prevention Prevention is the name given to method that guarantee that deadlocks can never happen for the reason that the way the system is structured. Since 4 conditio

Define Server Processing with Queues The efficient server processing of client request is essential for optimized client- server applications. This process is greatly enhanced

How does communication takes place in a distributed system? Distributed systems depend on the networking for their functionality. A network is a communication path among two

Q. A CPU scheduling algorithm determined an order for the execution of its scheduled processes. Convinced n processes to be scheduled on one processor how numerous possible differ

What are the differences between process and thread? The fundamental difference between a process and a thread is that a process has an entire copy of the program to itself and

Discuss the high barriers to entry in the market for PL operating systems