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
Question: a) What special measures must be taken to ensure the reliability, availability, and security of electronic commerce and digital business process? b) What are the c

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

Define the Sleep (sec) Function Sleep function suspends execution of this process for sec number of seconds. While this function is used in our example for consistency, other f

What is page fault and what are the steps taken by the OS to remove page fault? A page fault is caused while a page that hasn't been brought into memory is being addressed. The

Define throughput? Throughput in CPU scheduling is the number of processes that are completed per unit time. For long processes, this rate might be one process per hour; for sh

Q. Researchers have recommended that instead of having an access list associated with each file (specifying which users can access the file and how) we should have a user control

What is turnaround time? Turnaround time is the interval from the time of submission to the time of completion of a process. It is the sum of the periods exhausted waiting to g

Define Virtual memory Virtual memory is employed in all major commercial operating systems

Q. Explain the File-system manipulation? File-system manipulation there is several details in file allocation, creation, deletion and naming that users should not have to perfo

Purpose This is intended to help you develop your understanding of shell scripting in both a Windows and Unix environment. Deliverables For both the first and second op