All pastes #449790 Raw Edit

Untitled

public text v1 · immutable
·published 2007-04-20 13:19 UTC
rendered paste body
binarysem  coffee;
binarysem  brewer;
semaphore  sync;

int askB = 0;
int mugs;	

//---Change To Specs---
int students = 7;
int mugsperpot = 10;
//---------------------


void BrewerCall()
{
	int loop;
	
	while (0 == 0)	//Won't accept true for some reason;
	{
		wait(brewer);
		wait(coffee);
		
		cout << "Brewer brewed fresh coffee! \n";
		mugs = mugsperpot;	

		for (loop = 0; loop < students; loop++)
			signal(sync);
		signal(coffee);
	}
}


void Student(int num)
{
	while(0 == 0)	//Won't accept true for some reason;
		{	
			wait(coffee);

			if (askB == 1)
			{
				cout << "Student " << num << " asked brewer to brew more coffee. \n";
				cout << "Student " << num << " is sleeping... \n";
				askB = 0;
				signal(coffee);
				signal(brewer);
				wait(sync);				
			}
			else if (mugs > 0)
			{
				cout << "Student " << num << " got a new mug of coffee! \n";
				mugs = mugs - 1;		
				if (mugs <= 0)
					askB = 1;
				signal (coffee);				
			}
			else
 			{
				cout << "Student " << num << " is sleeping... \n";
				signal(coffee);
				wait(sync);
			}
		}
}


main()
{
	int loop;
	mugs = mugsperpot;
	
	cout << students << "Students starting to study... \n";

	initialsem(coffee,1);
	initialsem(sync,0);
	initialsem(brewer,0);

	cobegin
	{
		BrewerCall();
		for(loop = 1; loop <= students; loop++)
				Student(loop);
	}
}