Milk Problem

23 views
Skip to first unread message

Partha

unread,
Mar 25, 2012, 8:56:12 PM3/25/12
to asu-cse-430
A synchronization problem called "too much milk problem is as follows"

Several people share a fridge, and the fridge contains milk (and other
things).
One person wants to drink milk.
-- check fridge, and there is no milk.
-- goes to shop to get milk
-- puts milk in fridge.

While the person was getting milk anotehr person checkd, found no milk
and went shopping. Hence the second person (or maybe third and fourth
too) buy milk, resulting in too much milk. What is the code for a good
solution to the milk problem.

An attempt is:

P(Faccess);
if(milk==0 && Leave ==0)
{
Leave = 1; //Leave for store
}
V(Faccess);

Buy milk

P(Faccess);
Milk=1;
Leave=0;
V(Faccess);


The above solution is almost correct, but the person who found no milk
seems to also go to store.

The solution that seems to be deemed the good one, as per Internet,
is:

P(mutex)
Look inside fridge;
If no milk go to supermarket and put milk in fridge
Take milk
V(mutex)

Drink milk and do other things


However, this means that while a person goes to get milk, others may
not look inside fridge for milk. So how would we modify the above
problem such that:
1. The first person to discover there is no milk, goes to supermarket.
2. Others who discover no milk, do not go to supermarket, but continue
without milk.

Post your solutions.

Reply all
Reply to author
Forward
0 new messages