void waitThread(){

	ServerAcquire(lock1,1);
	ServerWait(condition1,lock1,1);
	ServerRelease(lock1,0);
	ServerAcquire(lock2,1);
	ServerRelease(lock2,0);
	Exit(0);
}			
int
	main(){
	Print("\nSERVERSIGNAL_SYSCALL TEST\n",sizeof("\nSERVERSIGNAL_SYSCALL TEST\n"),a,0);
	condition1 = CreateServerCondition("Condition1",0);
	lock1 = CreateServerLock("Lock1",1);
	lock2 = CreateServerLock("Lock2",0);

	
	Fork(waitThread);
	Yield();
	ServerAcquire(lock1,1);
	ServerSignal(condition1,lock1,0);
	ServerRelease(lock1,1);
	ServerAcquire(lock2,0);
	ServerRelease(lock2,1);
	Print("\nNow Signalling on Garbage server lock and server condition: this should fail!\n",sizeof("\nNow Signalling on Garbage server lock and server condition: this should fail!\n"),a,0);
	ServerSignal(0xfff,0xfff,1);
		
}
int
main() {

    int a[0];

    Print("\nsimple TEST\n",sizeof("\nsimple TEST\n"),a,0);
    lock1 = CreateServerLock("pie",0);
    cv1 = CreateServerCondition("strudle",0);

    ServerAcquire(lock1,0);
    Exec("../test/simpleNetwork2");
    ServerWait(cv1,lock1,0);
    ServerRelease(lock1,0);
}
예제 #4
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);
}
예제 #5
0
파일: main.c 프로젝트: fruitsamples/Echo
/* static */ void
AcceptConnection(ServerRef server, CFSocketNativeHandle sock, CFStreamError* error, void* info) {

	if (sock == ((CFSocketNativeHandle)(-1))) {
	
		fprintf(stderr, "AcceptConnection - Received an error (%d, %d)\n", (int)error->domain, (int)error->error);
		
		ServerInvalidate(server);
		ServerRelease(server);
		
		CFRunLoopStop(CFRunLoopGetCurrent());
	}
	else {
	
		EchoContextRef echo = EchoContextCreate(NULL, sock);
		
		if ((echo != NULL) && !EchoContextOpen(echo))
			EchoContextRelease(echo);
	}
}
예제 #6
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);
		}
	}
}
예제 #7
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);
		}
	}
}
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*/
예제 #9
0
void Waiter(){
	unsigned int myID;
	int myChef;
	int i = 0;
	int shortestLength;
	
	int one[1];
	int two[2];
	int three[3];
	int four[4];
	
	ServerAcquire(IDLock,-1);
	myID = GetMV(waiterID,-1);
	SetMV(waiterID, GetMV(waiterID,-1) + 1,-1);
	ServerRelease(IDLock,-1);
	
	while(true){
		myChef=-1;
		ServerAcquire(waiterLock[myID],-1);
		ServerAcquire(waiterLineLock[myID],-1);
		if(GetMV(waiterLineLength[myID],-1) != 0){
			ServerAcquire(waiterGetFoodLock[myID],-1);
			ServerRelease(waiterLock[myID],-1);  /*maybe wrong??!*/
			ServerSignal(waiterLine[myID], waiterLineLock[myID],-1);
			ServerRelease(waiterLineLock[myID],-1);
			/*process a waiting passenger*/
			do{
				ServerWait(waiterGetFood[myID], waiterGetFoodLock[myID],-1);
				ServerAcquire(globalFoodLock,-1);

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

				ServerSignal(waiterGetFood[myID], waiterGetFoodLock[myID],-1);
			} while (!GetMV(GetMV(passengerFoodValidFoodOrder[myID],-1),-1));
			two[0] = myID;
			two[1] = GetMV(GetMV(passengerFoodHolder[myID],-1),-1);
			Print("Waiter %d is serving Passenger %d of Train 0\n",sizeof("Waiter %d is serving Passenger %d of Train 0\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 Porter was available, so I have to get in line*/
			if(myChef==-1){
				myChef = 0;
				ServerAcquire(chefLock[0],-1);
				shortestLength = GetMV(chefRegularOrdersLeft[0],-1);
				for(i = 1; i < CHEFS; i += 1){
					ServerAcquire(chefLock[i],-1);
					if(GetMV(chefRegularOrdersLeft[i],-1) < shortestLength){
						shortestLength = GetMV(chefRegularOrdersLeft[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("waiter%d acquires chef%d's lock\n",myID,myChef);*/
			ServerAcquire(chefOrdersLineLock[myChef],-1);
			SetMV(chefRegularOrdersLeft[myChef], GetMV(chefRegularOrdersLeft[myChef],-1) + 1,-1);
			ServerRelease(chefLock[myChef],-1);
			/*wait for the chef to cook my food*/
			/*printf("waiter%d is waiting on chef%d to cook\n",myID,myChef);*/
			ServerWait(chefRegularOrdersLine[myChef], chefOrdersLineLock[myChef],-1);
			ServerRelease(chefOrdersLineLock[myChef],-1);
			/*one[0] = myID;*/
			/*Print("waiter%d has the order from the chef\n",sizeof("waiter%d has the order from the chef\n"),one,1);*/
			/*now I give the food to the passenger, and wait for him to eat,*/
			/*Print("waiter%d is waiting for the passenger to eat\n",sizeof("waiter%d is waiting for the passenger to eat\n"),one,1);*/
			
			/*waiterWaitingForEatingPassenger[myID]->ServerWait(waiterGetFoodLock[myID]);*/
			ServerWait(waiterGetFood[myID], waiterGetFoodLock[myID],-1);

			/*passenger is done eating and wating for a bill*/
			/*one[0] = myID;*/
			/*Print("waiter%d is giving passenger the bill\n",sizeof("waiter%d is giving passenger the bill\n"),one,1);*/

			/*passengerWaitingForBill[passengerFood[myID]->holder]->ServerSignal(waiterGetFoodLock[myID]);*/
			/*wait for payment*/
			ServerSignal(waiterGetFood[myID], waiterGetFoodLock[myID],-1);
			
			/*one[0] = myID;*/
			/*Print("waiter%d is waiting for passenger to pay\n",sizeof("waiter%d is waiting for passenger to pay\n"),one,1);*/

			/*waiterWaitingForMoney[myID]->ServerWait(waiterGetFoodLock[myID]);*/
			ServerWait(waiterGetFood[myID], waiterGetFoodLock[myID],-1);

			/*printf("waiter%d was paid by passenger%d!\n",myID, passengerFood[myID]->holder);*/
		
			/*update steward about the dining car safe*/
			ServerAcquire(waiterRevenueLock,-1);
			SetMV(waiterRevenue, GetMV(waiterRevenue,-1) + 2,-1);
			ServerRelease(waiterRevenueLock,-1);

			SetMV(waiterLineLength[myID], GetMV(waiterLineLength[myID],-1) - 1,-1);
			ServerAcquire(diningCarCapacityLock,-1);
			SetMV(diningCarCapacity, GetMV(diningCarCapacity,-1) + 1,-1); 
			ServerRelease(diningCarCapacityLock,-1);
			ServerRelease(waiterGetFoodLock[myID],-1);
		}
		else{
			one[0] = myID;
			Print("Waiter %d of Train 0 is going for a break\n",sizeof("Waiter %d of Train 0 is going for a break\n"),one,1);
			SetMV(waiterOnBreak[myID], true,-1);
			ServerRelease(waiterLineLock[myID],-1);
			ServerWait(waiter[myID], waiterLock[myID],-1);
			one[0] = myID;
			Print("Waiter %d of Train 0 returned from break\n",sizeof("Waiter %d of Train 0 returned from break\n"),one,1);
		}
	}
}/*end of Waiter*/
예제 #10
0
int main() {
	loop = TRUE;
	/* Handle Creation Stuff */
	
	/* gettin mah Clerk Index */
	initIndex = CreateMV("PaIn", sizeof("PaIn"), 1, 0x9999);
	initIndexLock = ServerCreateLock("PaInLk", sizeof("PaInLk"), 1);
	
	ServerAcquire(initIndexLock, 0);
	myIndex = GetMV(initIndex, 0);
	SetMV(initIndex, 0, myIndex + 1);
	ServerRelease(initIndexLock, 0);
	
	/* Locks */
	passLock = ServerCreateLock("PaLk", sizeof("PaLk"), NUM_CLERKS);
	senatorLock = ServerCreateLock("SeLk", sizeof("SeLk"), 1);
	customerLock = ServerCreateLock("CuLk", sizeof("CuLk"), 1);
	passLineLock = ServerCreateLock("PaLiLk", sizeof("PaLiLk"), 1);
	clerkWaitLock = ServerCreateLock("ClWaLk", sizeof("ClWaLk"), 1);
	fileLock = ServerCreateLock("FiLk", sizeof("FiLk"), NUM_CUSTOMERS + NUM_SENATORS);
	passMoneyLock = ServerCreateLock("PaMnLk", sizeof("PaMnLk"), 1);
	
	/* CVs */
	clerkWaitCV = ServerCreateCV("ClWaCV", sizeof("ClWaCV"), 1);
	passCV = ServerCreateCV("PaCV", sizeof("PaCV"), NUM_CLERKS);
	regPassLineCV = ServerCreateCV("RgPaCV", sizeof("RgPaCV"), 1);
	privPassLineCV = ServerCreateCV("PrPaCV", sizeof("PrPaCV"), 1);
	
	/* MVs */
	shutdownIndex = CreateMV("shut", sizeof("shut"), 1, 0x9999);
	officeSenatorIndex = CreateMV("OfSe", sizeof("OfSe"), 1, 0x9999);
	officeCustomerIndex = CreateMV("OfCu", sizeof("OfCu"), 1, 0x9999);
	regPassLineLengthIndex = CreateMV("RgPaLn", sizeof("RgPaLn"), 1, 0x9999);
	privPassLineLengthIndex = CreateMV("PrPaLn", sizeof("PrPaLn"), 1, 0x9999);
	numPassWaitIndex = CreateMV("NuPaWa", sizeof("NuPaWa"), 1, 0x9999);
	passDataIndex = CreateMV("PaDa", sizeof("PaDa"), NUM_CLERKS, 0x9999);
	passDataBoolIndex = CreateMV("PaDaBo", sizeof("PaDaBo"), NUM_CLERKS, 0x9999);
	passStateIndex = CreateMV("PaSt", sizeof("PaSt"), NUM_CLERKS, 0x9999);
	fileTypeIndex = CreateMV("FiTp", sizeof("FiTp"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);
	fileStateIndex = CreateMV("FiSt", sizeof("FiSt"), NUM_CUSTOMERS + NUM_SENATORS, 0x9999);
	passMoneyIndex = CreateMV("PaMn", sizeof("PaMn"), 1, 0x9999);
	
	traceLock = ServerCreateLock("trace", sizeof("trace"), 1);
	
	ServerAcquire(passLock, myIndex);
	SetMV(passStateIndex, myIndex, BUSY);
	ServerRelease(passLock, myIndex);
	
	while(loop == TRUE){
		shutdown = GetMV(shutdownIndex, 0);
		if (shutdown == TRUE) {
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pass", myIndex, 0x00, 0, "Shutting down.\n");
			ServerRelease(traceLock, 0);
			Exit(0);
		}
		ServerAcquire(senatorLock, 0);		/* only one ServerLock, so acquire index 0? */
		ServerAcquire(customerLock, 0);
		
		officeSenator = GetMV(officeSenatorIndex, 0);
		officeCustomer = GetMV(officeCustomerIndex, 0);
		
		if(officeSenator > 0 && officeCustomer > 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);	
		}
		/* Check for customers in line */
		ServerAcquire(passLineLock, 0);
		
		privPassLineLength = GetMV(privPassLineLengthIndex, 0);
		regPassLineLength = GetMV(regPassLineLengthIndex, 0);
		if(privPassLineLength > 0){
		/* Decrement line length, set state to AVAIL, signal 1st customer and wait for them */
			privPassLineLength--;
			SetMV(privPassLineLengthIndex, 0, privPassLineLength);
			
			ServerAcquire(senatorLock, 0);
			numPassWait = GetMV(numPassWaitIndex, 0);
			numPassWait++;
			SetMV(numPassWaitIndex, 0, numPassWait);
			ServerRelease(senatorLock, 0);
			
			ServerAcquire(passLock, myIndex);
			SetMV(passStateIndex, myIndex, AVAILABLE);
			ServerSignal(privPassLineCV, 0, passLineLock, 0);
			ServerRelease(passLineLock, 0);
			
			/* wait for customer to signal me */
			ServerWait(passCV, myIndex, passLock, myIndex);
			
			/* customer gave me thier SSN index to check thier file */
			mySSN = GetMV(passDataIndex, myIndex);
			ServerAcquire(fileLock, mySSN);
			/* check customer type */
			
			cType = GetMV(fileTypeIndex, mySSN);
			
			fileState = GetMV(fileStateIndex, mySSN);
			if(fileState == APPPICDONE){
				SetMV(passDataBoolIndex, myIndex, TRUE);
				
				doPassport = TRUE;

				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Gives valid certification to Customer.\n");
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Gives valid certification to Senator.\n");
				}
				ServerRelease(traceLock, 0);
			} else {
				SetMV(passDataBoolIndex, myIndex, FALSE);
				doPassport = FALSE;
				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Gives invalid certification to Customer.\n");
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Punishes Customer to wait.\n");		
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Gives invalid certification to Senator.\n");
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Punishes Senator to wait.\n");
				}
				ServerRelease(traceLock, 0);
			}
			ServerRelease(fileLock, mySSN);

			/* add $500 to passClerk money amount for privileged fee */
			ServerAcquire(passMoneyLock, 0);
			passMoney = GetMV(passMoneyIndex, 0);
			passMoney += 500;
			SetMV(passMoneyIndex, 0, passMoney);
			ServerRelease(passMoneyLock, 0);

			ServerAcquire(traceLock, 0);
			if(cType == CUSTOMER){
				ClerkTrace("Pass", myIndex, "Cust", mySSN, "Accepts $500 from Customer.\n");
				ClerkTrace("Pass", myIndex, "Cust", mySSN, "Informs Customer that the procedure has been completed.\n");
			}
			else{
				ClerkTrace("Pass", myIndex, "Sen", mySSN, "Accepts $500 from Senator.\n");
				ClerkTrace("Pass", myIndex, "Sen", mySSN, "Informs Senator that the produre has been completed.\n");
			}
			ServerRelease(traceLock, 0);

			/* signal customer awake */
			ServerSignal(passCV, myIndex, passLock, myIndex);
			ServerRelease(passLock, myIndex);				/* release clerk lock */

			if(doPassport == TRUE){
				for(i = 0; i < 20; i++){
					/*Yield();	*/
				}

				ServerAcquire(fileLock, mySSN);
				SetMV(fileStateIndex, mySSN, PASSDONE);
				ServerRelease(fileLock, mySSN);
				
				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Finished filing Customer's passport.\n");
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Finished filing Senator's passport.\n");
				}
				ServerRelease(traceLock, 0);
			}
			
			doPassport = FALSE;
			mySSN = 0;
		}

		else if (regPassLineLength > 0){
		/* Decrement line length, set state to AVAIL, signal 1st customer and wait for them */
			regPassLineLength--;
			SetMV(regPassLineLengthIndex, 0, regPassLineLength);
			
			ServerAcquire(senatorLock, 0);
			numPassWait = GetMV(numPassWaitIndex, 0);
			numPassWait++;
			SetMV(numPassWaitIndex, 0, numPassWait);
			ServerRelease(senatorLock, 0);
			
			ServerAcquire(passLock, myIndex);
			SetMV(passStateIndex,  myIndex, AVAILABLE);
			ServerSignal(regPassLineCV, 0, passLineLock, 0);
			ServerRelease(passLineLock, 0);
			ServerWait(passCV, myIndex, passLock, myIndex); /* wait for customer to signal me */

			/* customer gave me thier SSN index to check thier file */
			mySSN = GetMV(passDataIndex, myIndex);
			ServerAcquire(fileLock, mySSN);
			/* check customer type */
			cType = GetMV(fileTypeIndex, mySSN);
			fileState = GetMV(fileStateIndex, mySSN);
			
			if(fileState == APPPICDONE){
				SetMV(passDataBoolIndex, myIndex, TRUE);
				doPassport = TRUE;

				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Gives valid certification to Customer.\n");
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Gives valid certification to Senator.\n");
				}
				ServerRelease(traceLock, 0);
			} else {
				SetMV(passDataBoolIndex, myIndex, FALSE);
				doPassport = FALSE;
				/*Trace("CUSTOMER'S STATE WAS ", fileState);
				Trace("\n", 0x9999);*/
				
				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Gives invalid certification to Customer.\n");
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Punishes Customer to wait.\n");	
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Gives invalid certification to Senator.\n");
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Punishes Senator to wait.\n");
				}
				ServerRelease(traceLock, 0);
			}
			
			ServerAcquire(traceLock, 0);
			if(cType == CUSTOMER){
				ClerkTrace("Pass", myIndex, "Cust", mySSN, "Informs Customer that the procedure has been completed.\n");
			}
			else{
				ClerkTrace("Pass", myIndex, "Sen", mySSN, "Informs Senator that the procedure has been completed.\n");
			}
			ServerRelease(traceLock, 0);

			ServerRelease(fileLock, mySSN);

			ServerSignal(passCV, myIndex, passLock, myIndex); /* signal customer awake */
			ServerRelease(passLock, myIndex);				/* release clerk lock */

			if(doPassport){
				for(i = 0; i < 20; i++){
					/*Yield();*/
				}

				ServerAcquire(fileLock, mySSN);
				SetMV(fileStateIndex, mySSN, PASSDONE);
				ServerRelease(fileLock, mySSN);
				
				ServerAcquire(traceLock, 0);
				if(cType == CUSTOMER){
					ClerkTrace("Pass", myIndex, "Cust", mySSN, "Finished filing Customer's passport.\n");
				}
				else{
					ClerkTrace("Pass", myIndex, "Sen", mySSN, "Finished filing Senator's passport.\n");
				}
				ServerRelease(traceLock, 0);
			}
			doPassport = FALSE;
			mySSN = 0;
		}
		else{
			/* No one in line...take a break */
			ServerRelease(passLineLock, 0);
			ServerAcquire(passLock, myIndex);
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pass", myIndex, 0x00, 0, "Goin on break.\n");
			ServerRelease(traceLock, 0);
			
			SetMV(passStateIndex, myIndex, BREAK);
			ServerWait(passCV, myIndex, passLock, myIndex);
			
			ServerAcquire(traceLock, 0);
			ClerkTrace("Pass", myIndex, 0x00, 0, "Returning from break.\n");
			ServerRelease(traceLock, 0);

			ServerRelease(passLock, myIndex);
		}
	}
}