Exemplo n.º 1
0
int main()
{
	mv = CreateMV("mv1");
	mv2 = CreateMV("mv2");
	
	lck = CreateLock("lock1");
	lck2 = CreateLock("lock2");
	
	cv = CreateCV("cv1");

	for (loop =0; loop<500; loop++) 
	{
		AcquireLock(lck);
		SignalCV(lck,cv);

		AcquireLock(lck2);
		ret = GetMV(mv2);
		ReleaseLock(lck2);
		
		if (ret != 21)/*This will wait only if the other one isnt finished*/
			WaitCV(lck,cv);

		mvEdit = GetMV(mv);
		Print("Client 2: Monitor variable was read to be %d..",mvEdit,-1,-1);

		mvEdit+=7;
	
		
		ret=SetMV(mv,mvEdit);
		mvEdit = GetMV(lck);
	
		Print("Client 2: Monitor variable was set to be %d..",mvEdit,-1,-1);
		
		ReleaseLock(lck);	

		Print("Client 2: %d Iterations done..",loop,-1,-1);
	}
	
	AcquireLock(lck2);
	ret = SetMV(mv2,21);
	
	/*this monitor variable is set to 21 here to signify 
	*one of the processes is over and the other will not
	*wait for it to signal it
	*/
	ReleaseLock(lck2);
	
	AcquireLock(lck);
	SignalCV(lck,cv);
	ReleaseLock(lck);

}
Exemplo n.º 2
0
void ManagerCountMoney(int* appMoney, int* picMoney, int* passMoney, int* cashMoney) {
	int i; /* for loops */
	int total; /* total money */

	Acquire(moneyLock);
	for(i = 0; i < NUM_APPCLERKS; i++) {
		*appMoney += GetMV(appClerkMoney, i);
		SetMV(appClerkMoney, i, 0);
	}
	Acquire(outputLock);
	Print("Manager has counted a total of $", 32);
	PrintInt((*appMoney) * 10);
	Print(" for Application Clerks.\n", 25);
	Release(outputLock);
	for(i = 0; i < NUM_PICCLERKS; i++) {
		*picMoney += GetMV(picClerkMoney, i);
		SetMV(picClerkMoney, i, 0);
	}
	Acquire(outputLock);
	Print("Manager has counted a total of $", 32);
	PrintInt((*picMoney) * 10);
	Print(" for Picture Clerks.\n", 21);
	Release(outputLock);
	for(i = 0; i < NUM_PASSCLERKS; i++) {
		*passMoney += GetMV(passClerkMoney, i);
		SetMV(passClerkMoney, i, 0);
	}
	Acquire(outputLock);
	Print("Manager has counted a total of $", 32);
	PrintInt((*passMoney) * 10);
	Print(" for Passport Clerks.\n", 22);
	Release(outputLock);
	for(i = 0; i < NUM_CASHIERS; i++) {
		*cashMoney += GetMV(cashierMoney, i);
		SetMV(cashierMoney, i, 0);
	}
	Acquire(outputLock);
	Print("Manager has counted a total of $", 32);
	PrintInt((*cashMoney) * 10);
	Print(" for Cashiers.\n", 15);
	Release(outputLock);
	total = *appMoney + *picMoney + *passMoney + *cashMoney;
	Acquire(outputLock);
	Print("Manager has counted a total of $", 32);
	PrintInt(total*10);
	Print(" for the Passport Office.\n", 26);
	Release(outputLock);
	Release(moneyLock);
}
Exemplo n.º 3
0
void getCustomer(int index){
		 while(true){
			int myLine = index;	
			PrintNum(myLine);
			Write("That was me; pass clerk\n", sizeof("That was me; pass clerk\n"), ConsoleOutput);
			
			if (GetMV(passport_clerks[myLine].bribeLineCount)>0){ /*if someone on bribe line */
				Write("   Someone is in Bribe line\n", sizeof("   Someone is in Bribe line\n"), ConsoleOutput);
				Acquire(PassClerkBribeLineLock[myLine]);
				Signal(PassClerkBribeLineLock[myLine], PassClerkBribeLineCV[myLine]); /*wake them up, call to register*/
				SetMV(passport_clerks[myLine].state, 0); /*make myself busy */
				passport_clerks[myLine].money = passport_clerks[myLine].money + 500;
				SetMV(manager.passClerkMoney, GetMV(manager.passClerkMoney)+500);
				Write("-PassClerk-Received bribe from customer\n", sizeof("-PassClerk-Received bribe from customer\n"), ConsoleOutput);
				
				Release(PassClerkBribeLineLock[myLine]); 
			}
			else if(GetMV(passport_clerks[myLine].lineCount)>0){ /* if someone on regular line  */
				Write("   Someone is in Regular Line, no one in Bribe line\n", sizeof("   Someone is in Regular Line, no one in Bribe line\n"), ConsoleOutput);
				Acquire(PassClerkLineLock[myLine]);
				Signal(PassClerkLineLock[myLine], PassClerkLineCV[myLine]); /* wake up waiting customer*/
				SetMV(passport_clerks[myLine].state, 0); /*state is busy now*/
				Release(PassClerkLineLock[myLine]);
			}
	
			else{
				SetMV(passport_clerks[myLine].state, 1); /*app clerk is available*/
			}	 
	
			Acquire(PassClerkLock[myLine]); /*acquire interaction lock*/
			Write("-PassClerk-Acquired Lock\n", sizeof("-PassClerk-Acquired Lock\n"), ConsoleOutput);
			Wait(PassClerkLock[myLine], PassClerkCV[myLine]); /*wait for customer to give SSN*/
	
			Write("--Got ssn and app from customer- verified \n", sizeof("--Got ssn and app from customer- verified \n"), ConsoleOutput);
			SetMV(customer_data[customer_counter].verified, 1);
		
			Signal(PassClerkLock[myLine], PassClerkCV[myLine]);
			/*if (passport_clerks[myLine].bribe == true){ 
				passport_clerks[myLine].money = passport_clerks[myLine].money + 500;
				Write("-PassClerk-Received bribe from customer\n", sizeof("-PassClerk-Received bribe from customer\n"), ConsoleOutput);
				passport_clerks[myLine].bribe = false;
			}*/
			SetMV(passport_clerks[myLine].state, 1);
			/*Wait(PassClerkLock[myLine], PassClerkCV[myLine]);
			Signal(PassClerkLock[myLine], PassClerkCV[myLine]); /*signal the customer that record is completed*/
			Release(PassClerkLock[myLine]); /*release the lock*/
			/*break;*/
		}
}
Exemplo n.º 4
0
int
main()
{
	int lock3,mv3,cv3;

	lock3 = CreateLock("lock3",5);
	cv3 = CreateCV("cv3",3);
	mv3 = CreateMV("mv3",3,5);

	WriteToConsole("\nAcquiring lock3 in rpcclient5\n\0", -1, -1, -1);
	if((Acquire("lock3",5)) == -1){
		WriteToConsole("Error acquiring lock3 in rpcclient5\n\0",-1,-1,-1);
		Exit(0);
	}

	if((Wait("cv3",3, "lock3",5)) == -1){
			WriteToConsole("Error waiting on cv3 and lock3 in rpcClient1\n\0",-1,-1,-1);
			Exit(0);
	}

	WriteToConsole("\nGot Signal on cv3 lock3 from rpcClient1\n\0", -1, -1, -1);
	WriteToConsole("\nValue of mv3 index 3 set by rpcClient1 is %d\n\0", GetMV("mv3",3,3), -1, -1);
	Release("lock3",5);

	DeleteLock("lock3",5);
	WriteToConsole(" Deleted Lock3\n\0",-1,-1,-1);
	DeleteMV("mv3",3);
	WriteToConsole(" Deleted MV3\n\0",-1,-1,-1);
	DeleteCV("cv3",3);
	WriteToConsole(" Deleted CV3\n\0",-1,-1,-1);

	Exit(0);
}
Exemplo n.º 5
0
void main()
{
	int i=0;

	CreateMV("TC\0");
	SetMV("TC\0", 1);

	CreateMV("TCN\0");
	SetMV("TCN\0", -1);

	CreateMV("CU\0");
	SetMV("CU\0", 3);

	CreateMV("CC\0");
	SetMV("CC\0", -1);
	
	CreateMV("TT\0");
	SetMV("TT\0", -1);
	
	CreateMV("CN\0");
	SetMV("CN\0", -1);

	CreateMV("CG\0");
	SetMV("CG\0", -1);

        CreateMV("NTT\0");
        SetMV("NTT\0", 0);
        CreateMV("SIM\0");
        SetMV("SIM\0", 1);

	CreateMV("Z\0");
	SetMV("Z\0", 0);
			
	Exec("../test/man", 11, 16); /* starting manager*/

	CreateMV("EP1\0");
	CreateMV("EP2\0");
	SetMV("EP1\0", 0);
	SetMV("EP2\0", 0);

	while(GetMV("Z\0") == 0)
	{
		 Yield();
		 Yield();
		 Yield();
		 Yield();
		 Yield();
	}
	Exec("../test/tc", 10, 2); /*starting ticket clerk*/
	Exec("../test/cc", 10, 13); /*starting concession clerk*/
	Exec("../test/tt", 10, 14); /*starting ticket taker*/
	Exec("../test/mt", 10, 15); /*starting movie technician*/ 
	for(i = 3; i < 13; i++)
	{
		Exec("../test/cust", 12, i); /*starting customers*/
	}
	
	Exit(0);	
	
}
Exemplo n.º 6
0
void CRUCache::RecursiveFetchCascadedMVs(CDDMV *pddRootMV)
{
	// Setup the wrapper object for the MV
	FetchSingleInvolvedMV(pddRootMV);

	CDDUIDTripleList &uList = pddRootMV->GetUsedObjects();

	DSListPosition pos = uList.GetHeadPosition();
	while (NULL != pos) 
	{
		CDDUIDTriple& uidt = uList.GetNext(pos);

		// Skip the regular tables 
		if (FALSE == pddRootMV->IsUsedObjectAnMV(uidt.objUID))
		{
			continue;
		}

		CDDMV *pddMV = GetDDMVByUID(uidt);

		// An ON STATEMENT MV is a regular table 
		// for the purpose of cascade. Stop drilling inside.
		if (CDDObject::eON_STATEMENT == pddMV->GetRefreshType())
		{
			continue;
		}

		// Since the same object cannot be used twice by the same MV,
		// the used MV should not have been referenced before
		// (and placed in the MV list).
		RUASSERT (NULL != GetMV(pddRootMV->GetUID()));

		RecursiveFetchCascadedMVs(pddMV);		
	}
}
Exemplo n.º 7
0
int main() {
	
	int rv = 0, index = 0, cvrv = 0;
	int mvVal = 0;
	
	Print_Stmt("Calling start simulation\n");
	StartSimulation();
	Print_Stmt("In halt.c\n");
	
	
	rv = CreateLock("firstLock", 9, 1);
	
	
    rv = CreateLock("secondLock", 10, 1); 
	rv = CreateLock("firstLock", 9, 2); 
 	rv = CreateLock("secondLock", 10, 2);
	rv = CreateLock("firstLock", 9, 3); 
 	rv = CreateLock("secondLock", 10, 3);
	rv = CreateLock("firstLock", 9, 4); 
 	rv = CreateLock("secondLock", 10, 4);
	rv = CreateLock("firstLock", 9, 5); 
 	rv = CreateLock("secondLock", 10, 5);
	rv = CreateLock("firstLock", 9, 6); 
 	rv = CreateLock("secondLock", 10, 6);
	rv = CreateLock("firstLock", 9, 7); 
 	rv = CreateLock("secondLock", 10, 7);
	rv = CreateLock("firstLock", 9, 8); 
 	rv = CreateLock("secondLock", 10, 8);
	rv = CreateLock("firstLock", 9, 9); 
 	rv = CreateLock("secondLock", 10, 9);
	rv = CreateLock("firstLock", 9, 10); 
 	rv = CreateLock("secondLock", 10, 10);
	
	
	Acquire(rv);
 	Release(rv); 
	
	cvrv = CreateCV("firstCV", 7, 0);
 	cvrv = CreateCV("firstCV", 7, 0);
	cvrv = CreateCV("firstCV2", 8, 0);
	cvrv = CreateCV("firstCV3", 8, 0); 
	
 	Wait(cvrv, rv); 
	Signal(cvrv, rv); 
	
	DestroyLock(rv); 
	
	rv = CreateCV("firstCV", 7, 4);  
	CreateMV("firstMV", 7, 0); 
	
 	rv = CreateMV("firstMV", 7, 0);
	SetMV(rv, 3);
	mvVal = GetMV(rv);
	
	Print_1Arg("MV value obtained in halt is %d\n", mvVal); 
	
	Print_Stmt("Exiting in halt.c\n");
	
}
Exemplo n.º 8
0
int main(){
	setup();
	index = GetMV(MyLinePassClerk);
	SetMV(MyLinePassClerk, index+1);
	getCustomer(index);

	return 0;
}
Exemplo n.º 9
0
int
main()
{
	int lock2, cv2, mv2;

	if((lock2 = CreateLock("lock2",5)) == -1){
		WriteToConsole("Error Creating lock2 in rpcClient4\n\0",-1,-1,-1);
		Exit(0);
	}
	WriteToConsole("lock2 created in rpcClient4 with lockid: %d\n\0",lock2,-1,-1);
	
	WriteToConsole("cv1 created in rpcClient4 with CVid: %d\n\0",cv2,-1,-1);
	if((cv2 = CreateCV("cv2", 3)) == -1){
		WriteToConsole("Error Creating CV2 in rpcClient4\n\0",-1,-1,-1);
		Exit(0);
	}
	WriteToConsole("cv2 created in rpcClient4 with CVid: %d\n\0",cv2,-1,-1);
	
	if((mv2 = CreateMV("mv2", 3, 5)) == -1){
		WriteToConsole("Error Creating MV2 in rpcClient2\n\0",-1,-1,-1);
		Exit(0);
	}
	WriteToConsole("mv2 created in rpcClient2 with MVid: %d\n\0",mv2,-1,-1);
	
	if((Acquire(lock2)) == -1){
		WriteToConsole("Error acquiring lock2 with id:%d in rpcClient4\n\0",lock2,-1,-1);
		Exit(0);
	}
	WriteToConsole("\nAcquired lock2: %d\n\0", lock2, -1, -1);
	WriteToConsole("\nValue of MV2 index 0 set by rpcClient4 is %d\n\0", GetMV(mv2,0), -1, -1);
	
	if((SetMV(mv2, 1, 5)) == -1){
		WriteToConsole("Error setting mv2 in rpcClient4\n\0",-1,-1,-1);
		Exit(0);
	}
	WriteToConsole("\nMV2 index: 1 set to 5 by rpcClient4\n\0", -1, -1, -1);
	
	WriteToConsole("\nSignalling on CV2 Lock2 to rpcClient1\n\0", -1, -1, -1);
	if((Signal(cv2, lock2)) == -1){
		WriteToConsole("Error signalling on cv2 and lock2 in rpcClient4\n\0",-1,-1,-1);
		Exit(0);
	}
	if((Release(lock2)) == -1){
		WriteToConsole("Error releasing lock2 with id:%d in rpcClient1\n\0",lock2,-1,-1);
		Exit(0);
	}
	WriteToConsole("\nReleased lock2: %d\n\0", lock2, -1, -1);

	DeleteLock(lock2);
	WriteToConsole("Sent DeletLock Request id: %d\n\0",lock2,-1,-1);
	DeleteCV(cv2);
	WriteToConsole("Sent DeletCV Request id: %d\n\0",cv2,-1,-1);
	DeleteMV(mv2);
	WriteToConsole("Sent DeleteMV Request id: %d\n\0",mv2,-1,-1);
	Exit(0);
}
Exemplo n.º 10
0
int main() {
	int id;
	Setup();
	Acquire(appLock);
	id = GetMV(appClerkIndex, 0);
	SetMV(appClerkIndex, 0, id+1);
	Release(appLock);
	appClerkRun(id);
	Exit(0);
}
Exemplo n.º 11
0
int main()
{
	Trace("Creating Lock", 0x9999);
	Trace("\n", 0x9999);
	lock = ServerCreateLock("Lock", sizeof("Lock"), 1);
	Trace("Lock's index: ", lock);
	Trace("\n", 0x9999);
	
	Trace("Creating CV", 0x9999);
	Trace("\n", 0x9999);
	cv = ServerCreateCV("CV", sizeof("CV"), 1);
	Trace("CV's index: ", cv);
	Trace("\n", 0x9999);
	
	Trace("Creating MV\n", 0x9999);
	mv = CreateMV("MV", sizeof("MV"), 1, 10);
	Trace("MV's index: ", mv);
	Trace("\n", 0x9999);
	Trace("MV's value: ", GetMV(mv, 0));
	Trace("\n", 0x9999);
	
	Trace("Acquiring lock\n", 0x9999);
	ServerAcquire(lock, 0);
	
	Trace("Incrementing MV\n", 0x9999);
	SetMV(mv, 0, GetMV(mv, 0)+1);
	
	Trace("Waiting on CV with Lock\n", 0x9999);
	ServerWait(cv, 0, lock, 0);
	Trace("Woken up from wait\n", 0x9999);
	Trace("Now deleteing lock and cv\n", 0x9999);
	ServerRelease(lock, 0);
	
	ServerDestroyLock(lock, 0);
	ServerDestroyCV(cv, 0);
	
	Trace("Final MV value should be 4\n", 0x9999);
	Trace("Final MV value: ", GetMV(mv, 0));
	Trace("\n", 0x9999);
	/* not reached */
	Exit(0);
}
Exemplo n.º 12
0
int main()
{
	mv = CreateMV("mv1");
	lck = CreateLock("lock1");

	AcquireLock("lock1");
	mv2 = GetMV("mv1");
	Print("Client 2: Monitor variable was read to be %d..",mv2,-1,-1);
	ReleaseLock("lock1");

	mv2++;
	AcquireLock("lock1");
	ret=SetMV("mv1",mv2);
	mv2 = GetMV("mv1");
	
	Print("Client 2: Monitor variable was set to be %d..",mv2,-1,-1);
	ReleaseLock("lock1");

	Exit(0);

}
Exemplo n.º 13
0
int main () {

  int lock0 = -1;
  int mv0 = -1;
  int mv0value = -1;

  Print("Registering with network thread.\n",0,0,0);
  Register();
  Print("Done Registering with network thread.\n",0,0,0);

  Print("Creating lock with name \"lockOne\"\n",0,0,0);
  lock0 = CreateLock("lockOne");
  Print("Created lock with name \"lockOne\"\n",0,0,0);
  
  Print("Acquiring lock0\n",0,0,0);
  Acquire(lock0);
  Print("Acquired lock0\n",0,0,0);

  Print("Creating MV with name \"mvOne\"\n",0,0,0);
  mv0 = CreateMV("mvOne");
  Print("Created MV with name \"mvOne\" and ID=%d\n",mv0,0,0);

  
  mv0value = GetMV(mv0);
  Print("Current value of \"mvOne\" =%d\n",mv0value,0,0);

  mv0value++;

  Print("Setting value of \"mvOne\" to %d\n",mv0value,0,0);
  SetMV(mv0,mv0value);

  mv0value = GetMV(mv0);
  Print("New value of \"mvOne\" =%d\n",mv0value,0,0);
  
  Print("Releasing lock0\n",0,0,0);
  Release(lock0);

}
Exemplo n.º 14
0
void main()
{
    Print("\nTest3 is for waiting and broadcasting condition variable.\n", sizeof("\nTest3 is for waiting and broadcasting condition variable.\n")/sizeof(char), 0, 0);
     l1 = CreateLock("l1", 2);
     c1 = CreateCondition("c1", 2);
     m1 = CreateMV("m1", 2, 3);
        
    AcquireLock(l1);
    Print("client2 of Test3 has acquired lock1.\n", sizeof("client2 of Test3 has acquired lock1.\n")/sizeof(char), 0, 0);
    if (value == 0) {
        Print("client2 is waiting other to signal.\n", sizeof("client2 is waiting other to signal.\n")/sizeof(char), 0, 0);
        WaitCondition(c1, l1);
    }
    value = GetMV(m1, 1);
    Print("The MV value of client2 is [%d].\n", sizeof("The MV value of client2 is [%d].\n")/sizeof(char), 1, value);
    ReleaseLock(l1);
    Print("client2 of Test3 has released lock1.\n", sizeof("client2 of Test3 has released lock1.\n")/sizeof(char), 0, 0);
    Exit(0);
}
Exemplo n.º 15
0
void CRUCache::FetchNonInvolvedMVsMetadata()
{
	DSListPosition tpos = tableList_.GetHeadPosition();
	while (NULL != tpos) 
	{
		CRUTbl *pTbl = tableList_.GetNext(tpos);
		
		// Retrieve the table's private dependency list
		CDDUIDTripleList &privTblList = pTbl->GetUIDsOfAllMVsUsingMe();
			
		DSListPosition mvpos = privTblList.GetHeadPosition();
		while (NULL != mvpos)
		{
			CDDUIDTriple &uidt = privTblList.GetNext(mvpos);

			CRUMV *pMV = GetMV(uidt.objUID); 
			if (NULL == pMV)
			{
				// The MV is non-involved, fetch the DD object
				CDDMV *pddMV = GetDDMVByUID(uidt);

				// If the MV is not ON REQUEST, skip it
				if (CDDObject::eON_REQUEST != pddMV->GetRefreshType())
				{
					continue;
				}

				pMV = FetchSingleNonInvolvedMV(pddMV);
			}

			if (FALSE == pMV->IsInvolved())
			{
				// If the MV is involved, the pointer exists already
				pTbl->AddRefToUsingMV(pMV);
			}
		}
	}	
}
Exemplo n.º 16
0
void CRUCache::SetupUsedObject(CRUTbl *pTbl)
{
	// If the table is also an involved MV, the DDL lock is already handled
	if (NULL == GetMV(pTbl->GetUID()))
	{
		// Register the table for the further DDL lock handling
		ddlLockHandler_.AddObject(pTbl);
	}
	
	if (TRUE == isCancelOnly_)
	{
		return;	// No more processing is required
	}

	if (1 == pTbl->GetOnRequestMVsUsingMe().GetCount())
	{
		// Fetch the epochs' metadata
		pTbl->FetchMetadata();

		// Fixup the pointer to the IUD log table
		FixupLogTblReference(pTbl);
	}
}
Exemplo n.º 17
0
void main()
{
	int lck1, lck2,cv1,cv2, mv1, setMvValue=-1, getMvValue =-1, arrayIndex=1;

	Print("\n TC11:: Checking Negative Working of Create, Set & Get MV. \n\0",1,1,1);

	lck1 = CreateLock("lock1", 5);

	Acquire(lck1);

	cv1 = CreateCV("cv1", 3);

/*	mv1 = CreateMV("mv1", 3, 1);			Print("\n TC1:: MV name 'mv1' having Index '%d', of Array Size 1 Created. \n\0",mv1,1,1);
*/
	setMvValue = SetMV("mv1", 3, 1, 5);		Print("\n TC11:: MV Value: %d, Has Been Set for mv1. \n\0",setMvValue,1,1);

	getMvValue = GetMV("mv1", 3, 1);		Print("\n TC11:: GetMV gets: %d, for mv1. \n\0",getMvValue,1,1);

	Release(lck1);

	Print("\n TC11:: Reached end. value returned is %d\n\0",lck1,1,1);
	Exit(0);
}
Exemplo n.º 18
0
void main()
{
	int lck1, lck2,cv1,cv2, mv1, setMvValue=-1, getMvValue =-1, arrayIndex=1;

	Print("\n  TC10_1 ::Multiple creation of same MV by diff userprog's. \n\0",1,1,1);

	lck1 = CreateLock("lock1", 5);

	Acquire(lck1);

	cv1 = CreateCV("cv1", 3);

	mv1 = CreateMV("mv1", 3, 1);
	Print("\n TC10_1:: MV name 'mv1' having Index '%d', of Array Size 1 Created. \n\0",mv1,1,1);

	Print("\n TC10_1:: Setting value for MV name 'mv1' at location '%d', at its Array Index '0'\n\0",mv1,1,1);
	SetMV(mv1, 0, 5);

	getMvValue = GetMV(mv1, 0);
	Print("\n TC10_1:: GetMV gets: '%d' for 'mv1', Array Index '0'. \n\0",getMvValue,1,1);

	Print("\nTC10_1 :: Going on wait on 'cv1' using 'lock1' that is acquired. Now run the 'tc10_2' file. \n",1,1,1);
	Wait(cv1,lck1);
	Print("\nTC10_1 :: Have been signalled on 'cv1' using 'lock1'.\n",1,1,1);

	Release(lck1);

	DeleteCV(cv1);

	DeleteLock(lck1);

	DeleteMV(mv1);

	Print("\n Reached end. value returned is %d\n\0",lck1,1,1);
	Exit(0);
}
Exemplo n.º 19
0
int getMv(MV *mv)
{
	mv->value= GetMV(mv->name);
	return mv->value;
}
Exemplo n.º 20
0
int main() {
	/* ------------------Local data -------------------------*/
	int myIndex;

	int initIndex = CreateMV("AppIndex", sizeof("AppIndex"), 1, 0x9999);
	int initIndexLock = ServerCreateLock("AppIndexLock", sizeof("AppIndexLock"), 1);

	int mySSN;
	int i;
	int cType = 0;

	enum BOOLEAN loop = true;
	int shutdown = CreateMV("shutdown", sizeof("shutdown"), 1, 0x9999);
	/* -----------------Shared Data------------------------*/

	/* Number of customers/senators in office and thier locks */
	int officeSenator = CreateMV("officeSenator", sizeof("officeSenator"), 1, 0x9999);
	int officeCustomer = CreateMV("officeCustomer", sizeof("officeCustomer"), 1, 0x9999);	
	int senatorLock = ServerCreateLock("senatorLock", sizeof("senatorLock"), 1);
	int customerLock = ServerCreateLock("customerLock", sizeof("customerLock"), 1);

	/*Locks, conditions, and data used for waitinng */
	int clerkWaitLock = ServerCreateLock("clerkWaitLock", sizeof("clerkWaitLock"), 1);
	int clerkWaitCV = ServerCreateCV("clerkWaitCV", sizeof("clerkWaitCV"), 1);
	int numAppWait = CreateMV("numAppWait", sizeof("numAppWait"), 1, 0x9999);

	/* Reg and priv line lengths, line locks, and reg and priv line conditions. */
	int regACLineLength = CreateMV("regACLineLength", sizeof("regACLineLength"), 1, 0x9999);
	int privACLineLength = CreateMV("privACLineLength", sizeof("privACLineLength"), 1, 0x9999);
	int acpcLineLock = ServerCreateLock("acpcLineLock", sizeof("acpcLineLock"), 1);
	int regACLineCV = ServerCreateCV("regACLineCV", sizeof("regACLineCV"), 1);
	int privACLineCV = ServerCreateCV("privACLineCV", sizeof("privACLineCV"), 1);


	/* Individual clerk locks, conditions, data, data booleans, and states */
	int appLock = ServerCreateLock("appLock", sizeof("appLock"), NUM_CLERKS);
	int appCV = ServerCreateCV("appCV", sizeof("appCV"), NUM_CLERKS);

	int appState = CreateMV("appState", sizeof("appState"), NUM_CLERKS, 0x9999);
	int appData = CreateMV("appData", sizeof("appData"), NUM_CLERKS, 0x9999);

	/* Money data and locks for each clerk type */
	int appMoney = CreateMV("appMoney", sizeof("appMoney"), 1, 0x9999);
	int appMoneyLock = ServerCreateLock("appMoneyLock", sizeof("appMoneyLock"), 1);

	/* Individual customer's file state, type, and its lock */
	int fileLock = ServerCreateLock("fileLock", sizeof("fileLock"), NUM_CUSTOMERS + NUM_SENATORS);
	int fileState = CreateMV("fileState", sizeof("fileState"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);
	int fileType = CreateMV("fileType", sizeof("fileType"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);

	traceLock = ServerCreateLock("traceLock", sizeof("traceLock"), 1);
	
	/* initializes myIndex */
	ServerAcquire(initIndexLock, 0);
	myIndex = GetMV(initIndex, 0);
	SetMV(initIndex, 0, GetMV(initIndex, 0) + 1);
	ServerRelease(initIndexLock, 0);
	
	SetMV(appState, myIndex, 1);

	/* --------------------BEGIN APPCLERK STUFF----------------*/
	while(loop == true){
		if (GetMV(shutdown, 0) == 1) {
			ServerAcquire(traceLock, 0);
			ClerkTrace("App", myIndex, 0x00, 0, "Shutting down.\n"); 
			ServerRelease(traceLock, 0);
			Exit(0);
		}
		ServerAcquire(senatorLock, 0);
		ServerAcquire(customerLock, 0);
		if(GetMV(officeSenator, 0) > 0 && GetMV(officeCustomer, 0) > 0){
			ServerRelease(senatorLock, 0);
			ServerRelease(customerLock, 0);

			ServerAcquire(clerkWaitLock, 0);
			ServerWait(clerkWaitCV, 0, clerkWaitLock, 0);
			ServerRelease(clerkWaitLock, 0);
		}
		else{
			ServerRelease(senatorLock, 0);
			ServerRelease(customerLock, 0);
		}

		ServerAcquire(acpcLineLock, 0);
		
		/* Check for the privileged customer line first
		*  If there are privileged customers, do AppClerk tasks, then received $500
		*/
		if(GetMV(privACLineLength, 0) > 0){
			/*Trace("Checking privLine\n", 0x9999);*/
			SetMV(privACLineLength, 0, GetMV(privACLineLength, 0)-1);

			ServerAcquire(senatorLock, 0);
			SetMV(numAppWait, 0, GetMV(numAppWait, 0)+1);	/* shows customer that one clerk is waiting */
			ServerRelease(senatorLock, 0);

			ServerAcquire(appLock, myIndex);
			SetMV(appState, myIndex, 0);	/* 0 = AVAILABLE */
			ServerSignal(privACLineCV, 0, acpcLineLock, 0); /* Signals the next customer in priv line */
			ServerRelease(acpcLineLock, 0);
			/*Trace("Waiting privLine\n", 0x9999);*/
			ServerWait(appCV, myIndex, appLock, myIndex); /* Waits for the next customer */

			mySSN = GetMV(appData, myIndex);
			ServerAcquire(fileLock, mySSN);

			/* check the customer type */
			if(GetMV(fileType, mySSN) == 0){ /* 0 = CUSTOMER */
				cType = 0;
			}
			else{
				cType = 1;	/* 1 = SENATOR */
			}

			if(GetMV(fileState, mySSN) == 0){ /* 0 = NONE */
				SetMV(fileState, mySSN, 2); /* 2 = APPDONE */
				ServerRelease(fileLock, mySSN);
			}
			else if(GetMV(fileState, mySSN) == 1){ /* 1 = PICDONE */
				SetMV(fileState, mySSN, 3); /* 3 = APPPICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else{
				ServerAcquire(traceLock, 0);
				ClerkTrace("App", myIndex, "Cust", mySSN,
					"ERROR:Customer doesn't have a picture application or no application. What are you doing here?\n");
				ServerRelease(traceLock, 0);
				ServerRelease(fileLock, mySSN);
			}

			for (i = 0; i < 20; i++){
				Yield();
			}

			ServerAcquire(traceLock, 0);
			if(cType == 0){
				ClerkTrace("App", myIndex, "Cust", mySSN, "Customer's app has been filed.\n");
			}
			else{
				ClerkTrace("App", myIndex, "Sen", mySSN, "Senator's app has been filed.\n");
			}
			ServerRelease(traceLock, 0);

			ServerAcquire(appMoneyLock, 0);
			SetMV(appMoney, 0, GetMV(appMoney, 0) + 500);

			ServerAcquire(traceLock, 0);
			if(cType == 0){
				ClerkTrace("App", myIndex, "Cust", mySSN, "Accepts $500 from Customer.\n");
			}
			else{
				ClerkTrace("App", myIndex, "Sen", mySSN, "Accepts $500 from Senator.\n");
			}
			ServerRelease(traceLock, 0);

			ServerRelease(appMoneyLock, 0);
			ServerSignal(appCV, myIndex, appLock, myIndex); /* signal customer awake */
			ServerRelease(appLock, myIndex);	/* Release clerk lock */

		}	
		/* Check for regular customer line next.
		* If there are regular customers, do AppClerk tasks 
		*/
		else if(GetMV(regACLineLength,0) > 0){
			/*Trace("Checking regLine\n", 0x9999);*/
			SetMV(regACLineLength, 0, GetMV(regACLineLength, 0)-1);

			ServerAcquire(senatorLock, 0);
			SetMV(numAppWait, 0, GetMV(numAppWait, 0)+1);	/* shows customer that one clerk is waiting */
			ServerRelease(senatorLock, 0);

			ServerAcquire(appLock, myIndex);
			SetMV(appState, myIndex, 0); /* 0 = AVAILABLE */
			ServerSignal(regACLineCV, 0, acpcLineLock, 0); /* Signals the next customer in priv line */
			ServerRelease(acpcLineLock, 0);
			/*Trace("Waiting regLine\n", 0x9999);*/
			ServerWait(appCV, myIndex, appLock, myIndex); /* Waits for the next customer */

			mySSN = GetMV(appData, myIndex);
			ServerAcquire(fileLock, mySSN);

			/* check the customer type */
			if(GetMV(fileType, mySSN) == 0){
				cType = 0;
			}
			else{
				cType = 1;
			}

			if(GetMV(fileState, mySSN) == 0){ /* 0 = NONE */
				SetMV(fileState, mySSN, 2); /* 2 = APPDONE */
				ServerRelease(fileLock, mySSN);
			}
			else if(GetMV(fileState, mySSN) == 1){ /* 1 = PICDONE */
				SetMV(fileState, mySSN, 3); /* 3 = APPPICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else{
				ServerAcquire(traceLock, 0);
				ClerkTrace("App", myIndex, "Cust", mySSN,
					"ERROR:Customer doesn't have a picture application or no application. What are you doing here?\n");
				ServerRelease(traceLock, 0);
				ServerRelease(fileLock, mySSN);
			}

			for (i = 0; i < 20; i++){
				Yield();
			}

			ServerAcquire(traceLock, 0);
			if(cType == 0){
				ClerkTrace("App", myIndex, "Cust", mySSN, "Informs Customer their app has been filed.\n");
			}
			else{
				ClerkTrace("App", myIndex, "Sen", mySSN, "Informs Senator their app has been filed.\n");
			}
			ServerRelease(traceLock, 0);

			ServerSignal(appCV, myIndex, appLock, myIndex); /* signal customer awake */
			ServerRelease(appLock, myIndex);	/* Release clerk lock */
		}
		/* If there are neither privileged or regular customers, go on break */
		else{
			/*Trace("Going on break\n", 0x9999);*/
			ServerRelease(acpcLineLock, 0);
			ServerAcquire(appLock, myIndex);
			SetMV(appState, myIndex, 2); /* 2 = BREAK */
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("App", myIndex, 0x00, 0, "Going on break.\n");
			ServerRelease(traceLock, 0);

			ServerWait(appCV, myIndex, appLock, myIndex);
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("App", myIndex, 0x00, 0, "Returning from break.\n");
			ServerRelease(traceLock, 0);
			
			ServerRelease(appLock, myIndex);
		}
	}
}
Exemplo n.º 21
0
int
main()
{
	int lockId, lockStatus, cvId, mvStatus, mvId, mvValue, lockId1, lockId2;

	WriteToConsole("\nCreating a Lock\n\0", -1, -1, -1);

	lockId = CreateLock("lock1",5);
	if(lockId == -1)
	{
		WriteToConsole("\nError Occurred while creating Lock. Exiting program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nLock Created, Lock Id returned by the server is : %d\n\0", lockId, -1, -1);
	}

	WriteToConsole("\nCreating a CV\n\0", -1, -1, -1);

	cvId = CreateCV("cv1",3);
	if(cvId == -1)
	{
		WriteToConsole("\nError Occurred while creating CV. Exiting program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nCV Created, CV Id returned by the server is : %d\n\0", cvId, -1, -1);
	}

	WriteToConsole("\nCreating a MV\n\0", -1, -1, -1);

	mvId = CreateMV("mv1",3, 5);
	if(mvId == -1)
	{
		WriteToConsole("\nError Occurred while creating MV. Exiting program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nMV Created, MV Id returned by the server is : %d\n\0", mvId, -1, -1);
	}


	WriteToConsole("\nSetting MV to 100\n", -1, -1, -1);
	mvStatus = SetMV(mvId, 1, 100);
	if(mvStatus == -1)
	{
		WriteToConsole("\nFailed to Set MV. Exiting Program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nMV set successfully.\n\0", -1, -1, -1);
	}

	WriteToConsole("\nDeleting MV\n", -1, -1, -1);
	mvStatus = DeleteMV(mvId);
	if(mvStatus == -1)
	{
		WriteToConsole("\nFailed to Delete MV. Exiting Program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nMV deleted successfully.\n\0", -1, -1, -1);
	}

	WriteToConsole("\nTrying to fetch value from MV which is deleted : %d\n\0",mvId,-1,-1);
	mvValue = GetMV(mvId+25, 1);
	if(mvValue == -1)
	{
		WriteToConsole("\nInvalid mv Id. Exiting Program\n\0", -1, -1, -1);
		Exit(0);
	}
	else
	{
		WriteToConsole("\nAbnormal Execution\n\0", -1, -1, -1);
	}

   Exit(0);
}
Exemplo n.º 22
0
 void patient()
{
	int myIndex,k;
	int patientWaitingCount;
		
	Acquire(patientIndexLock);
	myIndex=GetMV(globalPatientIndexMV,0);
	SetMV(globalPatientIndexMV,0,myIndex+1);
	Release(patientIndexLock);

	Acquire(printLock);
	Printf1((unsigned int)"Adult Patient:",myIndex);
	Printf((unsigned int)" has entered the Doctor's Office Waiting Room.\n");
	Release(printLock);

	Acquire(wrnLineLock);
	if(GetMV(wrnStatusMV,0)==BUSY)
	{
		/*If wrn is busy, enter into line*/
		/*Printf((unsigned int)" wrnStatus=>BUSY\n");*/
		Acquire(printLock); 
		Printf1((unsigned int)"Adult Patient:",myIndex);
		Printf((unsigned int)" gets in line of the Waiting Room Nurse to get registration form.\n");
		Release(printLock); 
		 		 
		patientWaitingCount=GetMV(patientWaitingCountMV,0);
		SetMV(patientWaitingCountMV,0,patientWaitingCount+1);
		
		Acquire(printLock); 
		Printf1((unsigned int)"Adult Patient:",myIndex);
		Printf((unsigned int)" waits in line to get form\n");
		Release(printLock); 
		
		Wait(patientWaitingCV,wrnLineLock);
	} 		 
	else if(GetMV(wrnStatusMV,0)==FREE)
	{
		/* If wrn is FREE, make wrnStatus = BUSY*/
		Printf1((unsigned int)"Adult Patient:",myIndex);
		Printf((unsigned int)" finds no one is in line\n");
		SetMV(wrnStatusMV,0,BUSY);
	}
	Release(wrnLineLock);	
	Acquire(wrnLock);
	SetMV(patientTaskMV,0,GETFORM);	
	SetMV(patientGettingFormMV,0,myIndex);
	Signal(wrnWaitingCV,wrnLock); 
	Release(wrnLock);
	
	/* Allocate random values of age & name to patient*/
	Acquire(infoLock);
	/*SetMV(patientES_age,myIndex,random()%100 + 1);
	SetMV(patientES_name,myIndex,random()%26 + 1);
	SetMV(patientES_pid,myIndex,myIndex);*/
	Release(infoLock);
		
	for(k=0;k<10;k=k+1) {
			Yield();
	}
	
	Acquire(wrnLineLock);
	if(GetMV(wrnStatusMV,0) == BUSY)
	{
		/*Printf((unsigned int)"wrnStatus => Busy\n");*/
		Acquire(printLock);
		Printf1((unsigned int)"Adult Patient:",myIndex);
		Printf((unsigned int)" gets in line of the Waiting Room Nurse to submit registration form.\n");
		Release(printLock);
		patientWaitingCount=GetMV(patientWaitingCountMV,0);
		SetMV(patientWaitingCountMV,0,patientWaitingCount+1);
		Wait(patientWaitingCV,wrnLineLock);
	}
	else if(GetMV(wrnStatusMV,0) == FREE)	
	{ 
		/*Printf((unsigned int)"wrnStatus=>Free\n");*/
		SetMV(wrnStatusMV,0,BUSY);
	}
	Release(wrnLineLock);
	Acquire(wrnLock);
	SetMV(patientTaskMV,0, GIVEFORM);
	SetMV(patientGivingFormMV,0, myIndex);
	Acquire(printLock);	
	Printf1((unsigned int)"Adult patient:",myIndex);
	Printf((unsigned int)" submits the filled form to the Waiting Room Nurse.\n");
	Release(printLock);	
	Signal(wrnWaitingCV,wrnLock);
	Release(wrnLock);		 	
}
Exemplo n.º 23
0
void CoachAttendant() {
    unsigned int myID;
    int myChef=-1;
    int i = 0;
    int shortestLength;

    int one[1];
    int two[2];
    int three[3];
    int four[4];

    ServerAcquire(IDLock,-1);
    myID = GetMV(coachAttendantID,-1);
    SetMV(coachAttendantID, GetMV(coachAttendantID,-1) + 1,-1);
    ServerRelease(IDLock,-1);

    /*printf("coach attendant%d is alive\n", myID);*/

    while(true) {
        myChef=-1;
        ServerAcquire(coachAttendantLock[myID],-1);
        /*printf("coachAttendant%d acquired a lock on themselves\n", myID);*/
        if(GetMV(coachAttendantFirstLineLength[myID],-1) != 0) {
            ServerAcquire(coachAttendantLineLock[myID],-1);
            /*printf("coachAttendant%d has %d people in FC line\n and is signalling the first to come up\n", myID, coachAttendantFirstLineLength[myID]);*/
            /*I process a fc. passenger in line*/
            ServerSignal(coachAttendantFirstLine[myID], coachAttendantLineLock[myID],-1);
            /*wait for a passenger to hand over their ticket*/
            ServerWait(coachAttendantLine[myID], coachAttendantLineLock[myID],-1);
            ServerAcquire(getSeatLock[myID],-1);
            ServerRelease(coachAttendantLineLock[myID],-1);

            /*find a seat for this passenger*/
            ServerAcquire(globalSeatLock,-1);
            /*side effect---- seat is incremented*/
            SetMV(GetMV(passengerTicketsSeatNumber[myID],-1), GetMV(nextSeat,-1),-1);
            SetMV(nextSeat, GetMV(nextSeat,-1) + 1,-1);
            ServerRelease(globalSeatLock,-1);
            /*I wake up the passenger and wait for them to have a porter*/
            ServerSignal(getSeat[myID], getSeatLock[myID],-1);
            ServerAcquire(CAWaitingForPorterLock[myID],-1);
            ServerRelease(getSeatLock[myID],-1);
            ServerWait(CAWaitingForPorter[myID], CAWaitingForPorterLock[myID],-1);
            /*passenger has signalled me that they have a porter and I can seat them now!*/
            three[0] = myID;
            three[1] = GetMV(GetMV(passengerTicketsSeatNumber[myID],-1),-1);
            three[2] = GetMV(GetMV(passengerTicketsHolder[myID],-1),-1);
            Print("Coach Attendant %d of Train 0 gives seat number %d to Passenger %d\n",sizeof("Coach Attendant %d of Train 0 gives seat number %d to Passenger %d\n"),three,3);
            SetMV(coachAttendantFirstLineLength[myID], GetMV(coachAttendantFirstLineLength[myID],-1) - 1,-1);
            ServerRelease(CAWaitingForPorterLock[myID],-1);
            ServerAcquire(boardingPassengersCountLock,-1);
            SetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)], GetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)],-1) + 1,-1);
            ServerRelease(boardingPassengersCountLock,-1);
            if(GetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)],-1) == GetMV(passengersWaitingToBoard[GetMV(currentStop,-1)],-1)) {
                ServerAcquire(waitingForBoardingCompletionLock,-1);
                /*printf("CA%d finds that all passengers have boarded for this stop and is telling conductor\n",myID);*/
                ServerSignal(waitingForBoardingCompletion, waitingForBoardingCompletionLock,-1);
                ServerRelease(waitingForBoardingCompletionLock,-1);
            }
        }
        else if(GetMV(coachAttendantRegularLineLength[myID],-1) != 0) {
            ServerAcquire(coachAttendantLineLock[myID],-1);
            /*printf("coachAttendant%d has %d people in reg line\n and is signalling the first to come up\n", myID, coachAttendantRegularLineLength[myID]);*/
            /*I process a reg. passenger in line*/
            ServerSignal(coachAttendantRegularLine[myID], coachAttendantLineLock[myID],-1);
            /*wait for a passenger to hand over their ticket*/
            ServerWait(coachAttendantLine[myID], coachAttendantLineLock[myID],-1);
            ServerAcquire(getSeatLock[myID],-1);
            ServerRelease(coachAttendantLineLock[myID],-1);
            /*find a seat for this passenger*/
            ServerAcquire(globalSeatLock,-1);
            SetMV(GetMV(passengerTicketsSeatNumber[myID],-1), GetMV(nextSeat,-1),-1);
            /*side effect seat is incremented after print*/
            three[0] = myID;
            three[1] = GetMV(nextSeat,-1);
            three[2] = GetMV(GetMV(passengerTicketsHolder[myID],-1),-1);
            Print("Coach Attendant %d of Train 0 gives seat number %d to Passenger %d\n",sizeof("Coach Attendant %d of Train 0 gives seat number %d to Passenger %d\n"),three,3);
            SetMV(nextSeat, GetMV(nextSeat,-1) + 1,-1);
            /*/decrement my line length*/
            SetMV(coachAttendantRegularLineLength[myID], GetMV(coachAttendantRegularLineLength[myID],-1) - 1,-1);
            ServerRelease(globalSeatLock,-1);
            ServerSignal(getSeat[myID], getSeatLock[myID],-1);
            ServerRelease(getSeatLock[myID],-1);
            ServerAcquire(boardingPassengersCountLock,-1);
            SetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)], GetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)],-1) + 1,-1);
            ServerRelease(boardingPassengersCountLock,-1);
            if(GetMV(passengersBoardedAtThisStop[GetMV(currentStop,-1)],-1) == GetMV(passengersWaitingToBoard[GetMV(currentStop,-1)],-1)) {
                ServerAcquire(waitingForBoardingCompletionLock,-1);
                /*printf("CA%d finds that all passengers have boarded for this stop and is telling conductor\n",myID);*/
                ServerSignal(waitingForBoardingCompletion, waitingForBoardingCompletionLock,-1);
                ServerRelease(waitingForBoardingCompletionLock,-1);
            }

        }
        else if(GetMV(coachAttendantFoodLineLength[myID],-1) != 0) {
            /*do food orders*/
            ServerAcquire(coachAttendantLineLock[myID],-1);
            /*printf("coachAttendant%d has %d people in food line\n and is signalling the first to come up\n", myID, coachAttendantFoodLineLength[myID]);*/
            ServerSignal(coachAttendantFoodLine[myID], coachAttendantLineLock[myID],-1);

            ServerAcquire(coachAttendantGetFoodLock[myID],-1);
            ServerRelease(coachAttendantLineLock[myID],-1);

            /*validate food orders until what they want is in stock*/
            do {
                ServerWait(coachAttendantGetFood[myID], coachAttendantGetFoodLock[myID],-1);
                ServerAcquire(globalFoodLock,-1);

                if(GetMV(foodStock[GetMV(GetMV(coachAttendantOrderFoodOrder[myID],-1),-1)],-1) == 0) {
                    SetMV(GetMV(coachAttendantOrderValidFoodOrder[myID],-1), false,-1);
                }
                else {
                    SetMV(GetMV(coachAttendantOrderValidFoodOrder[myID],-1), true,-1);
                    SetMV(foodStock[GetMV(GetMV(coachAttendantOrderFoodOrder[myID],-1),-1)], GetMV(foodStock[GetMV(GetMV(coachAttendantOrderFoodOrder[myID],-1),-1)],-1) - 1,-1);
                }
                ServerRelease(globalFoodLock,-1);

                ServerSignal(coachAttendantGetFood[myID], coachAttendantGetFoodLock[myID],-1);
            } while (!GetMV(GetMV(coachAttendantOrderValidFoodOrder[myID],-1),-1));
            two[0] = myID;
            two[1] = GetMV(GetMV(coachAttendantOrderHolder[myID],-1),-1);
            Print("Coach Attendant %d of Train 0 takes food order of 1st class Passenger %d\n",sizeof("Coach Attendant %d of Train 0 takes food order of 1st class Passenger %d\n"),two,2);
            for(i = 0; i < CHEFS; i += 1) {
                ServerAcquire(chefLock[i],-1);
                if(GetMV(chefOnBreak[i],-1)) {
                    SetMV(chefOnBreak[i], false,-1);
                    myChef = i;
                    /*my chef was waiting for work, so i come up and signal him*/
                    ServerSignal(chef[myChef], chefLock[myChef],-1);
                    break;
                }
                else {
                    ServerRelease(chefLock[i],-1);
                }
            }
            /*No chef was available, so I have to get in line*/
            if(myChef==-1) {
                myChef = 0;
                ServerAcquire(chefLock[0],-1);
                shortestLength = GetMV(chefFirstClassOrdersLeft[0],-1);
                for(i = 1; i < CHEFS; i += 1) {
                    ServerAcquire(chefLock[i],-1);
                    if(GetMV(chefFirstClassOrdersLeft[i],-1) < shortestLength) {
                        shortestLength = GetMV(chefFirstClassOrdersLeft[i],-1);
                        ServerRelease(chefLock[myChef],-1);
                        myChef = i;
                    }
                    else {
                        ServerRelease(chefLock[i],-1);
                    }
                }
                ServerSignal(chef[myChef], chefLock[myChef],-1); /*QUESTIONABLE*/
            }
            /*now we definitely have a chef that will eventually want to see our order*/
            /*printf("CA%d acquires chef%d's lock\n",myID,myChef);*/
            ServerAcquire(chefOrdersLineLock[myChef],-1);
            SetMV(chefFirstClassOrdersLeft[myChef], GetMV(chefFirstClassOrdersLeft[myChef],-1) + 1,-1);
            ServerRelease(chefLock[myChef],-1);
            /*wait for the chef to cook my food*/
            /*printf("CA%d is waiting on chef%d to cook\n",myID,myChef);*/
            ServerWait(chefFirstClassOrdersLine[myChef], chefOrdersLineLock[myChef],-1);
            ServerRelease(chefOrdersLineLock[myChef],-1);
            three[0] = myID;
            three[1] = myChef;
            three[2] = GetMV(GetMV(coachAttendantOrderHolder[myID],-1),-1);
            Print("Coach Attendant %d of Train 0 takes food prepared by Chef %d to the 1st class Passenger %d\n", sizeof("Coach Attendant %d of Train 0 takes food prepared by Chef %d to the 1st class Passenger %d\n"),three,3);
            /*printf("CA%d has delivered the food from the chef, and is waiting for a tip\n",myID);*/
            /*wait for your tip*/
            ServerWait(coachAttendantGetFood[myID], coachAttendantGetFoodLock[myID],-1);
            /*I got my tip and I'm done with this guy*/
            SetMV(coachAttendantFoodLineLength[myID], GetMV(coachAttendantFoodLineLength[myID],-1) - 1,-1);
            ServerRelease(coachAttendantGetFoodLock[myID],-1);
        }
        else {
            /*printf("coachAttendant%d has nothing requiring his immediate attention\n", myID);*/
            SetMV(coachAttendantAvailable[myID], true,-1);
            ServerWait(coachAttendant[myID], coachAttendantLock[myID],-1);
        }
        /*FOR NOW coachAttendantLock[myID]->ServerRelease();*/
    }
} /*end of CoachAttendant*/
Exemplo n.º 24
0
int main(){
	
	int departmentOfChoice,goodsFlag=-1,mySalesID,i,goodsIndex,mvValue1,mvValue2,mvValue3;
	
	initialize();
	
	/* Retrieving index */
	AcquireLock(goodsIndexLock);
	Print("\nInside goodsloader"); 
	goodsIndex=GetMV(globalGoodsIndex);
	SetMV(globalGoodsIndex,goodsIndex+1);
	ReleaseLock(goodsIndexLock);
	
	while(1){
		AcquireLock(salesGoodsLock);
		AcquireLock(goodsloaderLock[goodsIndex]);
		if(GetMV(goodsStatus[goodsIndex])==2){ /* Salesman signalled to restock */
			ReleaseLock(salesGoodsLock);
			SignalCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
			WaitCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
			AcquireLock(storeroomLock);
			mvValue1 = GetMV(itemToBeRestocked_FromSalesmanToGoodsloader[goodsIndex]);
			AcquireLock(itemLock[mvValue1]);
			departmentOfChoice=mvValue1/10;
			mvValue2=GetMV(salesGoodsId[goodsIndex]);
			if(departmentOfChoice==0)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [0] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==1)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [1] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==2)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [2] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==3)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [3] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [4] to restock item%d",goodsIndex,mvValue2,mvValue1);
			
			Print2("\nGoodsloader [%d] is in the StockRoom and got item%d",goodsIndex,mvValue1);
			
			
			SetMV(quantityOnShelf[mvValue1],10);
			Print3("\nGoodsloader [%d] restocked item%d in Department [%d]",goodsIndex,mvValue1,departmentOfChoice);
			Print1("\nGoodsloader [%d] leaves StockRoom",goodsIndex);
			ReleaseLock(storeroomLock);
			ReleaseLock(itemLock[mvValue1]);
			AcquireLock(waitingLock[departmentOfChoice]);
			mvValue3 = GetMV(goodsWaitingLineCount[departmentOfChoice]);
			SetMV(goodsWaitingLineCount[departmentOfChoice],++mvValue3);
			mvValue2=GetMV(salesGoodsId[goodsIndex]);
			AcquireLock(salesmanLock[departmentOfChoice][mvValue2]);
			SignalCV(salesmanLock[departmentOfChoice][mvValue2],salesWaitingCV[departmentOfChoice][mvValue2]);
			ReleaseLock(salesmanLock[departmentOfChoice][mvValue2]);
			WaitCV(waitingLock[departmentOfChoice],goodsWaitingCV[departmentOfChoice]);
			
			mySalesID=-1;
			for(i=0;i<salesmanCount;i++){
				if(GetMV(salesStatus[departmentOfChoice][i]) == 5){
					mySalesID=i;
					SetMV(salesStatus[departmentOfChoice][mySalesID],1);
					break;
				}
			}
			AcquireLock(salesmanLock[departmentOfChoice][mySalesID]);
			ReleaseLock(waitingLock[departmentOfChoice]);
			mvValue1 = GetMV(itemToBeRestocked_FromSalesmanToGoodsloader[goodsIndex]);
			SetMV(restockedItem[departmentOfChoice][mySalesID],mvValue1);
			SetMV(restockingGoodsloader[departmentOfChoice][mySalesID],goodsIndex);
			SignalCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			WaitCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			SignalCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			ReleaseLock(salesmanLock[departmentOfChoice][mySalesID]);
		}
		else{
			ReleaseLock(salesGoodsLock);
		}
		AcquireLock(trolleyEndLock);		
		if(GetMV(trolleyEndCount)>0){ /* If trolley is present at the exit, replace the trolley */
			mvValue1 = GetMV(trolleyEndCount);
			SetMV(trolleyEndCount,--mvValue1);
			ReleaseLock(trolleyEndLock);
			AcquireLock(trolleyFirstLock);
			mvValue1 = GetMV(trolleyFirstCount);
			SetMV(trolleyFirstCount,++mvValue1);
			Print1("\nGoodsloader [%d] replaced the trolley",goodsIndex);
			mvValue1 = GetMV(trolleyWaitingCount);
			if(mvValue1>0){
				SetMV(trolleyWaitingCount,--mvValue1);
				SignalCV(trolleyFirstLock,trolleyWaitingCV);
			}
			ReleaseLock(trolleyFirstLock);
		}
		else{
			ReleaseLock(trolleyEndLock);
		}
		AcquireLock(salesGoodsLock);	
		SetMV(goodsStatus[goodsIndex],0);
		ReleaseLock(salesGoodsLock);
		
		Print1("\nGoodsloader [%d] is waiting for orders to restock",goodsIndex);
		WaitCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
		ReleaseLock(goodsloaderLock[goodsIndex]);
	}
	Exit(0);
}
Exemplo n.º 25
0
int main() {
	/* ------------------Local data -------------------------*/
	int myIndex;

	int initIndex = CreateMV("PicIndex", sizeof("PicIndex"), 1, 0x9999);
	int initIndexLock = ServerCreateLock("PicIndexLock", sizeof("PicIndexLock"), 1);

	int mySSN;
	int i;
	int cType = 0;

	enum BOOLEAN loop = true;
	int shutdown = CreateMV("shutdown", sizeof("shutdown"), 1, 0x9999);
	/* -----------------Shared Data------------------------*/

	/* Number of customers/senators in office and thier locks */
	int officeSenator = CreateMV("officeSenator", sizeof("officeSenator"), 1, 0x9999);
	int officeCustomer = CreateMV("officeCustomer", sizeof("officeCustomer"), 1, 0x9999);	
	int senatorLock = ServerCreateLock("senatorLock", sizeof("senatorLock"), 1);
	int customerLock = ServerCreateLock("customerLock", sizeof("customerLock"), 1);

	/*Locks, conditions, and data used for waitinng */
	int clerkWaitLock = ServerCreateLock("clerkWaitLock", sizeof("clerkWaitLock"), 1);
	int clerkWaitCV = ServerCreateCV("clerkWaitCV", sizeof("clerkWaitCV"), 1);
	int numPicWait = CreateMV("numPicWait", sizeof("numPicWait"), 1, 0x9999);

	/* Reg and priv line lengths, line locks, and reg and priv line conditions. */
	int regPCLineLength = CreateMV("regPCLineLength", sizeof("regPCLineLength"), 1, 0x9999);
	int privPCLineLength = CreateMV("privPCLineLength", sizeof("privPCLineLength"), 1, 0x9999);
	int acpcLineLock = ServerCreateLock("acpcLineLock", sizeof("acpcLineLock"), 1);
	int regPCLineCV = ServerCreateCV("regPCLineCV", sizeof("regPCLineCV"), 1);
	int privPCLineCV = ServerCreateCV("privPCLineCV", sizeof("privPCLineCV"), 1);


	/* Individual clerk locks, conditions, data, data booleans, and states */
	int picLock = ServerCreateLock("picLock", sizeof("picLock"), NUM_CLERKS);
	int picCV = ServerCreateCV("picCV", sizeof("picCV"), NUM_CLERKS);
	int picDataBool = CreateMV("picDataBool", sizeof("picDataBool"), NUM_CLERKS, 0x9999);
	int picState = CreateMV("picState", sizeof("picState"), NUM_CLERKS, 0x9999);
	int picData = CreateMV("picData", sizeof("picData"), NUM_CLERKS, 0x9999);

	/* Money data and locks for each clerk type */
	int picMoney = CreateMV("picMoney", sizeof("picMoney"), 1, 0x9999);
	int picMoneyLock = ServerCreateLock("picMoneyLock", sizeof("picMoneyLock"), 1);

	/* Individual customer's file state, type, and its lock */
	int fileLock = ServerCreateLock("fileLock", sizeof("fileLock"), NUM_CUSTOMERS + NUM_SENATORS);
	int fileState = CreateMV("fileState", sizeof("fileState"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);
	int fileType = CreateMV("fileType", sizeof("fileType"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);

	traceLock = ServerCreateLock("traceLock", sizeof("traceLock"), 1);
	
	/* initializes myIndex */
	ServerAcquire(initIndexLock, 0);
	myIndex = GetMV(initIndex, 0);
	SetMV(initIndex, 0, GetMV(initIndex, 0) + 1);
	ServerRelease(initIndexLock, 0);
	
	SetMV(picState, myIndex, 1);

	/* --------------------BEGIN PICCLERK STUFF----------------*/
	while(loop == true){
		if (GetMV(shutdown, 0) == 1) {
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pic", myIndex, 0x00, 0, "Shutting down.\n");
			ServerRelease(traceLock, 0);
			Exit(0);
		}
		ServerAcquire(senatorLock, 0);
		ServerAcquire(customerLock, 0);
		if (GetMV(officeSenator, 0) > 0 && GetMV(officeCustomer, 0) > 0) {
			ServerRelease(senatorLock, 0);
			ServerRelease(customerLock, 0);
			
			ServerAcquire(clerkWaitLock, 0);
			ServerWait(clerkWaitCV, 0, clerkWaitLock, 0);
			ServerRelease(clerkWaitLock, 0);
		}
		else{
			ServerRelease(senatorLock, 0);
			ServerRelease(customerLock, 0);
		}

		ServerAcquire(acpcLineLock, 0);

		/* Check for the privileged customer line first
		* If there are privileged customers, do PicClerk tasks, then receive $500
		*/
		if(GetMV(privPCLineLength, 0) > 0){
			SetMV(privPCLineLength, 0, GetMV(privPCLineLength, 0)-1);
			
			ServerAcquire(senatorLock, 0);
			SetMV(numPicWait, 0, GetMV(numPicWait, 0)+1);		/* Shows customer that one clerk is waiting */
			ServerRelease(senatorLock, 0);

			ServerAcquire(picLock, myIndex);
			SetMV(picState, myIndex, 0); /* 0 = AVAILABLE */
			ServerSignal(privPCLineCV, 0, acpcLineLock, 0);
			ServerRelease(acpcLineLock, 0);
			
			ServerWait(picCV, myIndex, picLock, myIndex);	/* Waits for the next customer */

			mySSN = GetMV(picData, myIndex);
			/* check the customer type */
			ServerAcquire(fileLock, mySSN);
			if (GetMV(fileType, mySSN) == 0) { /* 0 = CUSTOMER */
				cType = 0;
			} else {
				cType = 1;	/* 1 = SENATOR */
			}

			if(GetMV(fileState, mySSN) == 0){	/* 0 = NONE */
				SetMV(fileState, mySSN, 1);/* 1 = PICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else if(GetMV(fileState, mySSN) == 2 ){ /* 2 = APPDONE */
				SetMV(fileState, mySSN, 3); /* 3 = APPPICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else{
				ServerAcquire(traceLock, 0);
				ClerkTrace("Pic", myIndex, "Cust", mySSN,
					"ERROR: Customer does not have either an application or no application. What are you doing here?\n");
				ServerRelease(traceLock, 0);
				ServerRelease(fileLock, mySSN);
			}

			ServerAcquire(traceLock, 0);
			if (cType == 0) {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Takes picture of Customer.\n");
			} else {
				ClerkTrace("Pic", myIndex, "Sen", mySSN, "Takes picture of Senator.\n");
			}
			ServerRelease(traceLock, 0);

			/* yield to take picture
			* print statement: "Taking picture"
			* picCV->Signal then picCV->Wait to
			* show customer picture
			*/
			while(GetMV(picDataBool, myIndex) == 0){ /* 0 = false */
				for(i = 0; i < 4; i++){
					Yield();
				}
				ServerSignal(picCV, myIndex, picLock, myIndex);	
				ServerWait(picCV, myIndex, picLock, myIndex);	/* Shows the customer the picture */

				if(GetMV(picDataBool, myIndex) == 0){
					ServerAcquire(traceLock, 0);
					if (cType == 0) {
						ClerkTrace("Pic", myIndex, "Cust", mySSN, "Takes picture of Customer again.\n");

					} else {
						ClerkTrace("Pic", myIndex, "Sen", mySSN, "Takes picture of Senator again.\n");
					}
					ServerRelease(traceLock, 0);
				}
			}


			/* file picture using
			* current thread yield
			
			for(i = 0; i < 20; i++){
				Yield();
			}*/
			ServerAcquire(traceLock, 0);
			if (cType == 0) {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Informs Customer that the picture has been completed.\n");
			} else {
				ClerkTrace("Pic", myIndex, "Sen", mySSN, "Informs Senator that the picture has been completed.\n");
			}
			ServerRelease(traceLock, 0);

			/* signal customer awake */
			ServerAcquire(picMoneyLock, 0);
			SetMV(picMoney, 0, GetMV(picMoney, 0) + 500);

			ServerAcquire(traceLock, 0);
			if (cType == 0) {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Accepts $500 from Customer.\n");
			} else {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Accepts $500 from Senator.\n");
			}
			ServerRelease(traceLock, 0);

			ServerRelease(picMoneyLock, 0);

			SetMV(picDataBool, myIndex, 0);
			ServerSignal(picCV, myIndex, picLock, myIndex); /* signal customer awake */
			ServerRelease(picLock, myIndex); /* release clerk lock */
		}
		/* Check for regular customer line next
		* If there are regular customers, do PicClerk tasks
		*/
		else if(GetMV(regPCLineLength,0) > 0){
			SetMV(regPCLineLength, 0, GetMV(regPCLineLength, 0)-1);

			ServerAcquire(senatorLock, 0);
			SetMV(numPicWait, 0, GetMV(numPicWait, 0)+1);	/* shows customer that one clerk is waiting */
			ServerRelease(senatorLock, 0);

			ServerAcquire(picLock, myIndex);
			SetMV(picState, myIndex, 0); /* 0  = AVAILABLE */
			ServerSignal(regPCLineCV, 0, acpcLineLock, 0);
			ServerRelease(acpcLineLock, 0);
			
			ServerWait(picCV, myIndex, picLock, myIndex);	/* Waits for the next customer */

			mySSN = GetMV(picData, myIndex);
			/* check the customer type */
			ServerAcquire(fileLock, mySSN);
			if (GetMV(fileType, mySSN) == 0) {
				cType = 0;
			} else {
				cType = 1;
			}

			if(GetMV(fileState, mySSN) == 0){	/* 0 = NONE */
				SetMV(fileState, mySSN, 1);/* 1 = PICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else if(GetMV(fileState, mySSN) == 2 ){ /* 2 = APPDONE */
				SetMV(fileState, mySSN, 3); /* 3 = APPPICDONE */
				ServerRelease(fileLock, mySSN);
			}
			else{
				ServerAcquire(traceLock, 0);
				ClerkTrace("Pic", myIndex, "Cust", mySSN,
					"ERROR: Customer does not have either an application or no application. What are you doing here?\n");
				ServerRelease(traceLock, 0);
				ServerRelease(fileLock, mySSN);
			}

			ServerAcquire(traceLock, 0);
			if (cType == 0) {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Takes picture of Customer.\n");
			} else {
				ClerkTrace("Pic", myIndex, "Sen", mySSN, "Takes picture of Senator.\n");
			}
			ServerRelease(traceLock, 0);
			
				/* yield to take picture
				* print statement: "Taking picture"
				* picCV->Signal then picCV->Wait to
				* show customer picture
				*/
			while(GetMV(picDataBool, myIndex) == 0){
				for(i = 0; i < 4; i++){
					Yield();
				}
				ServerSignal(picCV, myIndex, picLock, myIndex);	
				ServerWait(picCV, myIndex, picLock, myIndex);	/* Shows the customer the picture */

				if(GetMV(picDataBool, myIndex) == 0){
					ServerAcquire(traceLock, 0);
					if (cType == 0) {
						ClerkTrace("Pic", myIndex, "Cust", mySSN, "Takes picture of Customer again.\n");
					} else {
						ClerkTrace("Pic", myIndex, "Sen", mySSN, "Takes picture of Senator again.\n");
					}
					ServerRelease(traceLock, 0);
				}
			}


			/* file picture using
			* current thread yield
			
			for(i = 0; i < 20; i++){
				Yield();
			}*/
			ServerAcquire(traceLock, 0);
			if (cType == 0) {
				ClerkTrace("Pic", myIndex, "Cust", mySSN, "Informs Customer that the picture has been completed.\n");
			} else {
				ClerkTrace("Pic", myIndex, "Sen", mySSN, "Informs Senator that the picture has been completed.\n");
			}
			ServerRelease(traceLock, 0);

			SetMV(picDataBool, myIndex, 0);
			ServerSignal(picCV, myIndex, picLock, myIndex); /* signal customer awake */
			ServerRelease(picLock, myIndex); /* release clerk lock */
		}		
		/* If there are neither privileged or regular customers, go on break */
		else{
			ServerRelease(acpcLineLock, 0);
			ServerAcquire(picLock, myIndex);
			SetMV(picState, myIndex, 2); /* 2 = BREAK */
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pic", myIndex, 0x00, 0, "Going on break.\n");
			ServerRelease(traceLock, 0);

			ServerWait(picCV, myIndex, picLock, myIndex);
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pic", myIndex, 0x00, 0, "Returning from break.\n");
			ServerRelease(traceLock, 0);
			
			ServerRelease(picLock, myIndex);
		}
	}
}
int
main()
{
    int lockStatus, cvStatus, mvStatus, mvId, mvValue;

    WriteToConsole("\nCreating a Lock\n\0", -1, -1, -1);

    lockStatus = CreateLock("lock1",5);
    if(lockStatus == -1)
    {
        WriteToConsole("\nError Occurred while creating Lock. Exiting program\n\0", -1, -1, -1);
        Exit(0);
    }
    else
    {
        WriteToConsole("\nLock Created by the server.\n\0", -1, -1, -1);
    }


    WriteToConsole("\nCreating a MV\n\0", -1, -1, -1);

    mvStatus = CreateMV("mv1",3, 5);
    if(mvStatus == -1)
    {
        WriteToConsole("\nError Occurred while creating MV. Exiting program\n\0", -1, -1, -1);
        Exit(0);
    }
    else
    {
        WriteToConsole("\nMV Created by the server.\n\0", -1, -1, -1);
    }


    WriteToConsole("\nAcquiring lock to access Monitor Variable\n\0", -1, -1, -1);

    lockStatus = Acquire("lock1",5);
    if(lockStatus == -1)
    {
        WriteToConsole("\nError Occurred while acquiring Lock. Exiting program\n\0", -1, -1, -1);
        Exit(0);
    }
    else
    {
        WriteToConsole("\nLock acquired by the server.\n\0", -1, -1, -1);
    }

    WriteToConsole("\nSetting MV to 100\n", -1, -1, -1);
    mvStatus = SetMV("mv1",3, 1, 100);
    if(mvStatus == -1)
    {
        WriteToConsole("\nFailed to Set MV. Exiting Program\n\0", -1, -1, -1);
        Exit(0);
    }
    else
    {
        WriteToConsole("\nMV set successfully.\n\0", -1, -1, -1);
    }


    WriteToConsole("\nTrying to fetch value at invalid mv Index : %d\n\0", 21, -1,-1);
    mvValue = GetMV("mv1",3, 20);
    if(mvValue == -1)
    {
        WriteToConsole("\nInvalid mvIndex, Failed to fetch value at given index. Exiting Program\n\0", -1, -1, -1);
        Exit(0);
    }
    else
    {
        WriteToConsole("\nAbnormal Execution\n\0", -1, -1, -1);
    }

    Exit(0);
}
Exemplo n.º 27
0
bool ManagerCheckClose() {
	int i; /* for loops */
	bool doneFlag = true, allHereFlag = true, breakFlag = true;
	for(i = 0; i < NUM_SENATORS; i++) {
		if(GetMV(senatorArrived, i) == 0) {
			doneFlag = false;
			allHereFlag = false;
			break;
		}
		if(GetMV(senatorSocial, i) == 0 ||
		GetMV(senatorPicture, i) == 0 ||
		GetMV(senatorPassport, i) == 0 ||
		GetMV(senatorPaid, i) == 0) {
			doneFlag = false;
			break;
		}
	}
	for(i = 0; i < NUM_CUSTOMERS; i++) {
		if(GetMV(customerArrived, i) == 0) {
			doneFlag = false;
			allHereFlag = false;
			break;
		}
		if(GetMV(customerSocial, i) == 0 ||
		GetMV(customerPicture, i) == 0 ||
		GetMV(customerPassport, i) == 0 ||
		GetMV(customerPaid, i) == 0) {
			doneFlag = false;
			break;
		}
	}
	if(doneFlag == true) {
		Print("Manager is closing the Passport Office.\n", 40);
		return true;
	}
	else if(allHereFlag == true) {
		for(i = 0; i < NUM_APPCLERKS; i++) {
			if(GetMV(appClerkState, i) != 2) {
				breakFlag = false;
				break;
			}
		}
		for(i = 0; i < NUM_PICCLERKS; i++) {
			if(GetMV(picClerkState, i) != 2) {
				breakFlag = false;
				break;
			}
		}
		for(i = 0; i < NUM_PASSCLERKS; i++) {
			if(GetMV(passClerkState, i) != 2) {
				breakFlag = false;
				break;
			}
		}
		for(i = 0; i < NUM_CASHIERS; i++) {
			if(GetMV(cashierState, i) != 2) {
				breakFlag = false;
				break;
			}
		}
		if(breakFlag == true) {
			for(i = 0; i < NUM_APPCLERKS; i++) {
				if(GetMV(appClerkLineCount, i) > 0) {
					Acquire(appClerkMutex[i].clerkLock);
					Acquire(outputLock);
					Print("Manager is waking Application Clerk ", 36);
					PrintInt(i);
					Print(" from break.\n", 13);
					Release(outputLock);
					Signal(appClerkMutex[i].clerkCV, appClerkMutex[i].clerkLock);
					Release(appClerkMutex[i].clerkLock);
				}
			}
			for(i = 0; i < NUM_PICCLERKS; i++) {
				if(GetMV(picClerkLineCount, i) > 0) {
					Acquire(picClerkMutex[i].clerkLock);
					Acquire(outputLock);
					Print("Manager is waking Picture Clerk ", 32);
					PrintInt(i);
					Print(" from break.\n", 13);
					Release(outputLock);
					Signal(picClerkMutex[i].clerkCV, picClerkMutex[i].clerkLock);
					Release(picClerkMutex[i].clerkLock);
				}
			}
			for(i = 0; i < NUM_PASSCLERKS; i++) {
				if(GetMV(passClerkLineCount, i) > 0) {
					Acquire(passClerkMutex[i].clerkLock);
					Acquire(outputLock);
					Print("Manager is waking Passport Clerk ", 33);
					PrintInt(i);
					Print(" from break.\n", 13);
					Release(outputLock);
					Signal(passClerkMutex[i].clerkCV, passClerkMutex[i].clerkLock);
					Release(passClerkMutex[i].clerkLock);
				}
			}
			for(i = 0; i < NUM_CASHIERS; i++) {
				if(GetMV(cashierLineCount, i) > 0) {
					Acquire(cashierMutex[i].clerkLock);
					Acquire(outputLock);
					Print("Manager is waking Cashier ", 26);
					PrintInt(i);
					Print(" from break.\n", 13);
					Release(outputLock);
					Signal(cashierMutex[i].clerkCV, cashierMutex[i].clerkLock);
					Release(cashierMutex[i].clerkLock);
				}
			}
		} 
	}
	return false;
}
Exemplo n.º 28
0
void ManagerCheckLines() {
	int i; /* for loops */
	bool customersGone = true;

	for(i = 0; i < NUM_APPCLERKS; i++) {
		if(GetMV(appClerkState, i) == 2 &&
		(GetMV(appClerkLineCount, i) > 2 || (GetMV(appClerkSenInLine, i) == 1) ||
		(GetMV(appClerkLineCount, i) > 0 && GetMV(senatorFlag, 0) == 1))) {
			Acquire(appClerkMutex[i].clerkLock);
			Acquire(outputLock);
			Print("Manager is waking Application Clerk ", 36);
			PrintInt(i);
			Print(" from break.\n", 13);
			Release(outputLock);
			Signal(appClerkMutex[i].clerkCV, appClerkMutex[i].clerkLock);
			Release(appClerkMutex[i].clerkLock);
		}
		if(GetMV(appClerkState, i) == 1) {
			customersGone = false;
		}
	}
	for(i = 0; i < NUM_PICCLERKS; i++) {
		if(GetMV(picClerkState, i) == 2 &&
		(GetMV(picClerkLineCount, i) > 2 || (GetMV(picClerkSenInLine, i) == 1) ||
		(GetMV(picClerkLineCount, i) > 0 && GetMV(senatorFlag, 0) == 1))) {
			Acquire(picClerkMutex[i].clerkLock);
			Acquire(outputLock);
			Print("Manager is waking Picture Clerk ", 32);
			PrintInt(i);
			Print(" from break.\n", 13);
			Release(outputLock);
			Signal(picClerkMutex[i].clerkCV, picClerkMutex[i].clerkLock);
			Release(picClerkMutex[i].clerkLock);
		}
		if(GetMV(picClerkState, i) == 1) {
			customersGone = false;
		}
	}
	for(i = 0; i < NUM_PASSCLERKS; i++) {
		if(GetMV(passClerkState, i) == 2 &&
		(GetMV(passClerkLineCount, i) > 2 || (GetMV(passClerkSenInLine, i) == 1) ||
		(GetMV(passClerkLineCount, i) > 0 && GetMV(senatorFlag, 0) == 1))) {
			Acquire(passClerkMutex[i].clerkLock);
			Acquire(outputLock);
			Print("Manager is waking Passport Clerk ", 33);
			PrintInt(i);
			Print(" from break.\n", 13);
			Release(outputLock);
			Signal(passClerkMutex[i].clerkCV, passClerkMutex[i].clerkLock);
			Release(passClerkMutex[i].clerkLock);
		}
		if(GetMV(passClerkState, i) == 1) {
			customersGone = false;
		}
	}
	for(i = 0; i < NUM_CASHIERS; i++) {
		if(GetMV(cashierState, i) == 2 &&
		(GetMV(cashierLineCount, i) > 2 || (GetMV(cashierSenInLine, i) == 1) ||
		(GetMV(cashierLineCount, i) > 0 && GetMV(senatorFlag, 0) == 1))) {
			Acquire(cashierMutex[i].clerkLock);
			Acquire(outputLock);
			Print("Manager is waking Cashier ", 26);
			PrintInt(i);
			Print(" from break.\n", 13);
			Release(outputLock);
			Signal(cashierMutex[i].clerkCV, cashierMutex[i].clerkLock);
			Release(cashierMutex[i].clerkLock);
		}
		if(GetMV(cashierState, i) == 1) {
			customersGone = false;
		}
	}
	for(i = 0; i < NUM_CUSTOMERS; i++) {
		if(GetMV(customerArrived, i) == 1 && GetMV(customerOutside, i) == 0) {
			customersGone = false;
		}
	}
	if(customersGone == true && GetMV(senatorFlag, 0) == 1) {
		Acquire(outsideLock);
		/* tell the senator that all customers are outside */
		Signal(customerCV, outsideLock);
		Release(outsideLock);
	}
}
Exemplo n.º 29
0
void Customer() {

	/* Get the Index for this Customer before starting the order*/
	Acquire(nextCustomerIndexLock);	
	myIndex = GetMV(nextCustomerIndexMVi);
	tempV = myIndex + 1;
	SetMV(nextCustomerIndexMVi, tempV);
	
	Release(nextCustomerIndexLock);
	
	
	for(index = 1; index <= OT_COUNT; index++)
	{
		Acquire(custLineLock);
		
		orderTakerStatus = GetMV(orderTakerStatusMVi[index]);
		
		Print_2Arg("OT status of OT %d is %d\n", index, orderTakerStatus);
		
		if(orderTakerStatus == OT_FREE)
		{	
			Print_1Arg("Found OT %d FREE\n", index);
			orderTakerStatus = OT_BUSY;
			SetMV(orderTakerStatusMVi[index], orderTakerStatus);
			
			Print_3Arg("Cust Signalling FREE OT %d with CV = %d and Lock = %d\n", index, custLineCV, custLineLock);
			Signal(custLineCV, custLineLock);
	
			Acquire(customerDataLock);
			
			myOT = index;
			Print_1Arg("Found OT %d FREE I made him BUSY\n\n", index);
			SetMV(myOTMVi[myIndex], index);
			Release(customerDataLock);
			break;
		}
		myOT = -1;
		
	}
	
	/*If no Order Takers are free */
	/* - increment the linelength monitor variable */
	/* - then wait in the customer waiting line */
	Acquire(customerDataLock);
			
	if(myOT == -1){
		
		Print_1Arg("Since no OT available, Cust %d has myOT = myOT\n", myIndex);
		Release(customerDataLock);
	
		tempV = GetMV(custLineLengthMVi);	
		tempV++;
		SetMV(custLineLengthMVi, tempV);
		
		Print_1Arg("Increased the linelength to = %d", tempV);
		
		Print_3Arg("Customer %d going on wait on CV %d and Lock %d\n\n", myIndex, custLineCV, custLineLock);
		Wait(custLineCV, custLineLock);
		Print_1Arg("Customer %d is out of wait \n\n", myIndex);

	}else	
		Release(customerDataLock);
		
	/* Customer is here because he received a signal from the Order Taker */
	/* - customer has also acquired the custLineLock */

	/* Check for the Order Taker who is WAITING */
	/* - make him as the Order Taker for the current customer */
	/* - Make that Order Taker status as BUSY */
	Acquire(customerDataLock);
	if(GetMV(myOTMVi[myIndex]) == -1){
	
		Print_1Arg("Cust %d searching for the waiting OT\n", myIndex);
		Release(customerDataLock);
		for(index = 1; index <= OT_COUNT; index++){ 
	
			orderTakerStatus = GetMV(orderTakerStatusMVi[index]);
			
			Print_2Arg("OT status (in waiting ot loop) of OT %d is %d\n", index, orderTakerStatus);
			
			if(orderTakerStatus == OT_WAIT){
				
				Print_1Arg("Cust found waiting OT %d\n", index);
				orderTakerStatus = OT_BUSY;
				SetMV(orderTakerStatusMVi[index], orderTakerStatus);
			
				Acquire(customerDataLock);
				
				SetMV(myOTMVi[myIndex], index); 			
				Release(customerDataLock);	
				
				myOT = index;
				break;
				
			}else
				Release(custLineLock);
		}
	}else	
		Release(customerDataLock);	

	inOT = myOT;
	
	Release(custLineLock);
	
	Acquire(custLineLock);
	Acquire(orderTakerLock[inOT]);
	
	/* Send a signal to the Order Taker which indicates that the customer */
	/* is ready to give the order */
	/* $ */
	Print_2Arg("Customer %d signalling the OT %d\n", myIndex, inOT);
	
	Release(custLineLock);
	Signal(orderTakerCV[inOT], orderTakerLock[inOT]);
	
    /* Customer goes on wait to receive a signal from the Order Taker */
	/* which indicates Order Taker is ready to take the order */
	Wait(orderTakerCV[inOT], orderTakerLock[inOT]);
	
	Release(orderTakerLock[inOT]);
	
	/* Customer received a signal from the Order Taker to place an order */
	/* Order is randomly generated and is stored in the customer database */
	Print_2Arg("Customer %d is giving order to OrderTaker %d\n$", myIndex, inOT);
	
	Acquire(customerDataLock);
		
	custDataSixBurger = GetMV(sixBurgerincustDBMVi[myIndex]);
	custDataThreeBurger = GetMV(threeBurgerincustDBMVi[myIndex]);
	custDataVegBurger = GetMV(vegBurgerincustDBMVi[myIndex]);
	custDataFries = GetMV(friesincustDBMVi[myIndex]);
	custDataSoda = GetMV(sodaincustDBMVi[myIndex]);
	custDataDineType = GetMV(dineTypeincustDBMVi[myIndex]);	
	
	custDataDineType = SetMV(dineTypeincustDBMVi[myIndex], 1); 
	custDataDineType = GetMV(dineTypeincustDBMVi[myIndex]);	
	
	
	Print_1Arg("\n\nCUST %d ORDER \n\n\n\n", myIndex);
	if(custDataSixBurger == 1){
		Print_1Arg("Customer %d is ordering 6-dollar burger\n$", myIndex);
	}else{
		Print_1Arg("Customer %d is not ordering 6-dollar burger\n$", myIndex);
	}
	if(custDataThreeBurger == 1){
		Print_1Arg("Customer %d is ordering 3-dollar burger\n$", myIndex);
	}else{
		Print_1Arg("Customer %d is not ordering 3-dollar burger\n$", myIndex);
	}
	if(custDataVegBurger == 1){
		Print_1Arg("Customer %d is ordering veggie burger\n$", myIndex);
	}else{
		Print_1Arg("Customer %d is not ordering veggie burger\n$", myIndex);
	}
	if(custDataFries == 1){
		Print_1Arg("Customer %d is ordering french fries\n$", myIndex);
	}else{
		Print_1Arg("Customer %d is ordering french fries\n$", myIndex);
	}
	if(custDataSoda == 1){
		Print_1Arg("Customer %d is ordering soda\n$", myIndex);
	}else{
		Print_1Arg("Customer %d is not ordering soda\n$", myIndex);
	}
	if(custDataDineType == 1){
		Print_1Arg("Customer %d chooses to to-go the food\n$", myIndex);
	}else{
		Print_1Arg("Customer %d chooses to eat-in the food\n$", myIndex);
	}
	Release(customerDataLock);
	Acquire(orderTakerLock[inOT]);
	
	
	Print_2Arg("Customer %d signalling the OT %d after placing order\n", myIndex, inOT);
	Signal(orderTakerCV[inOT],orderTakerLock[inOT]);
	
	/* wait for the Order Taker to reply once he checks the food availability */
	Wait(orderTakerCV[inOT],orderTakerLock[inOT]);
	
	/* Received a signal from Order Taker which indicates that the order is */
	/* processed and its time to pay money */
	
	/* Send a signal to the Order Taker which indicates that the customer */
	/* has payed the bill amount */ 
	Signal(orderTakerCV[inOT],orderTakerLock[inOT]);
	
	/* Wait for the Order Taker to acknowledge the payment being done */ 
	/* successfully */
	Wait(orderTakerCV[inOT], orderTakerLock[inOT]);
	Release(orderTakerLock[inOT]);

	/* Received a signal from the Order Taker to indicate that the payment */
	/* was completed and the customer can move to the next stage */ 

	/* If to-go then check if the order is bagged right away */
	/* else go and wait for a broadcast of the tokenNo */
	Print_1Arg("custDataDineType = %d\n", custDataDineType);
	
	if(custDataDineType == 1)
	{               
		
		
		/* if to-go and if the order is delivered then the customer takes the */
		/* bag and just leaves the restaurant */
		/* release the Order Taker Lock */
		Acquire(customerDataLock);
		
		
		custDataDelivered = GetMV(deliveredincustDBMVi[myIndex]);
		if(custDataDelivered == 1)
		{
			Release(customerDataLock);	
			/* OG */
			Print_2Arg("Customer %d receives food from the OrderTaker %d\n$",myIndex, inOT);
			/* Acquire the Lock to update the custer count served the food */
			Acquire(custServedLock);
			
			tempV = GetMV(custServedMVi);
			tempV ++;
			SetMV(custServedMVi, tempV);
			
			
			Print_2Arg("\n\nCUSTOMER %d SERVICED IS %d\n\n$", myIndex, tempV); 
			
			Release(custServedLock);
	
			/* if to-go and if the order is not ready then customer has to wait */
			/* for a broadcast signal from the Order Taker when the order is bagged */
		
			/* Acquire the Lock which is used to match with To-go waiting */
			/* condition variable waitingLock */
		}
		else
		{
			Release(customerDataLock);	
			/* OG */
			Print_3Arg("Togo Customer %d is given token number %d by the OrderTaker %d\n$", myIndex, custDataTokenNo, inOT);
			Acquire(waitToGoLock);
				
			/* Go on wait to receive the broadcast signal from the Order Taker */
			Wait(toGoCV,waitToGoLock);
			Release(waitToGoLock);

			while(1)
			{
				/* Received a Broadcast signal from one of the Order Taker */
				/* - Check if the broadcasted tokenNo is customer's tokenNo */
				Acquire(customerDataLock);
				if(custDataDelivered = 1)
				{
					Release(customerDataLock);
					/* Acquire the Lock to update the custer count served the food */
					Acquire(custServedLock);
					
					tempV = GetMV(custServedMVi);
					tempV ++;
					SetMV(custServedMVi, tempV);
			
					Print_2Arg("\n\nCUSTOMER %d SERVICED IS %d\n\n$", myIndex,tempV); 
					Release(custServedLock);
					
					break;
				}else
				{ 
					Release(customerDataLock);
					/* Go on wait to receive the next broadcast signal from the waiter */
					Acquire(waitToGoLock);
					Wait(toGoCV,waitToGoLock);
					Release(waitToGoLock);
				}	
			}
	    }
	}/* if dineType is eat-in */
	else if(custDataDineType == 0)
	{
				/*  OG */
		Print_3Arg("EatinCustomer %d is given token number %d by the OrderTaker %d\n$", myIndex, custDataTokenNo, inOT); 
		/* Acquire a Lock to add customer onto the queue waiting for */
		/* manager's reply */
		Acquire(managerLock);
		/* Increment the customer waiting count */
					
					
		tempV = GetMV(managerLineLengthMVi);
		tempV ++;
		SetMV(managerLineLengthMVi, tempV);
		
		Print_1Arg("Customer setting manager line length to %d\n", tempV);
		
		
		Print_1Arg("Customer %d going on wait till the manager replies\n", myIndex);
		
		/* go on wait till manager signals with table availability */
		Wait(managerCV,managerLock);

		/* Received a signal from manager */
		/* Customer has acquired the manager lock */
		/* Manager replied saying "restaurant is not full" */
		tableAvailable = GetMV(tableAvailableMVi);
		
		if(tableAvailable == 1)
		{		
			/* OG */
			Print_1Arg("Customer %d is informed by the Manager-the restaurant is not full\n$", myIndex);
			
			tableAvailable = 0;
			SetMV(tableAvailableMVi, tableAvailable);
			custDataTableNo = GetMV(tableNoincustDBMVi[myIndex]);

			Release(managerLock);			
			for(index = 0; index < TABLE_COUNT; index++)
			{
				Acquire(tablesDataLock);
				
				tables = GetMV(tablesMVi[index]);
				
				if(tables == 0)
				{
					/* make the first table which is free as the customer */
					/* table */
					
					custDataTokenNo = GetMV(tokenNoincustDBMVi[myIndex]);
					tables = custDataTokenNo;
					SetMV(tablesMVi[index],tables);
					
					Release(tablesDataLock);
					Acquire(customerDataLock);
					
					custDataTableNo = index;
					SetMV(tableNoincustDBMVi[myIndex], custDataTableNo);
								
					Release(customerDataLock);
					/* @ */
					/* Print_3Arg("table %d is taken by custom %d tokenNo %d\n$", index, myIndex, tables);	 */
					break;
				}else
					Release(tablesDataLock);
			}

			/* if a table is found then go sit and wait for food */
			if(custDataTableNo != 0)
			{	
				/* OG	*/
				Print_2Arg("customer %d is seated at table number %d\n$",myIndex, custDataTableNo);
					
				/* Before releasing the tablesDataLock Acquire the */
				/* eatInFoodWaitingLock to ensure that this customer is the */ 
				/* next one to join the queue waiting for the food to be bagged */

				/* customer goes on wait to receive a signal from the waiter */
				/* after the tokenNo is validated */
				while(1)
				{
					Acquire(eatInFoodWaitingLock);
					Wait(eatInFoodWaitingCV,eatInFoodWaitingLock);	
					Release(eatInFoodWaitingLock);
		 			/* upon receiving the signal check if order is delivered */
					Acquire(customerDataLock);
					
					custDataDelivered = GetMV(deliveredincustDBMVi[myIndex]);
					
					if(custDataDelivered == 1)
					{
						/* if delivered then the customer eats the food and */
						/* leaves the restaurant */
						Release(customerDataLock);
						Acquire(tablesDataLock);			
						tables = 0;
						
						SetMV(tablesMVi[custDataTableNo], tables);
						
						Release(tablesDataLock);
						Acquire(eatInWaitingLock);
						Signal(eatInWaitingCV,eatInWaitingLock);
						Release(eatInWaitingLock);	
						/* Acquire the Lock to update the custer count served the food */
						Acquire(custServedLock);
						
						tempV = GetMV(custServedMVi);
						tempV ++;
						SetMV(custServedMVi, tempV);
						/* @ */
						/* Print_2Arg("\n\nCUSTOMER %d SERVICED IS %d\n\n$", myIndex, custServed); */
						Release(custServedLock);
						/* Print_1Arg("Customer %d calling E---X---I---T\n$", myIndex); */
						Exit(0);	
					}
					Release(customerDataLock);
				}
			}

		}
		else if(tableAvailable == 0)
		{
			Release(managerLock);
			/* OG */
			Print_1Arg("Customer %d is informed by the Manager-the restaurant is full\n$", myIndex);
			/* if restaurant is full and no table is available where the */
			/* customer can sit */
				
			/* Acquire the eat-in table waiting lock before releasing the */
			/* tablesDataLock so that customer wont get context switched */
			/* and he can be the next person to join the waiting queue */
				
			/* Release the tables Data lock	 */
			
			Acquire(eatInWaitingLock);	
			/* OG */
			Print_1Arg("customer %d is waiting to sit on the table\n$", myIndex);
				
			/* Customer goes on wait till he receives a signal from */
			/* a seated customer who received his bag and is leaving */
			/* the restaurant */
			Wait(eatInWaitingCV,eatInWaitingLock);
			Release(eatInWaitingLock);
			/* Received a signal from one of the customer leaving */
			/* Acquire the Lock on the tables data to make the */
			/* freed table number as the current customer table */
			Acquire(tablesDataLock);	
		
			for(index = 0; index < TABLE_COUNT; index++)
			{
				
				tables = GetMV(tablesMVi[index]);
				
				if(tables == 0)
				{
					/* make the first table which is free as the customer */
					/* table */
					
					custDataTokenNo = GetMV(tokenNoincustDBMVi[myIndex]);
					tables = custDataTokenNo;
					SetMV(tablesMVi[index],tables);
					
					Release(tablesDataLock);
					Acquire(customerDataLock);
					
					custDataTableNo = index;
					SetMV(tableNoincustDBMVi[myIndex], custDataTableNo);
					
					Release(customerDataLock);
					Acquire(tablesDataLock);
					break;
				}	
			}
			/* Release the tables data lock */
			Release(tablesDataLock);
			Acquire(customerDataLock);
	
			if(custDataTableNo != 0)
			{
				Release(customerDataLock);
		
				/* Before releasing the tablesDataLock Acquire the */
				/* eatInFoodWaitingLock to ensure that this customer is the */
				/* next one to join the queue waiting for the food to be bagged */
				Acquire(eatInFoodWaitingLock);
				/* customer goes on wait to receive a signal from the waiter */
				/* after the tokenNo is validated */	
				/* OG */
				Print_1Arg("Customer %d is waiting for the waiter to serve the food\n$",myIndex);
		
				Wait(eatInFoodWaitingCV,eatInFoodWaitingLock);
					/*potential problem	*/
				/* upon receiving the signal check if order is delivered */
				custDataDelivered = GetMV(deliveredincustDBMVi[myIndex]);
				while(custDataDelivered != 1)
				{				
					/* else Go Back to wait */
					Wait(eatInFoodWaitingCV,eatInFoodWaitingLock);
				}
				Release(eatInFoodWaitingLock);
				/* OG */
				Print_1Arg("Customer %d is served by waiter\n$", myIndex);
				/* if delivered then the customer eats the food and */
				/* leaves the restaurant */
				Print_2Arg("Customer %d is leaving the restaurant after OrderTaker %d packed the food\n$",myIndex, inOT);						
				Acquire(tablesDataLock);			
				
				tables = 0;
				SetMV(tablesMVi[index], tables);
				
				Release(tablesDataLock);
				Acquire(eatInWaitingLock);		
				Signal(eatInWaitingCV,eatInWaitingLock);		
				Release(eatInWaitingLock);
				/* OG */	
				Print_1Arg("EAT-IN  Customer %d is leaving the restaurant after having food\n$",myIndex);				 
				/* Acquire the Lock to update the custer count served the food	*/	
				Acquire(custServedLock);

				tempV = GetMV(custServedMVi);
				tempV ++; 
				SetMV(custServedMVi, tempV);
		
				/* @ */
				/* Print_2Arg("\n\nCUSTOMER %d SERVICED IS %d\n\n$", myIndex, custServed);		 */
				Release(custServedLock);	
				/* Print_1Arg("Customer %d calling E---X---I---T\n$", myIndex);	 */
				Exit(0);
			}
			else
				Release(customerDataLock);
		}
	}	


}
Exemplo n.º 30
0
int main() {

  int i;
  
  initTheater();

  while(theaterDone == 0) {	
    
    if(theaterFull || (GetMV(totalCustomers,0)-(GetMV(totalCustomersServed,0)+GetMV(numSeatsOccupied,0))) <= (MAX_SEATS-GetMV(numSeatsOccupied,0)) || 1) {
      Acquire(movieStatusLock);
      movieStatus = 1;
      Release(movieStatusLock);
      Print("The MovieTechnician has started the movie.\n", -1, -1, -1);

      movieLength = 50;
      while(movieLength > 0) {
        Yield();
        movieLength--;
      }

      Acquire(movieStatusLock);
      movieStatus = 2;
      Release(movieStatusLock);

      Print("The MovieTechnician has ended the movie.\n", -1, -1, -1);

      Acquire(movieFinishedLock);
      Broadcast(movieFinishedLockCV, movieFinishedLock);
      Release(movieFinishedLock);
      
      /* Free all seats for next movie */
      for(i=0; i<NUM_ROWS; i++)
        SetMV(freeSeatsInRow, NUM_COLS, i);
      
      theaterFull = 0;
      movieStarted = 0;
      SetMV(numSeatsOccupied, 0, 0);
      SetMV(totalTicketsTaken, 0, 0);
      
      /* Tell Customers to go to theater */
      if(GetMV(totalCustomers,0) != GetMV(totalCustomersServed,0)) {
        Acquire(customerLobbyLock);
        Broadcast(customerLobbyCV, customerLobbyLock);
        Release(customerLobbyLock);
      }
        
      Print("The MovieTechnician has told all customers to leave the theater room.\n", -1, -1, -1);
    }
    
    if(movieStatus == -1) {
      Yield();
    } else {
      while(((GetMV(numSeatsOccupied,0) < MAX_SEATS) && (GetMV(numSeatsOccupied,0) < GetMV(totalCustomers,0))) 
          || ((GetMV(numSeatsOccupied,0) == 0) && (GetMV(totalCustomersServed,0) == 0))) {
/*        Print("THERE ARE %d CUSTOMERS AND %d OF THE BITCHES ARE IN THEIR SEATS\n", GetMV(totalCustomers,0), GetMV(numSeatsOccupied,0), -1);*/
        Yield();
      }
/*      Acquire(movieStatusLock);
      Wait(movieStatusLockCV, movieStatusLock);  /* Wait until manager says it's time to start the movie */
/*      Release(movieStatusLock);*/
    }
    
    if((GetMV(totalCustomers, 0) <= 0) && (GetMV(totalCustomersServed, 0) > 0)) {
      Print("\n Everyone has left, Manager is closing the theater. Good work boys!\n\n", -1, -1 ,-1);
      break;
    }
  }
  
  Exit(0);

  return 0;
  
}