Date: Mon, 11 Nov 1996 17:25:22 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Tue, 20 Feb 1996 13:25:27 GMT Content-length: 2251 CS 537 - Quiz #2
UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department
CS 537
Spring 1996
Bart Miller
Quiz #2
Wednesday, February 14

Too much of too much milk

Following is a solution to the N bottle Too Much Milk problem using semaphores. The problem is to have a refrigerator that we will try to keep full with N bottles of milk.

You live in a Co-op with many roommates. Each time a roommate comes home, they check the refrigerator to see if milk is needed. If so, they go to the store and buy a bottle. Each person only buys one bottle at a time.

Each roommate will be a process. The procedure MilkCheck is executed by each roommate process when arriving home. This procedure has the roommate check the refrigerator, and buy milk if necessary.

The program below is not quite correct. You are to fix this program so that is satisfies the above criteria.

The bold face text has been added to correct the problem.

Initialization
semaphore mutex(1);
int numberOfBottles = 0;
int numberOfShoppers = 0;

MilkCheck ()
{
	mutex.P();

	if ((numferOfBottles + numberOfShoppers) < N) {

		numberOfShoppers++;

		mutex.V();

		BuyABottleOfMilk();

		mutex.P();

		numberOfBottles++;

		numberOfShoppers--;
	}

	mutex.V();
}


Last modified: Tue Feb 13 08:36:53 CST 1996 by bart