Example of producer consumer - semaphore, Operating System

Assignment Help:

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.


Related Discussions:- Example of producer consumer - semaphore

Explain kernel activity for ults, KERNEL ACTIVITY FOR ULTS: The kernel...

KERNEL ACTIVITY FOR ULTS: The kernel isn't aware of thread activity but it is still managing process activity. When a thread compose a system call the whole process will be bl

Define the sleep (sec) function, Define the Sleep (sec) Function Sleep ...

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 are the methods for handling deadlocks, What are the methods for handl...

What are the methods for handling deadlocks? The deadlock problem can be dealt with in one of the three ways: a. Use a protocol to prevent or avoid deadlocks, ensuring that

Explain two level directory, Two Level Directory This kind of structure...

Two Level Directory This kind of structure overcomes the problems of assigning unique names to the files. Thus there need not be any confusion among users. In this kind of s

Student, Explain how the os implements virtual memory phenomenon.

Explain how the os implements virtual memory phenomenon.

Explain page-replacement algorithms, Q. Consider the following page-replac...

Q. Consider the following page-replacement algorithms. Rank the algorithms on a five-point scale from "bad" to "perfect" according to their page-fault rate. Detach those algorithm

Write in brief on unix file structure, Write in brief on UNIX file structur...

Write in brief on UNIX file structure. UNIX considers every file to be a sequence of 8-bit bytes no interpretation of these bits is made by the operating system. This system pr

Explain the resource ownership by platform, Explain the Resource Ownership ...

Explain the Resource Ownership by Platform Ownership of data items may further be complicated by concepts defined in certain operating systems. Each operating system defines "p

Computer organisation, basic advantage of using interrupt initiated data tr...

basic advantage of using interrupt initiated data transfer over transfer under program control without an interrupt

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd