void Cashier() { int holdingES,i; int _totalAmount, _cashWaitingCount, _patientType, _pid, _xrayStatus, _xrayImages, _shotStatus; while (1) { /*----------------------------------------------------- // Cashier wait for Nurse bringing patient to his desk //-----------------------------------------------------*/ AcquireLock(cashWaitingLock); _cashWaitingCount = GetMV(cashWaitingCount, 0); if (_cashWaitingCount > 0) { SetMV(cashWaitingCount, 0,_cashWaitingCount-1); SignalCondition(cashWaitingCV, cashWaitingLock); AcquireLock(cashLock); ReleaseLock(cashWaitingLock); WaitCondition(cashCV, cashLock); /*------------------------------------------ // Cashier gets the ES and calculate the fee //------------------------------------------*/ holdingES = GetMV(cashExamSheet, 0); _patientType = GetMV(esStack, holdingES*18+8); _pid = GetMV(esStack, holdingES*18+0); if(_patientType == 0) { Print("Cashier receives the examination sheet from Adult Patient [%d].\n", sizeof("Cashier receives the examination sheet from Adult Patient [%d].\n")/sizeof(char), 1, _pid); } else { Print("Cashier receives the examination sheet from Child Patient [%d] from Parent [%d].\n", sizeof("Cashier receives the examination sheet from Child Patient [%d] from Parent [%d].\n")/sizeof(char), 2, _pid*256+_pid); } _totalAmount = 20; _xrayStatus = GetMV(esStack, holdingES*18+11); _xrayImages = GetMV(esStack, holdingES*18+12); if (_xrayStatus == 4) { for (i=0; i<_xrayImages; i++) { _totalAmount += 10; } _totalAmount += 20; } _shotStatus = GetMV(esStack, holdingES*18+14); if (_shotStatus == 3) { _totalAmount += 20; } SetMV(esStack, holdingES*18+17, _totalAmount); if(_patientType == 0) { Print("Cashier reads the examination sheet of Adult Patient [%d] and asks him to pay $ [%d].\n", sizeof("Cashier reads the examination sheet of Adult Patient [%d] and asks him to pay $ [%d].\n")/sizeof(char), 2, _pid*256+_totalAmount); } else { Print("Cashier reads the examination sheet of Child Patient [%d] and asks Parent [%d] to pay $ [%d].\n", sizeof("Cashier reads the examination sheet of Child Patient [%d] and asks Parent [%d] to pay $ [%d].\n")/sizeof(char), 3, _pid*65536+_pid*256+_totalAmount); } SignalCondition(cashCV, cashLock); WaitCondition(cashCV, cashLock); if(_patientType == 0) { Print("Cashier accepts $[%d] from Adult Patient [%d].\n", sizeof("Cashier accepts $[%d] from Adult Patient [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); Print("Cashier gives a receipt of $[%d] to Adult Patient [%d].\n", sizeof("Cashier gives a receipt of $[%d] to Adult Patient [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); } else { Print("Cashier accepts $[%d] from Parent [%d].\n", sizeof("Cashier accepts $[%d] from Parent [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); Print("Cashier gives a receipt of $[%d] to Parent [%d].\n", sizeof("Cashier gives a receipt of $[%d] to Parent [%d].\n")/sizeof(char), 2, _totalAmount*256+_pid); } SignalCondition(cashCV, cashLock); WaitCondition(cashCV, cashLock); ReleaseLock(cashLock); } else { ReleaseLock(cashWaitingLock); } Yield(); } }
void doCreate() { int t; wrWaitingCount = CreateMV("wrwc", sizeof("wrwc")/sizeof(char), 1); wrnServiceId = CreateMV("wsid", sizeof("wsid")/sizeof(char), 1); wrnExamSheet = CreateMV("wes", sizeof("wes")/sizeof(char), 2); wrnCompleteESList = CreateMV("wrnces", sizeof("wrnces")/sizeof(char), 64); nwrnWaitingCount = CreateMV("nwwc", sizeof("nwwc")/sizeof(char), 1); nwrnExamSheet = CreateMV("nwes", sizeof("nwes")/sizeof(char), 1); nwrnId = CreateMV("nwid", sizeof("nwid")/sizeof(char), 1); pid = CreateMV("pid", sizeof("pid")/sizeof(char), 1); wrnTask = CreateMV("wt", sizeof("wt")/sizeof(char), 1); esStack = CreateMV("ess", sizeof("ess")/sizeof(char), 18*256); broadCastId = CreateMV("bcid", sizeof("bcid")/sizeof(char), 1); bcIdStatus = CreateMV("bcis", sizeof("bcis")/sizeof(char), 1); examRoomsLockMV = CreateMV("erlmv", sizeof("erlmv")/sizeof(char), 6); SetMV(examRoomsLockMV, 0, 5); examRoomsCVMV = CreateMV("ercmv", sizeof("ercmv")/sizeof(char), 6); SetMV(examRoomsCVMV, 0, 5); doctWaitingLockMV = CreateMV("dwlm", sizeof("dwlm")/sizeof(char), 3); SetMV(doctWaitingLockMV, 0, 2); docStatus = CreateMV("ds", sizeof("ds")/sizeof(char), 3); SetMV(docStatus, 0, 2); doctWaitingCVMV = CreateMV("dwcm", sizeof("dwcm")/sizeof(char), 3); SetMV(doctWaitingCVMV, 0, 2); ndocESNo = CreateMV("nesn", sizeof("nesn")/sizeof(char), 3); SetMV(ndocESNo, 0, 2); wallPackets = CreateMV("wp", sizeof("wp")/sizeof(char), 6); SetMV(wallPackets, 0, 5); docid = CreateMV("di", sizeof("di")/sizeof(char), 1); xid = CreateMV("xid", sizeof("xid")/sizeof(char), 1); xrayRoomsLockMV = CreateMV("xrlmv", sizeof("xrlmv")/sizeof(char), 3); SetMV(xrayRoomsLockMV, 0, 2); xrayRoomsCVMV = CreateMV("xrcmv", sizeof("xrcmv")/sizeof(char), 3); SetMV(xrayRoomsCVMV, 0, 2); xrayRoomsES = CreateMV("xres", sizeof("xres")/sizeof(char), 3); SetMV(xrayRoomsES, 0, 2); xrayRoomsStatus = CreateMV("xrs", sizeof("xrs")/sizeof(char), 3); SetMV(xrayRoomsStatus, 0, 2); xrayWaitingES = CreateMV("xwes", sizeof("xwes")/sizeof(char), 64); cashWaitingCount = CreateMV("cwc", sizeof("cwc")/sizeof(char), 1); cashExamSheet = CreateMV("ces", sizeof("ces")/sizeof(char), 1); nid = CreateMV("nid", sizeof("nid")/sizeof(char), 1); examRoomsStatus = CreateMV("ers", sizeof("ers")/sizeof(char), 6); SetMV(examRoomsStatus, 0, 5); cabinetCount = CreateMV("cac", sizeof("cac")/sizeof(char), 1); waitingRoomLock = CreateLock("wrl", sizeof("wrl")/sizeof(char)); wrnLock = CreateLock("wrnl", sizeof("wrnl")/sizeof(char)); nwrnLock = CreateLock("nwrnl", sizeof("nwrnl")/sizeof(char)); t = CreateLock("erl1", sizeof("erl1")/sizeof(char)); SetMV(examRoomsLockMV, 1, t); t = CreateLock("erl2", sizeof("erl2")/sizeof(char)); SetMV(examRoomsLockMV, 2, t); t = CreateLock("erl3", sizeof("erl3")/sizeof(char)); SetMV(examRoomsLockMV, 3, t); t = CreateLock("erl4", sizeof("erl4")/sizeof(char)); SetMV(examRoomsLockMV, 4, t); t = CreateLock("erl5", sizeof("erl5")/sizeof(char)); SetMV(examRoomsLockMV, 5, t); broadCastIdLock = CreateLock("bcil", sizeof("bcil")/sizeof(char)); cashWaitingLock = CreateLock("cwl", sizeof("cwl")); cashLock = CreateLock("cl", sizeof("cl")/sizeof(char)); pidLock = CreateLock("pl", sizeof("pl")/sizeof(char)); t = CreateLock("dwl1", sizeof("dwl1")/sizeof(char)); SetMV(doctWaitingLockMV, 1, t); t = CreateLock("dwl2", sizeof("dwl2")/sizeof(char)); SetMV(doctWaitingLockMV, 2, t); didLock = CreateLock("dl", sizeof("dl")/sizeof(char)); t = CreateLock("xrl1", sizeof("xrl1")/sizeof(char)); SetMV(xrayRoomsLockMV, 1, t); t = CreateLock("xrl2", sizeof("xrl2")/sizeof(char)); SetMV(xrayRoomsLockMV, 2, t); xidLock = CreateLock("xl", sizeof("xl")/sizeof(char)); nidLock = CreateLock("nl", sizeof("nl")/sizeof(char)); ersStatusLock = CreateLock("ersl", sizeof("ersl")/sizeof(char)); xrayWaitingLock = CreateLock("xwl", sizeof("xwl")/sizeof(char)); xrayRoomsStatusLock = CreateLock("xrsl", sizeof("xrsl")/sizeof(char)); cabinetLock = CreateLock("cl", sizeof("cl")/sizeof(char)); waitingRoomCV = CreateCondition("wrc", sizeof("wrc")/sizeof(char)); nwrnCV = CreateCondition("nwrnc", sizeof("nwrnc")/sizeof(char)); wrnCV = CreateCondition("wrnc", sizeof("wrnc")/sizeof(char)); nwrnWaitingCV = CreateCondition("nwc", sizeof("nwc")/sizeof(char)); t = CreateCondition("erc1", sizeof("erc1")/sizeof(char)); SetMV(examRoomsCVMV, 1, t); t = CreateCondition("erc2", sizeof("erc2")/sizeof(char)); SetMV(examRoomsCVMV, 2, t); t = CreateCondition("erc3", sizeof("erc3")/sizeof(char)); SetMV(examRoomsCVMV, 3, t); t = CreateCondition("erc4", sizeof("erc4")/sizeof(char)); SetMV(examRoomsCVMV, 4, t); t = CreateCondition("erc5", sizeof("erc5")/sizeof(char)); SetMV(examRoomsCVMV, 5, t); t = CreateCondition("dwc1", sizeof("dwc1")/sizeof(char)); SetMV(doctWaitingCVMV, 1, t); t = CreateCondition("dwc2", sizeof("dwc2")/sizeof(char)); SetMV(doctWaitingCVMV, 2, t); t = CreateCondition("xrc1", sizeof("xrc1")/sizeof(char)); SetMV(xrayRoomsCVMV, 1, t); t = CreateCondition("xrc2", sizeof("xrc2")/sizeof(char)); SetMV(xrayRoomsCVMV, 2, t); cashWaitingCV = CreateCondition("cwc", sizeof("cwc")/sizeof(char)); cashCV = CreateCondition("cc", sizeof("cc")/sizeof(char)); broadCastIdCV = CreateCondition("bcicv", sizeof("bcicv")/sizeof(char)); bcIdWaitingCV = CreateCondition("bwc", sizeof("bwc")/sizeof(char)); }
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); } } }
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*/
void CustomerHead() { int i,j; int myTicketClerk; int myConcessionClerk; int shortestTCLine; int shortestTCLineLen; int popRandVal; int sodaRandVal; int printPop; int printSoda; int shortestCCLine; int shortestCCLineLen; int foodAmount; int myTicketTaker; int shortestTTLine; int shortestTTLineLen; int bathRoomValue; int myGroupNumber,myCustomerNumber,myGroupSize; int localNoOfTC; int localTicketClerkState; int localReFindLineTC; int localCustFindLineTC; int localTicketClerkAmount; int localNoOfCC; int localConcessionClerkState; int localConcessionClerkLineCount; int localCustFindLineCC; int localIsTakingTickets; int localTicketTakerState; int localTicketTakerLineCount; int localTotal; int localTicketClerkLineCount; int localRowNo; int localColumnNo; int localGroupPopcorn; int localGroupSoda; int localReFindLineCC; int localNoOfPopcorn; int localNoOfSoda; int localAmountSoda; int localAmountPopcorn; int localNoOfTT; int localTicketTakerGroupIndicator; int localCountCustTT; int localCustEnteredTheater; int localCustLeavingTheater; int localCustFinalExitCount; int localCounterVal; /* Acquire(groupNumberLockInt); */ Acquire("groupNumberLock"); /* myGroupNumber=nextGroupNumber; */ myGroupNumber=GetMV("nextGroupNumber",0); /*Print("\n The value of GET myGroupNumber is %d\n",myGroupNumber,1,1);*/ /* nextGroupNumber++; */ SetMV("nextGroupNumber",0,myGroupNumber+1); /*Print("\n The value being SET of myGroupNumber is %d\n",myGroupNumber+1,1,1); Print("\n The value after SET of myGroupNumber is %d\n",GetMV("nextGroupNumber",0),1,1);*/ /* Release(groupNumberLockInt); */ Release("groupNumberLock"); /* Acquire(customerNumberLockInt); */ Acquire("customerNumberLock"); /* myCustomerNumber=nextCustomerNumber; */ myCustomerNumber=GetMV("nextCustomerNumber",0); /* nextCustomerNumber++; */ SetMV("nextCustomerNumber",0,myCustomerNumber+1); /* Release(customerNumberLockInt); */ Release("customerNumberLock"); /* myGroupSize=sizeForGroupArr[myGroupNumber]; */ myGroupSize=GetMV("sizeForGroupArr",myGroupNumber); /*Print("\n The value after GET of myGroupSize is %d\n",GetMV("sizeForGroupArr",myGroupNumber),1,1); Print("\n The value of GroupSize is %d\n",myGroupSize+1,1,1);*/ while(1) { myTicketClerk=-1; /* Acquire(ticketClerkLineLockInt); */ Acquire("tcLineLock"); /* for (i=0;i<noOfTC;i++) /* If any TC is available, not busy */ localNoOfTC = GetMV("noOfTC",0); for (i=0;i<localNoOfTC;i++) /* If any TC is available, not busy */ { /* if (ticketClerkState[i]==0) */ localTicketClerkState = GetMV("ticketClerkState",i); if (localTicketClerkState==0) { myTicketClerk=i; /* ticketClerkState[i]=1; */ SetMV("ticketClerkState",i,1); Print("\nCustomer[%d] in Group[%d] is getting in TicketClerk line[%d]\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,1); break; } } if (myTicketClerk == -1) /*Acquiring the shortest Line*/ { /* shortestTCLine; */ /* shortestTCLineLen=ticketClerkLineCount[0]; */ shortestTCLineLen = GetMV("tcLineCnt",0); /* for(i=0;i<noOfTC;i++) */ localNoOfTC = GetMV("noOfTC",0); for(i=0;i<localNoOfTC;i++) { /* if(ticketClerkState[i]!=-2) /*check that the TC is not on break*/ localTicketClerkState = GetMV("ticketClerkState",i); if(localTicketClerkState!=-2) /*check that the TC is not on break*/ { shortestTCLine=i; /* shortestTCLineLen=ticketClerkLineCount[i]; */ shortestTCLineLen=GetMV("tcLineCnt",i); break; } else { /*checking which all TCs are on break*/ Print("\nCustomer[%d] in Group [%d] sees TicketClerk[%d] is on break.\n\0",myCustomerNumber,myGroupNumber PRINTMETOO i,1); } } /* for (i=1;i<noOfTC;i++ ) /*Assigning the TC with shortest Line*/ localNoOfTC = GetMV("noOfTC",0); for (i=1;i<localNoOfTC;i++) { /* if (ticketClerkState[i]==1 && (shortestTCLineLen > ticketClerkLineCount[i])) */ localTicketClerkState = GetMV("ticketClerkState",i); localTicketClerkLineCount = GetMV("tcLineCnt",i); if (localTicketClerkState==1 && (shortestTCLineLen > localTicketClerkLineCount)) { shortestTCLine=i; /* shortestTCLineLen=ticketClerkLineCount[i]; */ shortestTCLineLen = GetMV("tcLineCnt",i); } } myTicketClerk=shortestTCLine; /* ticketClerkLineCount[myTicketClerk]++; */ localTicketClerkLineCount = GetMV("tcLineCnt",myTicketClerk); SetMV("tcLineCnt",myTicketClerk,localTicketClerkLineCount+1); Print("\nCustomer[%d] in Group[%d] is getting in TicketClerk line[%d]\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,1); /* Wait(ticketClerkLineCVInt[myTicketClerk],ticketClerkLineLockInt); */ Wait(ticketClerkLineCVChar[myTicketClerk],"tcLineLock"); } /* if(reFindLineTC[myTicketClerk]==1) /*If I have been asked to re-acquire the line*/ localReFindLineTC = GetMV("reFindLineTC",myTicketClerk); if(localReFindLineTC==1) /*If I have been asked to re-acquire the line*/ { /*customers in the line of ticketclerk[%d] are starting the process of finding shortest line again as they are asked to re-arrange*/ /* custFindLineTC[myTicketClerk]++; */ localCustFindLineTC = GetMV("custFindLineTC",myTicketClerk); SetMV("custFindLineTC",myTicketClerk,localCustFindLineTC+1); /* if(ticketClerkLineCount[myTicketClerk]==custFindLineTC[myTicketClerk]) */ localTicketClerkLineCount = GetMV("tcLineCnt",myTicketClerk); localCustFindLineTC = GetMV("custFindLineTC",myTicketClerk); if(localTicketClerkLineCount==localCustFindLineTC) { /* reFindLineTC[myTicketClerk]=0; */ SetMV("reFindLineTC",myTicketClerk,0); /* ticketClerkLineCount[myTicketClerk]=0; */ SetMV("tcLineCnt",myTicketClerk,0); /* custFindLineTC[myTicketClerk]=0; */ SetMV("custFindLineTC",myTicketClerk,0); } /* Release(ticketClerkLineLockInt); */ Release("tcLineLock"); continue; } else { /* Release(ticketClerkLineLockInt); /*continue with the same clerk*/ Release("tcLineLock"); break; } } /*while loop*/ /* Acquire(ticketClerkLockInt[myTicketClerk]); /*Time to interact with clerk*/ Acquire(ticketClerkLockChar[myTicketClerk]); /* noOfTickets[myTicketClerk]=myGroupSize+1; /*Telling the number of tickets I wish to purchase i.e Groupsize+HeadCust*/ SetMV("noOfTickets",myTicketClerk,myGroupSize+1); /* Print("\nCustomer[%d] in Group[%d] is walking up to TicketClerk[%d] to buy %d tickets\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,noOfTickets[myTicketClerk] PRINTMETOO 1); */ /* Print("\nCustomer[%d] in Group[%d] is walking up to TicketClerk[%d] to buy %d tickets\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,GetMV("noOfTickets",myTicketClerk) PRINTMETOO 1);*/ /*Print("GroupSize val: %d",myGroupSize+1,1,1);*/ Print("\nCustomer[%d] in Group[%d] is walking up to TicketClerk[%d] to buy %d tickets\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,myGroupSize+1 PRINTMETOO 1); /* Signal(ticketClerkCVInt[myTicketClerk],ticketClerkLockInt[myTicketClerk]); */ Signal(ticketClerkCVChar[myTicketClerk],ticketClerkLockChar[myTicketClerk]); /* Wait(ticketClerkCVInt[myTicketClerk],ticketClerkLockInt[myTicketClerk]); /*Waiting for TC o tell me the amount*/ Wait(ticketClerkCVChar[myTicketClerk],ticketClerkLockChar[myTicketClerk]); /* Print("\nCustomer[%d] in Group[%d] in TicketClerk line[%d] is paying %d for tickets\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,ticketClerkAmount[myTicketClerk] PRINTMETOO 1); */ localTicketClerkAmount = GetMV("ticketClerkAmount",myTicketClerk); Print("\nCustomer[%d] in Group[%d] in TicketClerk line[%d] is paying %d for tickets\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,localTicketClerkAmount PRINTMETOO 1); /* Signal(ticketClerkCVInt[myTicketClerk],ticketClerkLockInt[myTicketClerk]); */ Signal(ticketClerkCVChar[myTicketClerk],ticketClerkLockChar[myTicketClerk]); /* Wait(ticketClerkCVInt[myTicketClerk],ticketClerkLockInt[myTicketClerk]); */ Wait(ticketClerkCVChar[myTicketClerk],ticketClerkLockChar[myTicketClerk]); Print("\nCustomer[%d] in Group[%d] is leaving TicketClerk[%d]\n\0",myCustomerNumber,myGroupNumber PRINTMETOO myTicketClerk,1); /* Release(ticketClerkLockInt[myTicketClerk]); /*Going out from TC*/ Release(ticketClerkLockChar[myTicketClerk]); /******************************End of Tc Interaction**********************************/ /* ///////////////////// customer interaction for food///////////// */ /* Acquire(groupLockInt[myGroupNumber]); */ Acquire(groupLockChar[myGroupNumber]); /* haveTickets[myGroupNumber]=1; */ SetMV("haveTickets",myGroupNumber,1); if(myGroupSize != 0) { /* Broadcast(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Broadcast(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); /* Wait(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); /* waiting for group to give choices of popcorn & soda*/ Wait(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); /*Code for individual customer food choice*/ } popRandVal=Rand(100); /*Random value for popcorn and soda*/ popRandVal=popRandVal+1; sodaRandVal=Rand(100); sodaRandVal=sodaRandVal+1; printPop=0; printSoda=0; if(popRandVal<=75) /* Ensuring 75% probability */ { /* groupPopcorn[myGroupNumber]++; */ localGroupPopcorn = GetMV("groupPopcorn",myGroupNumber); SetMV("groupPopcorn",myGroupNumber,localGroupPopcorn+1); printPop=1; } if(sodaRandVal<=75) /*Ensuring 75% probability*/ { /* groupSoda[myGroupNumber]++; */ localGroupSoda = GetMV("groupSoda",myGroupNumber); SetMV("groupSoda",myGroupNumber,localGroupSoda+1); printSoda=1; } /* Got the number of popcorns and soda for my group */ Print("\nCustomer[%d] in group[%d] wants %d popcorn and %d soda.\n",myCustomerNumber,myGroupNumber PRINTMETOO printPop,printSoda PRINTMETOO 1); /* Release(groupLockInt[myGroupNumber]); */ Release(groupLockChar[myGroupNumber]); /* ///////////// CC Interaction //////////////// */ while(1) { myConcessionClerk=-1; /* Acquire(concessionClerkLineLockInt); */ Acquire("ccLineLock"); /* for (i=0;i<noOfCC;i++) /* Checking for an available clerk */ localNoOfCC = GetMV("noOfCC",0); for (i=0;i<localNoOfCC;i++) { /* if (concessionClerkState[i]==0) */ localConcessionClerkState = GetMV("ccState",i); if (localConcessionClerkState==0) { myConcessionClerk=i; /* concessionClerkState[i]=1; */ SetMV("ccState",i,1); Print("\nCustomer[%d] in Group[%d] is getting in ConcessionClerk Line[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,1); break; } } if (myConcessionClerk == -1) /* If no clerk is available */ { /* for(i=0;i<noOfCC;i++) */ localNoOfCC = GetMV("noOfCC",0); for (i=0;i<localNoOfCC;i++) { /* if(concessionClerkState[i]!=-2) */ localConcessionClerkState = GetMV("ccState",i); if(localConcessionClerkState!=-2) { shortestCCLine=i; /* checking that the particular clerk is not on break */ /* shortestCCLineLen=concessionClerkLineCount[i]; */ shortestCCLineLen = GetMV("ccLineCnt",i); break; } else { /* checking all the clerks on break */ Print("\nCustomer[%d] in Group [%d] sees ConcessionClerk[%d] is on break.\n",myCustomerNumber,myGroupNumber PRINTMETOO i,1); } } /* for (j=1;j<noOfCC;j++) */ localNoOfCC = GetMV("noOfCC",0); for (j=1;j<localNoOfCC;j++) { /* If the clerk is busy and he has the shortest line */ /* if (concessionClerkState[j]==1 && (shortestCCLineLen > concessionClerkLineCount[j])) */ localConcessionClerkState = GetMV("ccState",j); localConcessionClerkLineCount = GetMV("ccLineCnt",j); if (localConcessionClerkState==1 && (shortestCCLineLen > localConcessionClerkLineCount)) { shortestCCLine=j; /* shortestCCLineLen=concessionClerkLineCount[j]; */ shortestCCLineLen=GetMV("ccLineCnt",j);; } } myConcessionClerk=shortestCCLine; /* Got my clerk */ /* concessionClerkLineCount[myConcessionClerk]++; */ localConcessionClerkLineCount = GetMV("ccLineCnt",myConcessionClerk); SetMV("ccLineCnt",myConcessionClerk,localConcessionClerkLineCount+1); Print("\nCustomer[%d] in Group[%d] is getting in ConcessionClerk Line[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,1); /* Wait(concessionClerkLineCVInt[myConcessionClerk],concessionClerkLineLockInt); */ Wait(concessionClerkLineCVChar[myConcessionClerk],"ccLineLock"); } /* if(reFindLineCC[myConcessionClerk]==1) /* If manager has aked me to acquire the shortest line again */ localReFindLineCC = GetMV("reFindLineCC",myConcessionClerk); if(localReFindLineCC==1) { /*customers in the line of concessionclerk[%d] are starting the process of finding shortest line again as they are asked to re-arrange*/ /* custFindLineCC[myConcessionClerk]++; */ localCustFindLineCC = GetMV("custFindLineCC",myConcessionClerk); SetMV("custFindLineCC",myConcessionClerk,localCustFindLineCC+1); /* if(concessionClerkLineCount[myConcessionClerk]==custFindLineCC[myConcessionClerk]) */ localConcessionClerkLineCount = GetMV("ccLineCnt",myConcessionClerk); localCustFindLineCC = GetMV("custFindLineCC",myConcessionClerk); if(localConcessionClerkLineCount==localCustFindLineCC) { /* reFindLineCC[myConcessionClerk]=0; */ SetMV("reFindLineCC",myConcessionClerk,0); /* concessionClerkLineCount[myConcessionClerk]=0; */ SetMV("ccLineCnt",myConcessionClerk,0); /* custFindLineCC[myConcessionClerk]=0; */ SetMV("custFindLineCC",myConcessionClerk,0); } /* Release(concessionClerkLineLockInt); */ Release("ccLineLock"); continue; } else { /* Release(concessionClerkLineLockInt); */ Release("ccLineLock"); break; } } /*while loop*/ /* Acquire(concessionClerkLockInt[myConcessionClerk]); /*Time to interact with clerk*/ Acquire(concessionClerkLockChar[myConcessionClerk]); /* Acquire(groupLockInt[myGroupNumber]); */ Acquire(groupLockChar[myGroupNumber]); /* noOfSoda[myConcessionClerk]=groupSoda[myGroupNumber]; */ localGroupSoda = GetMV("groupSoda",myGroupNumber); SetMV("noOfSoda",myConcessionClerk,localGroupSoda); /* noOfPopcorn[myConcessionClerk]=groupPopcorn[myGroupNumber]; */ localGroupPopcorn = GetMV("groupPopcorn",myGroupNumber); SetMV("noOfPopcorn",myConcessionClerk,localGroupPopcorn); /* Release(groupLockInt[myGroupNumber]); */ Release(groupLockChar[myGroupNumber]); /* Signal(concessionClerkCVInt[myConcessionClerk],concessionClerkLockInt[myConcessionClerk]); */ Signal(concessionClerkCVChar[myConcessionClerk],concessionClerkLockChar[myConcessionClerk]); /* Print("\nCustomer[%d] in Group[%d] is walking up to ConcessionClerk[%d] to buy [%d] popcorn and [%d] soda.\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,noOfPopcorn[myConcessionClerk] PRINTMETOO noOfSoda[myConcessionClerk]); */ localNoOfPopcorn = GetMV("noOfPopcorn",myConcessionClerk); localNoOfSoda = GetMV("noOfSoda",myConcessionClerk); Print("\nCustomer[%d] in Group[%d] is walking up to ConcessionClerk[%d] to buy [%d] popcorn and [%d] soda.\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,localNoOfPopcorn PRINTMETOO localNoOfSoda); /* Wait(concessionClerkCVInt[myConcessionClerk],concessionClerkLockInt[myConcessionClerk]); /*Waiting for clerk to tell him the amount*/ Wait(concessionClerkCVChar[myConcessionClerk],concessionClerkLockChar[myConcessionClerk]); /* foodAmount=amountSoda[myConcessionClerk]+amountPopcorn[myConcessionClerk]; */ localAmountSoda = GetMV("amountSoda",myConcessionClerk); localAmountPopcorn = GetMV("amountPopcorn",myConcessionClerk); foodAmount = localAmountSoda + localAmountPopcorn; Print("\nCustomer[%d] in Group[%d] in ConcessionClerk line[%d] is paying[%d] for food.\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,foodAmount PRINTMETOO 1); /* Signal(concessionClerkCVInt[myConcessionClerk],concessionClerkLockInt[myConcessionClerk]); */ Signal(concessionClerkCVChar[myConcessionClerk],concessionClerkLockChar[myConcessionClerk]); /*Giving the amount, now waiting his confirmation to leave*/ /* Wait(concessionClerkCVInt[myConcessionClerk],concessionClerkLockInt[myConcessionClerk]); */ Wait(concessionClerkCVChar[myConcessionClerk],concessionClerkLockChar[myConcessionClerk]); Print("\nCustomer[%d] in Group [%d] is leaving ConcessionClerk[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO myConcessionClerk,1); /* Release(concessionClerkLockInt[myConcessionClerk]); */ Release(concessionClerkLockChar[myConcessionClerk]); /* /////////////////// Done With CC ////////////////// */ /************************ Ticket Taker Interaction **************************/ /*Ticket Taker and Head Customer Interaction*/ /* Acquire(ticketTakerLineLockInt); */ Acquire("ttLineLock"); while(1) /*It will run until break happens when it is allowed to go in the theater.*/ { /* Monitor variable to check the state of TT. If he is taking tickets then go to TTLineCV else go to LobbyCV. */ /* isTakingTickets=0; */ SetMV("isTakingTickets",0,0); /* for (i=0;i<noOfTT;i++) */ localNoOfTT = GetMV("noOfTT",0); for (i=0;i<localNoOfTT;i++) { /* if (ticketTakerState[i]!=-1) */ localTicketTakerState = GetMV("ticketTakerState",i); if (localTicketTakerState!=-1) { /* isTakingTickets=1; /*I got a TT who is taking tickets.*/ SetMV("isTakingTickets",0,1); break; } } /* if (isTakingTickets != 1) */ localIsTakingTickets = GetMV("isTakingTickets",0); if (localIsTakingTickets!=1) { /* Release(ticketTakerLineLockInt); /*I should add myself to the Lobby queue.*/ Release("ttLineLock"); /* Acquire(customerLobbyLockInt); */ Acquire("customerLobbyLock"); Print("\nCustomer[%d] in Group[%d] is in the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(customerLobbyCVInt,customerLobbyLockInt); */ Wait("customerLobbyCV","customerLobbyLock"); Print("\nCustomer[%d] in Group[%d] is leaving the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Release(customerLobbyLockInt); */ Release("customerLobbyLock"); /* Acquire(ticketTakerLineLockInt); */ Acquire("ttLineLock"); continue; } /* Theatre is not full. Fighting for the shortest TT line. */ myTicketTaker=-1; /* for (i=0;i<noOfTT;i++) /*see if any ticket taker is not busy*/ localNoOfTT = GetMV("noOfTT",0); for (i=0;i<localNoOfTT;i++) { /* if (ticketTakerState[i]==0) */ localTicketTakerState = GetMV("ticketTakerState",i); if (localTicketTakerState==0) { myTicketTaker=i; /*a TT is not busy, I dont have to wait*/ /* ticketTakerState[i]=1; */ SetMV("ticketTakerState",i,1); Print("\nCustomer[%d] in Group[%d] is getting in TicketTaker Line[%d] \n",myCustomerNumber,myGroupNumber PRINTMETOO myTicketTaker,1); break; } } if (myTicketTaker==-1) /*finding the TT with shortest Line*/ { /* shortestTTLineLen = ticketTakerLineCount[0]; */ shortestTTLineLen = GetMV("ttLineCnt",0); /* for(i=0;i<noOfTT;i++) */ localNoOfTT = GetMV("noOfTT",0); for(i=0;i<localNoOfTT;i++) { /* if(ticketTakerState[i]!=-1) */ localTicketTakerState = GetMV("ticketTakerState",i); if(localTicketTakerState!=-1) { shortestTTLine=i; /* shortestTTLineLen=ticketTakerLineCount[i];*/ shortestTTLineLen = GetMV("ttLineCnt",i); break; } } /* for (i=1; i<noOfTT; i++) */ localNoOfTT = GetMV("noOfTT",0); for (i=1; i<localNoOfTT; i++) { /*checking if the TT is busy and his line is shorter*/ /* if (ticketTakerState[i]==1 && (shortestTTLineLen > ticketTakerLineCount[i])) */ localTicketTakerState = GetMV("ticketTakerState",i); localTicketTakerLineCount = GetMV("ttLineCnt",i); if (localTicketTakerState==1 && (shortestTTLineLen > localTicketTakerLineCount)) { shortestTTLine=i; /* shortestTTLineLen > ticketTakerLineCount[i]; */ /* shortestTTLineLen = localTicketTakerLineCount; */ shortestTTLineLen > localTicketTakerLineCount; } } myTicketTaker=shortestTTLine; /* I have the shortest Line */ /* total++; */ localTotal = GetMV("total",0); SetMV("total",0,localTotal+1); /* ticketTakerLineCount[myTicketTaker]++; /* Get in that Line */ localTicketTakerLineCount = GetMV("ttLineCnt",myTicketTaker); SetMV("ttLineCnt",myTicketTaker,localTicketTakerLineCount+1); Print("\nCustomer[%d] in Group[%d] is getting in TicketTaker Line[%d] \n",myCustomerNumber,myGroupNumber PRINTMETOO myTicketTaker,1); /* Wait(ticketTakerLineCVInt[myTicketTaker],ticketTakerLineLockInt); */ Wait(ticketTakerLineCVChar[myTicketTaker],"ttLineLock"); /*Signalled by TT, now check, whether to go back to lobby?*/ /* if (ticketTakerGroupIndicator[myTicketTaker]==0) */ localTicketTakerGroupIndicator = GetMV("TTGroupInd",myTicketTaker); if (localTicketTakerGroupIndicator==0) { /*If that TT has stopped taking tickets and asking to go in Lobby*/ Print("\nCustomer[%d] in Group[%d] sees TicketTaker[%d] is no longer taking tickets. Going to the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO myTicketTaker,1); /* countCustTT++; */ localCountCustTT = GetMV("countCustTT",0); SetMV("countCustTT",0,localCountCustTT+1); /* Release(ticketTakerLineLockInt); */ Release("ttLineLock"); /* Acquire(customerLobbyLockInt); */ Acquire("customerLobbyLock"); Print("\nCustomer[%d] in Group[%d] is in the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(customerLobbyCVInt,customerLobbyLockInt); */ Wait("customerLobbyCV","customerLobbyLockChar"); Print("\nCustomer[%d] in Group[%d] is leaving the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Release(customerLobbyLockInt); */ Release("customerLobbyLock"); /* Acquire(ticketTakerLineLockInt); /* When manager wakes me up from the lobby */ Acquire("ttLineLock"); /* countCustTT--; */ localCountCustTT = GetMV("countCustTT",0); SetMV("countCustTT",0,localCountCustTT-1); /* total--; */ localTotal = GetMV("total",0); SetMV("total",0,localTotal-1); continue; } else { /* Release(ticketTakerLineLockInt); */ Release("ttLineLock"); } } /* end of if */ else { /* total++; */ localTotal = GetMV("total",0); SetMV("total",0,localTotal+1); /* Release(ticketTakerLineLockInt); */ Release("ttLineLock"); } /*Time to interact with the Ticket Taker*/ /* Acquire(ticketTakerLockInt[myTicketTaker]); */ Acquire(ticketTakerLockChar[myTicketTaker]); /* custNoOfTicket[myTicketTaker]=myGroupSize+1; /* 3 should be changed to the actual dynamic statement */ SetMV("custNoOfTicket",myTicketTaker,myGroupSize+1); /* Giving my tickets to TT */ /* If theatre is full then ticketTakerTheatreCountStatus is 1. he wont entertain more customers. */ /* Signal(ticketTakerCVInt[myTicketTaker],ticketTakerLockInt[myTicketTaker]); */ Signal(ticketTakerCVChar[myTicketTaker],ticketTakerLockChar[myTicketTaker]); /* Acquire(ticketTakerLineLockInt); */ Acquire("ttLineLock"); /* countCustTT++; */ localCountCustTT = GetMV("countCustTT",0); SetMV("countCustTT",0,localCountCustTT+1); /* Release(ticketTakerLineLockInt); */ Release("ttLineLock"); /* Wait(ticketTakerCVInt[myTicketTaker],ticketTakerLockInt[myTicketTaker]); */ Wait(ticketTakerCVChar[myTicketTaker],ticketTakerLockChar[myTicketTaker]); /*I have been given an indicator value. Check that. It its 1, I can go inside else I have to wait in the lobby.*/ /* if (ticketTakerGroupIndicator[myTicketTaker]==1) */ localTicketTakerGroupIndicator = GetMV("TTGroupInd",myTicketTaker); if (localTicketTakerGroupIndicator==1) { /* Release(ticketTakerLockInt[myTicketTaker]); /* Go inside the theater */ Release(ticketTakerLockChar[myTicketTaker]); Print("\nCustomer[%d] in Group[%d] is leaving TicketTaker[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO myTicketTaker,1); /* Adding myself to theatre CV by the theatre lock. */ break; /*exit from the while loop. You already are inside the theater.*/ } /* Release(ticketTakerLockInt[myTicketTaker]); */ Release(ticketTakerLockChar[myTicketTaker]); Print("\nCustomer[%d] in Group[%d] is rejected by TicketTaker[%d]. Going to the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO myTicketTaker,1); /* Acquire(ticketTakerLineLockInt); */ Acquire("ttLineLock"); /* Release(ticketTakerLineLockInt); */ Release("ttLineLock"); /* Acquire(customerLobbyLockInt); */ Acquire("customerLobbyLock"); Print("\nCustomer[%d] in Group[%d] is in the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(customerLobbyCVInt,customerLobbyLockInt); */ Wait("customerLobbyCV","customerLobbyLock"); Print("\nCustomer[%d] in Group[%d] is leaving the lobby.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Release(customerLobbyLockInt); */ Release("customerLobbyLock"); /* Acquire(ticketTakerLineLockInt); */ Acquire("ttLineLock"); /* countCustTT--; */ localCountCustTT = GetMV("countCustTT",0); SetMV("countCustTT",0,localCountCustTT-1); /* total--; */ localTotal = GetMV("total",0); SetMV("total",0,localTotal-1); } /*End of while statement*/ if(myGroupSize != 0) /* Forming the Group to go inside theater room. */ { /* Acquire(groupLockInt[myGroupNumber]); */ Acquire(groupLockChar[myGroupNumber]); /* canGoInsideTheater[myGroupNumber]=1; */ SetMV("canGoInsideTheater",myGroupNumber,1); Print("\nHeadCustomer[%d] of group[%d] has told the group to proceed.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Broadcast(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Broadcast(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); /* Acquire(movieTheaterLockInt); */ Acquire("movieTheaterLock"); /* Release(groupLockInt[myGroupNumber]); */ Release(groupLockChar[myGroupNumber]); } else { /* Acquire(movieTheaterLockInt); */ Acquire("movieTheaterLock"); } /* Customer group can go inside the theater room.*/ /* custEnteredTheater++; */ localCustEnteredTheater = GetMV("custEnteredTheater",0); SetMV("custEnteredTheater",0,localCustEnteredTheater+1); /*$$$$$$$$$$$$$$$$$$$ SEAT FUNCTIONALITY REMOVED*/ /*AssignSeat(myGroupSize+1, myGroupNumber); /*Calling function for assigning the seats to the group*/ /*MySeat(myGroupNumber); /*Calling function to view the assigned seat*/ /* counterVal[myGroupNumber]++; */ localCounterVal = GetMV("counterVal",myGroupNumber); SetMV("counterVal",myGroupNumber,localCounterVal+1); /* Print("\nCustomer[%d] in Group[%d] has found the following seat: row[%d] and seat[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO rowNo,columnNo PRINTMETOO 1); */ localRowNo = GetMV("rowNo",0); localColumnNo = GetMV("columnNo",0); Print("\nCustomer[%d] in Group[%d] has found the following seat: row[%d] and seat[%d]\n",myCustomerNumber,myGroupNumber PRINTMETOO localRowNo,localColumnNo PRINTMETOO 1); Print("\nCustomer[%d] of group[%d] is sitting in a theater room seat.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(movieTheaterCVInt,movieTheaterLockInt); */ Wait("movieTheaterCV","movieTheaterLock"); /*Movie Over*/ /* Acquire(groupLockInt[myGroupNumber]); */ Acquire(groupLockChar[myGroupNumber]); /* Release(movieTheaterLockInt); */ Release("movieTheaterLock"); if(myGroupSize != 0) { Print("\nHeadCustomer[%d] of group[%d] is waiting for the group to form.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Wait(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); Print("\nHeadCustomer[%d] of group[%d] has told the group to proceed.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /*Asking group to proceed*/ /* Broadcast(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Broadcast(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); } /* Acquire(movieTheaterLockInt); */ Acquire("movieTheaterLock"); /* Release(groupLockInt[myGroupNumber]); */ Release(groupLockChar[myGroupNumber]); /* custLeavingTheater++; /*Leaving Theater*/ localCustLeavingTheater = GetMV("custLeavingTheater",0); SetMV("custLeavingTheater",0,localCustLeavingTheater+1); /* Acquire(groupLockInt[myGroupNumber]); */ Acquire(groupLockChar[myGroupNumber]); /* Release(movieTheaterLockInt); */ Release("movieTheaterLock"); bathRoomValue = Rand(100); /*Went out of theater after re-grouping, now broadcasting for bathroom*/ bathRoomValue = bathRoomValue+1; if(bathRoomValue<=25) { Print("\nCustomer[%d] in Group[%d] is going to the bathroom.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Acquire(bathroomLockInt); */ Acquire("bathroomLock"); Yield(); /* Release(bathroomLockInt); */ Release("bathroomLock"); Print("\nCustomer[%d] in Group[%d] is leaving the bathroom.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); } /* canGoBathroom[myGroupNumber]=1; /*broadcast in groupCV for bathroom*/ SetMV("canGoBathroom",myGroupNumber,1); if(myGroupSize != 0) { /* Broadcast(groupCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Broadcast(groupCVChar[myGroupNumber],groupLockChar[myGroupNumber]); /*Waiting in bathroom Lobby*/ Print("\nHeadCustomer[%d] of group[%d] is waiting for the group to form.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Wait(bathroomLobbyCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Wait(bathroomLobbyCVChar[myGroupNumber],groupLockChar[myGroupNumber]); /*All group members have returned from bathroom, so signal them to go.*/ Print("\nHeadCustomer[%d] of group[%d] has told the group to proceed.\n",myCustomerNumber,myGroupNumber PRINTMETOO 1,1); /* Broadcast(bathroomLobbyCVInt[myGroupNumber],groupLockInt[myGroupNumber]); */ Broadcast(bathroomLobbyCVChar[myGroupNumber],groupLockChar[myGroupNumber]); } /* Release(groupLockInt[myGroupNumber]); */ Release(groupLockChar[myGroupNumber]); /* Acquire(custFinalExitLockInt); */ Acquire("custFinalExitLock"); /* custFinalExitCount++; */ localCustFinalExitCount = GetMV("custFinalExitCount",0); SetMV("custFinalExitCount",0,localCustFinalExitCount+1); /* Release(custFinalExitLockInt); */ Release("custFinalExitLock"); }
int main() { int lockStatus, cvStatus, mvStatus, waitStatus; 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); } cvStatus = CreateCV("cv1",3); if(cvStatus == -1) { WriteToConsole("\nError Occurred while creating CV. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nCV Created by the server\n\0", -1 , -1, -1); } mvStatus = CreateMV("mv1", 3, 1); 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\n",-1,-1,-1); lockStatus = Acquire("lock1",5); if(lockStatus == -1) { WriteToConsole("\nFailed to Acquire Lock. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nAcquired Lock\n\0", -1, -1, -1); } WriteToConsole("\nSetting MV1 to 100.\n",-1,-1,-1); SetMV("mv1", 3, 0, 100); WriteToConsole("\nWaiting on the condition cv1 with lock1.\n",-1,-1,-1); waitStatus = Wait("cv1",3,"lock1",5); if(lockStatus == -1) { WriteToConsole("\nServer failed to wait. Exiting program.\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nClient 1 is waiting. Start client 2 to signal.\0n", -1, -1, -1); } WriteToConsole("\nGot signal from client 2.\n\0", -1, -1, -1); WriteToConsole("\nReleasing Lock\n\0", -1, -1, -1); lockStatus = Release("lock1",5); if(lockStatus == -1) { WriteToConsole("\nFailed to Release Lock. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nReleased Lock\n\0", -1, -1, -1); } WriteToConsole("\nDeleting Lock\n\0", -1, -1, -1); lockStatus = DeleteLock("lock1",5); if(lockStatus == -1) { WriteToConsole("\nFailed to Delete Lock. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nDeleted Lock\n\0", -1, -1, -1); } Exit(0); }
void appClerkRun(int id) { while(GetMV(doneFlag, 0) == 0) { Acquire(lineLock); if(GetMV(senatorFlag, 0) == 1) { /* if a senator is in the building */ if(GetMV(appClerkLineCount, id) > 0) { /* wake up the customers so they leave the office */ Broadcast(appClerkMutex[id].lineCV, lineLock); } if(GetMV(appClerkBLineCount, id) > 0) { /* wake up the customers so they leave the office */ Broadcast(appClerkMutex[id].bribeLineCV, lineLock); } if(GetMV(appClerkSenInLine, id) == 1) { Signal(appClerkMutex[id].senatorLineCV, lineLock); /* wake the sleeping senator thread */ Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has signalled a Senator to come to their counter.\n", 51); Release(outputLock); appClerkInteract(id, true); } else { /* else if a senator is here and not in this line */ SetMV(appClerkState, id, 2); Release(lineLock); Acquire(appClerkMutex[id].clerkLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" is going on break.\n", 20); Release(outputLock); /* wait/go on break until the manager wakes this clerk */ Wait(appClerkMutex[id].clerkCV, appClerkMutex[id].clerkLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" is coming off of break.\n", 25); Release(outputLock); SetMV(appClerkState, id, 0); Release(appClerkMutex[id].clerkLock); } } else if(GetMV(appClerkBLineCount, id) > 0) { Signal(appClerkMutex[id].bribeLineCV, lineLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has signalled a Customer to come to their counter.\n", 52); Release(outputLock); appClerkInteract(id, false); } else if(GetMV(appClerkLineCount, id) > 0){ Signal(appClerkMutex[id].lineCV, lineLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has signalled a Customer to come to their counter.\n", 52); Release(outputLock); appClerkInteract(id, false); } else { /* go on break */ SetMV(appClerkState, id, 2); Release(lineLock); Acquire(appClerkMutex[id].clerkLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" is going on break.\n", 20); Release(outputLock); /* wait/go on break until the manager wakes this clerk */ Wait(appClerkMutex[id].clerkCV, appClerkMutex[id].clerkLock); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" is coming off of break.\n", 25); Release(outputLock); SetMV(appClerkState, id, 0); Release(appClerkMutex[id].clerkLock); } } }
void Initialize() { int i,j; int localGroupSize; int localNumberOfGroups; int concessionClerkStateRV; int ticketClerkStateRV; int ttTakingTicketsRV; int ticketTakerStateRV; int ticketTakerGroupIndicatorRV; int ticketTakerTheatreCountStatusRV; int localTotalCustomerNumber; int availableCustSizeForGroupArrRV; CreateLock("InitLock"); Acquire("InitLock"); /* int concessionClerkState[GlobalnoOfCC]; /*Stores the Concession Clerks State*/ concessionClerkStateRV = CreateMV("ccState",GlobalnoOfCC); if(concessionClerkStateRV==1) { for(i=0;i<GlobalnoOfCC;i++) { SetMV("ccState",i,1); } } /* int ticketClerkState[GlobalnoOfTC]; /* Stores the state of each Ticket Clerk */ ticketClerkStateRV = CreateMV("ticketClerkState",GlobalnoOfTC); if(ticketClerkStateRV==1) { for(i=0;i<GlobalnoOfTC;i++) { SetMV("ticketClerkState",i,1); } } /* int ttTakingTickets; /*Stores if TT are taking tickets*/ ttTakingTicketsRV = CreateMV("ttTakingTickets",1); if(ttTakingTicketsRV==1) { SetMV("ttTakingTickets",0,1); } /* int ticketTakerState[GlobalnoOfTT]; /*State of each TT(Ticket Taker)*/ ticketTakerStateRV = CreateMV("ticketTakerState",GlobalnoOfTT); if(ticketTakerStateRV==1) { for(i=0;i<GlobalnoOfTT;i++) { SetMV("ticketTakerState",i,1); } } /* int ticketTakerGroupIndicator[GlobalnoOfTT]; /*Indicates a group whether to go inside or wait in lobby*/ ticketTakerGroupIndicatorRV = CreateMV("TTGroupInd",GlobalnoOfTT); if(ticketTakerGroupIndicatorRV==1) { for(i=0;i<GlobalnoOfTT;i++) { SetMV("TTGroupInd",i,1); } } /* int ticketTakerTheatreCountStatus=1; /*count for the ticket taker*/ ticketTakerTheatreCountStatusRV = CreateMV("ttTheCount",1); if(ticketTakerTheatreCountStatusRV==1) { SetMV("ttTheCount",0,1); } /* int availableCustSizeForGroupArr[50]; /*Array for each customer to determine its group*/ availableCustSizeForGroupArrRV=CreateMV("avCustGrpArr",50); for(i=0;i<GlobalnoOfGroups;i++) { if(availableCustSizeForGroupArrRV==1) { SetMV("avCustGrpArr",i,GlobalGroupNormalCustSize); } } Release("InitLock"); /*******************************************************************************************************/ /* int totalCustomerNumber; /* Total Number of Customers */ CreateMV("totCustNum",1); /* int noOfGroups; /* Stores the number of groups generated */ CreateMV("noOfGroups",1); SetMV("noOfGroups",0,GlobalnoOfGroups); /* %%%%%%%%%%%%%% For the time being. %%%%%%%%%%%%%%%% */ /* int noOfTC;*/ CreateMV("noOfTC",1); SetMV("noOfTC",0,GlobalnoOfTC); /* int noOfCC;*/ CreateMV("noOfCC",1); SetMV("noOfCC",0,GlobalnoOfCC); /* int noOfTT;*/ CreateMV("noOfTT",1); SetMV("noOfTT",0,GlobalnoOfTT); /* int noOfCustomers;*/ CreateMV("noOfCustomers",1); /* int groupSize;*/ CreateMV("groupSize",1); /* int totalAmtTicket;*/ /* Total Amount Variable */ CreateMV("totalAmtTicket",1); /* int noOfTickets[GlobalnoOfTC]; /* Keep track of no. of tickets sold by each clerk */ CreateMV("noOfTickets",GlobalnoOfTC); /* int ticketClerkAmount[GlobalnoOfTC]; /* Amount given by customer at each TicketClerk */ CreateMV("ticketClerkAmount",GlobalnoOfTC); /* int totalAmountTicketClerk[GlobalnoOfTC]; /* Amount deposited at each TicketClerk */ CreateMV("totAmtTC",GlobalnoOfTC); /* int ticketClerkOnBreak=0; /* Count of the Ticket Clerks on break*/ CreateMV("tcOnBreak",1); /* int managerTcWait[GlobalnoOfTC]; /* Manager signal for each Ticket Clerk*/ CreateMV("managerTcWait",GlobalnoOfTC); /* int custFindLineTC[GlobalnoOfTC]; /* Variable to say that broadcast customers*/ CreateMV("custFindLineTC",GlobalnoOfTC); /* int reFindLineTC[GlobalnoOfTC]; /*Flag to say that the customer has to find line again*/ CreateMV("reFindLineTC",GlobalnoOfTC); /* int totalAmountConcessionClerk[GlobalnoOfCC]; /*Total Amount stored by all Concession Clerks*/ CreateMV("totAmtCC",GlobalnoOfCC); /* int concessionClerkLineCount[GlobalnoOfCC]; /*Stores the Concession Clerks Line Count*/ CreateMV("ccLineCnt",GlobalnoOfCC); /* $$$$$$$$ Ticket Clerk Variables $$$$$$$$ */ /* int ticketClerkLineCount[GlobalnoOfTC]; /* Stores the Line Count of each Ticket Clerk */ CreateMV("tcLineCnt",GlobalnoOfTC); /* int ticketClerkLineCVInt[GlobalnoOfTC]; */ /* char ticketClerkLineCVChar[GlobalnoOfTC][200]; */ for(i=0;i<GlobalnoOfTC;i++) { Sprint(ticketClerkLineCVChar[i],"TCLineCV",i); /*changed this $$$$$$$$$$$$$$$$$$$$*/ CreateCV(ticketClerkLineCVChar[i]); } /* int ticketClerkLineLockInt; */ CreateLock("tcLineLock"); /* int totalAmtTicketLockInt; */ CreateLock("toAmtTktLck"); /* int ticketClerkCVInt[GlobalnoOfTC]; */ /* char ticketClerkCVChar[GlobalnoOfTC][200]; */ for(i=0;i<GlobalnoOfTC;i++) { Sprint(ticketClerkCVChar[i],"TCCV",i); CreateCV(ticketClerkCVChar[i]); } /* int ticketClerkLockInt[GlobalnoOfTC]; */ /* char ticketClerkLockChar[GlobalnoOfTC][200]; */ for(i=0;i<GlobalnoOfTC;i++) { Sprint(ticketClerkLockChar[i],"TCLock",i); CreateLock(ticketClerkLockChar[i]); } /* int concessionClerkLineCVInt[GlobalnoOfCC]; */ /* char concessionClerkLineCVChar[GlobalnoOfCC][200]; */ for(i=0;i<GlobalnoOfCC;i++) { Sprint(concessionClerkLineCVChar[i],"CCLineCV",i); CreateCV(concessionClerkLineCVChar[i]); } /* int concessionClerkLineLockInt; */ CreateLock("ccLineLock"); /* int totalAmtPopSodaLockInt; */ CreateLock("toAmtPopLck"); /* int noOfSoda[GlobalnoOfCC]; /*customer Vaiable to store soda value on a Concession Clerk*/ CreateMV("noOfSoda",GlobalnoOfCC); /* int noOfPopcorn[GlobalnoOfCC]; /*customer Vaiable to store popcorn value on a Concession Clerk*/ CreateMV("noOfPopcorn",GlobalnoOfCC); /* int totalMoneySoda; /*total money by Soda*/ CreateMV("totalMoneySoda",1); /* int totalMoneyPopcorn; /*total money by popcorn*/ CreateMV("totalMoneyPopcorn",1); /* int amountSoda[GlobalnoOfCC]; /*Amount customer has to pay for Soda*/ CreateMV("amountSoda",GlobalnoOfCC); /* int amountPopcorn[GlobalnoOfCC]; /*Amount customer has to pay for Popcorn*/ CreateMV("amountPopcorn",GlobalnoOfCC); /* int concessionClerkCVInt[GlobalnoOfCC]; */ /* char concessionClerkCVChar[GlobalnoOfTC][200]; */ for(i=0;i<GlobalnoOfTC;i++) { Sprint(concessionClerkCVChar[i],"CCCV",i); CreateCV(concessionClerkCVChar[i]); } /* int concessionClerkLockInt[GlobalnoOfCC]; */ /* char concessionClerkLockChar[GlobalnoOfCC][200]; */ for(i=0;i<GlobalnoOfCC;i++) { Sprint(concessionClerkLockChar[i],"CCLock",i); CreateLock(concessionClerkLockChar[i]); } /* int conClerkOnBreak=0; /*Count of CCs on break*/ CreateMV("conClerkOnBreak",1); /* int managerCcWait[GlobalnoOfCC]; /*Flag for wait of CC*/ CreateMV("managerCcWait",GlobalnoOfCC); /* int custFindLineCC[GlobalnoOfCC]; /*Variable of CC to tell customer to find line*/ CreateMV("custFindLineCC",GlobalnoOfCC); /* int reFindLineCC[GlobalnoOfCC]; /*Flag for customer to find line again*/ CreateMV("reFindLineCC",GlobalnoOfCC); /*********Variables for Technician Manager and Customer Interaction*********/ /* int movieTheaterLockInt; */ CreateLock("movieTheaterLock"); /* int movieTheaterCVInt; */ CreateCV("movieTheaterCV"); /* int custEnteredTheater; /*Count of Cust entering the theater room*/ CreateMV("custEnteredTheater",1); /* int custLeavingTheater; /*Count of Cust leaving the theater room*/ CreateMV("custLeavingTheater",1); /* int isMovieOver; /* Stores the movie state*/ CreateMV("isMovieOver",1); /* int movieTechLockInt; /* Lock for Movie Technician */ CreateLock("movieTechLock"); /* int movieTechCVInt; /* Conditon Variable for Technician*/ CreateCV("movieTechCV"); /* int custFinalExitLockInt; /* Lock for count of finally exiting customers*/ CreateLock("custFinalExitLock"); /* int custFinalExitCount; /*Variable for count of finally exiting customers*/ CreateMV("custFinalExitCount",1); /*/////////////////////////////Ticket Taker//////////////////////////////////*/ /* int custTicketsApproved=0; /*Stores the no of tickets approved to go inside theater room*/ CreateMV("custTicketsApproved",1); /* int ticketTakerLineCount[GlobalnoOfTT]; /*Stores the Line count of each TT*/ CreateMV("ttLineCnt",GlobalnoOfTT); /* int custNoOfTicket[GlobalnoOfTT]; /*Number of tickets of each group*/ CreateMV("custNoOfTicket",GlobalnoOfTT); /* int ticketTakerLineLockInt; /* Lock for the TT Line*/ CreateLock("ttLineLock"); /* int ticketTakerLockInt[GlobalnoOfTT]; /* Lock for each TT*/ /* char ticketTakerLockChar[GlobalnoOfTT][200]; */ for(i=0;i<GlobalnoOfTT;i++) { Sprint(ticketTakerLockChar[i],"TTLock",i); CreateLock(ticketTakerLockChar[i]); } /* int ticketTakerManagerLockInt; /* TicketTaker Manager Lock*/ CreateLock("ttMgrLock"); /* int custTicketsApprovedLockInt; /* Lock for total tickets approved to go inside*/ CreateLock("TktsAppLock"); /* int ticketTakerLineCVInt[GlobalnoOfTT]; /* CV for each TT Line*/ /* char ticketTakerLineCVChar[GlobalnoOfTT][200]; */ for(i=0;i<GlobalnoOfTT;i++) { Sprint(ticketTakerLineCVChar[i],"TTLineCV",i); CreateCV(ticketTakerLineCVChar[i]); } /* int ticketTakerCVInt[GlobalnoOfTT]; /* CV for each TT interaction*/ /* char ticketTakerCVChar[GlobalnoOfTT][200]; */ for(i=0;i<GlobalnoOfTT;i++) { Sprint(ticketTakerCVChar[i],"TTCV",i); CreateCV(ticketTakerCVChar[i]); } /* int ticketTakerManagerCVInt; /* CV for Technician */ CreateCV("ttMgrCV"); /* int customerLobbyCVInt; /*CV for Lobby*/ CreateCV("customerLobbyCV"); /* int customerLobbyLockInt; /*Lock for Lobby*/ CreateLock("customerLobbyLock"); /* int isTakingTickets; /*Stores whether the TT is taking tickets or not*/ CreateMV("isTakingTickets",1); /* int ttWaitShowOverLockInt[GlobalnoOfTT]; /*Lock for TT till the next show starts*/ /* char ttWaitShowOverLockChar[GlobalnoOfTT][200]; */ for(i=0;i<GlobalnoOfTT;i++) { Sprint(ttWaitShowOverLockChar[i],"ttWSOLock",i); CreateLock(ttWaitShowOverLockChar[i]); } /* int ttWaitShowOverCVInt[GlobalnoOfTT]; /*CV for waiting of TT till next show start*/ /* char ttWaitShowOverCVChar[GlobalnoOfTT][200]; */ for(i=0;i<GlobalnoOfTT;i++) { Sprint(ttWaitShowOverCVChar[i],"ttWSOCV",i); CreateCV(ttWaitShowOverCVChar[i]); } /***************************Variables for Group Interaction*********************************/ /* int groupLockInt[GlobalnoOfGroups]; /*Group Lock for each group*/ /* char groupLockChar[GlobalnoOfGroups][200]; */ for(i=0;i<GlobalnoOfGroups;i++) { Sprint(groupLockChar[i],"groupLock",i); CreateLock(groupLockChar[i]); } /* int groupCVInt[GlobalnoOfGroups]; /* CV for each group*/ /* char groupCVChar[GlobalnoOfGroups][200]; */ for(i=0;i<GlobalnoOfGroups;i++) { Sprint(groupCVChar[i],"groupCV",i); CreateCV(groupCVChar[i]); } /* int canGoInsideTheater[GlobalnoOfGroups]; /*Stores whether Cust can go inside*/ CreateMV("canGoInsideTheater",GlobalnoOfGroups); /* int canGoBathroom[GlobalnoOfGroups]; /*Stores whtehr cust can go to bathroom*/ CreateMV("canGoBathroom",GlobalnoOfGroups); /* int counterAfterMuvi[GlobalnoOfGroups]; /* stores the count of cust got up after movie*/ CreateMV("counterAfterMuvi",GlobalnoOfGroups); /* int counterAfterBathroom[GlobalnoOfGroups]; /* stores the count of cust going to bathroom*/ CreateMV("ctrAfterBath",GlobalnoOfGroups); /* int bathroomLobbyCVInt[GlobalnoOfGroups]; /* CV for use when other cust has to wait when 1 goes to bathroom*/ /* char bathroomLobbyCVChar[GlobalnoOfGroups][200]; */ for(i=0;i<GlobalnoOfGroups;i++) { Sprint(bathroomLobbyCVChar[i],"bLobbyCV",i); CreateCV(bathroomLobbyCVChar[i]); } /* int bathroomLockInt; /* Lock for bathroom*/ CreateLock("bathroomLock"); /* int ttWaitingAfterFull[GlobalnoOfTT]; */ CreateMV("ttWaitingAfterFull",GlobalnoOfTT); /* int managerWokeTT[GlobalnoOfTT]; /*manager waking up TT to start taking ticket*/ CreateMV("managerWokeTT",GlobalnoOfTT); /* int countCustTT; /*count of cust going to TT*/ CreateMV("countCustTT",1); /* int total; */ CreateMV("total",1); /* int totalHeadCust; /*Count of all head customer*/ CreateMV("totalHeadCust",1); /* int groupPopcorn[GlobalnoOfGroups]; /*popcorn by each group*/ CreateMV("groupPopcorn",GlobalnoOfGroups); /* int groupSoda[GlobalnoOfGroups]; /* Soda ordered by each group*/ CreateMV("groupSoda",GlobalnoOfGroups); /* int haveTickets[GlobalnoOfGroups]; /*Stores whtehr the head have tickets or not*/ CreateMV("haveTickets",GlobalnoOfGroups); /* int givenFoodChoice[GlobalnoOfGroups]; /*sores whther give the food choice*/ CreateMV("givenFoodChoice",GlobalnoOfGroups); /*Seat variabes*/ /* int rowNo,columnNo; /*row number and column number of seat*/ CreateMV("rowNo",1); CreateMV("columnNo",1); /* int counterVal[GlobalnoOfGroups]; /*counter for each customer accessing seat*/ CreateMV("counterVal",GlobalnoOfGroups); /* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ /* int movieRoomSeatNoArray[5][5]; /*array for storing seat avaible or not int movieRoomGrpNoArray[5][5]; /*array for storing seat assigned to which group */ /* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ /* int sizeForGroupArr[50]; /*Storing the group size for each group*/ CreateMV("sizeForGroupArr",50); /*Global Part for TC myIndex value*/ /* int ticketClerkIndexLockInt; */ CreateLock("ticketClerkIndexLock"); /* int nextTCIndex=0; */ CreateMV("nextTCIndex",1); /*Global Part for Customer value*/ /* int groupNumberLockInt, customerNumberLockInt, groupSizeLockInt; */ CreateLock("groupNumberLock"); CreateLock("customerNumberLock"); CreateLock("groupSizeLock"); /* int nextGroupNumber=0, nextCustomerNumber=0, nextGroupSize=0; */ CreateMV("nextGroupNumber",1); CreateMV("nextCustomerNumber",1); CreateMV("nextGroupSize",1); /*Global Part for CC myIndex value*/ /* int concessionClerkIndexLockInt; */ CreateLock("ccIndexLock"); /* int nextCCIndex=0; */ CreateMV("nextCCIndex",1); /* int determineCustGroupNumberLockInt; */ CreateLock("custGrpNumLock"); /*Global Part for TT myIndex value*/ /* int ticketTakerIndexLockInt; */ CreateLock("ticketTakerIndexLock"); /* int nextTTIndex=0; */ CreateMV("nextTTIndex",1); /* int checkFinalExitLockInt; */ CreateLock("checkFinalExitLock"); /* int finalExitFlag=0; */ CreateMV("finalExitFlag",1); /* numberOfGroups=noOfGroups;*/ /*$$$$$$$$$$$$$$$$$ From Main Function $$$$$$$$$$$$$$$$$*/ localNumberOfGroups=GetMV("noOfGroups",0); /* Acquire("InitLock"); /* ############################################################### */ /* totalHeadCust=numberOfGroups;*/ SetMV("totalHeadCust",0,localNumberOfGroups); SetMV("totCustNum",0,GlobalnoOfGroups*(GlobalGroupNormalCustSize+1)); for(i=0;i<localNumberOfGroups;i++) { /* groupSize=Rand(5);*/ /*localGroupSize = Rand(5);*/ /*$$$$$$$$$$$$$$$$$ Hardcoded to 0 $$$$$$$$$$$$$$$$$*/ localGroupSize=0; SetMV("groupSize",0,GlobalGroupNormalCustSize); /* sizeForGroupArr[i]=groupSize; /*Assigning group size*/ localGroupSize = GetMV("groupSize",0); SetMV("sizeForGroupArr",i,localGroupSize); /* availableCustSizeForGroupArr[i]=groupSize; */ /* if(availableCustSizeForGroupArrRV==1) { SetMV("avCustGrpArr",i,localGroupSize); //Removed from here to top, inside the lock. } */ /* totalCustomerNumber++;*/ localTotalCustomerNumber=GetMV("totCustNum",0); /* SetMV("totCustNum",0,localTotalCustomerNumber+1);*/ for (j=0;j<localGroupSize;j++ ) { /* totalCustomerNumber++; */ localTotalCustomerNumber=GetMV("totCustNum",0); /* SetMV("totCustNum",0,localTotalCustomerNumber+1);*/ /* Fork(Customer); */ } } /* Release("InitLock"); /* ############################################################### */ /* Release("InitLock"); */ }
void appClerkInteract(int id, bool sen) { int custID; int k; /* for looping */ SetMV(appClerkState, id, 1); Acquire(appClerkMutex[id].clerkLock); /* get lock to ensure interaction order */ Release(lineLock); /* no need to hold this lock anymore */ Wait(appClerkMutex[id].clerkCV, appClerkMutex[id].clerkLock); /* wait for the customer to give me their social */ /* customer has now given me his ssn */ custID = GetMV(appClerkToFile, id); if(sen == true) { Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has received SSN ", 18); PrintInt(custID); Print(" from Senator ", 14); PrintInt(custID); Print(".\n", 2); Release(outputLock); } else { Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has received SSN ", 18); PrintInt(custID); Print(" from Customer ", 15); PrintInt(custID); Print(".\n", 2); Release(outputLock); } for(k = 0; k < 20; k++) { Yield(); /* make filing the ssn take some time */ } /* mark that the senator has filed his social */ if(sen == true) { SetMV(senatorSocial, custID, 1); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has recorded a complete application for Senator ", 49); PrintInt(custID); Print(".\n", 2); Release(outputLock); } else { SetMV(customerSocial, custID, 1); Acquire(outputLock); Print("Application Clerk ", 18); PrintInt(id); Print(" has recorded a complete application for Customer ", 50); PrintInt(custID); Print(".\n", 2); Release(outputLock); } /* alert the customer that they can leave */ Signal(appClerkMutex[id].clerkCV, appClerkMutex[id].clerkLock); /* wait for the customer to leave */ Wait(appClerkMutex[id].clerkCV, appClerkMutex[id].clerkLock); Release(appClerkMutex[id].clerkLock); SetMV(appClerkState, id, 0); }
void startScreeningOfficer() { #define mySOMACRO GetMV(screeningOfficers, _myIndex) int _myIndex; int _myMV; /* Claim my screening officer */ Acquire(GlobalDataLock); _myIndex = GetMV(NumActiveScreeningOfficers, 0); incrementMV(NumActiveScreeningOfficers, 0); _myMV = GetMV(screeningOfficers, _myIndex); Release(GlobalDataLock); /* Get to work! */ while (true) { int suspicionLevel = false; int shortestLineIndex = -1; /* impossible value */ int i; /* iterator */ Acquire(OfficersLineLock); if (queue_empty(officersLine)) { SetMV(mySOMACRO, SOState, ONBREAK); /*Printf1("SO %d is going to sleep\n", sizeof("SO %d is going to sleep\n"), _myIndex);*/ /* Done? */ if (GetMV(manager, ManAllSODone)) { Release(OfficersLineLock); break; } Wait(OfficersLineLock, GetMV(mySOMACRO, SOCommCV)); /* Wait for a passenger */ /* Done? */ if (GetMV(manager, ManAllSODone)) { Release(OfficersLineLock); break; } /*Printf1("SO %d is waking up\n", sizeof("SO %d is waking up\n"), _myIndex);*/ } if (!queue_empty(officersLine)) { Acquire(GetMV(mySOMACRO, SOLock)); SetMV(mySOMACRO, SOState, BUSY); SetMV(mySOMACRO, SOCurrentPassenger, queue_pop(officersLine)); /* SetMV(GetMV(passengers, GetMV(mySOMACRO, SOCurrentPassenger)), PassOfficerID, _myIndex);*/ SetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassOfficerID, _myMV); Signal(OfficersLineLock, OfficersLineCV); /* Wake up passenger */ Release(OfficersLineLock); Wait(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV)); /* Wait for passenger to approach */ /* Generate PASS/FAIL Results */ /* suspicionLevel = (17 * GetMV(mySOMACRO, SOCurrentPassenger)) % 10; *//* PSUEDO rand() */ suspicionLevel = 0; SetMV(SecurityFailResults, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex), suspicionLevel > 8); if (GetMV(SecurityFailResults, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex))) { /* FAIL */ Printf1("Screening officer %d is suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger))); } else { /* PASS */ Printf1("Screening officer %d is not suspicious of the hand luggage of passenger %d\n", sizeof("Screening officer %d is not suspicious of the hand luggage of passenger %d\n"), concat2Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger))); } /* Find an Available Security Inspector */ while (shortestLineIndex == -1) { /*Printf1("SO %d is trying to find SI\n", sizeof("SO %d is trying to find SI\n"), _myIndex);*/ #define inspector GetMV(securityInspectors, i) Acquire(InspectorLineLock); /*Printf1("SO %d acquired InspectorLineLock\n", sizeof("SO %d acquired InspectorLineLock\n"), _myIndex);*/ for (i = 0; i < NUM_SECURITY_INSPECTORS; ++i) { Acquire(GetMV(inspector, SILock)); /*Printf1("SO %d is checking SI %d\n", sizeof("SO %d is checking SI %d\n"), concat2Num(_myIndex, i));*/ if (GetMV(inspector, SIState) == AVAIL && GetMV(inspector, SINewPassenger) == -1) { /* Found an inspector! */ shortestLineIndex = GetMV(inspector, SIID); SetMV(inspector, SINewPassenger, GetMV(mySOMACRO, SOCurrentPassenger)); Release(GetMV(inspector, SILock)); break; } /*else { Printf1("GetMV(inspector, SIState) = %d\n", sizeof("GetMV(inspector, SIState) = %d\n"), GetMV(inspector, SIState)); Printf1("GetMV(inspector, SINewPassenger) = %d\n", sizeof("GetMV(inspector, SINewPassenger) = %d\n"), GetMV(inspector, SINewPassenger)); }*/ Release(GetMV(inspector, SILock)); } Release(InspectorLineLock); if (shortestLineIndex == -1) { /* Printf1("SO %d is about to Yield\n", sizeof("SO %d is about to Yield\n"), _myIndex);*/ Yield(); } #undef inspector /* Wait(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV));*/ } /* Found an inspetor for the passenger */ SetMV(GetMV(passengers, GetMV(GetMV(mySOMACRO, SOCurrentPassenger), PassIndex)), PassInspectorID, shortestLineIndex); Printf1("Screening officer %d directs passenger %d to security inspector %d\n", sizeof("Screening officer %d directs passenger %d to security inspector %d\n"), concat3Num(_myIndex, GetMV(mySOMACRO, SOCurrentPassenger), shortestLineIndex)); Signal(GetMV(mySOMACRO, SOLock), GetMV(mySOMACRO, SOCommCV)); Release(GetMV(mySOMACRO, SOLock)); } else { Release(OfficersLineLock); } } }
int main() { int myLineID; int firstTime; int ifBribed; int appClerkCV; int appClerkLock; int yieldCalls; int id; int i; int start; int currentCustomer; int cash; createServerMVs(1,1); initialize(&appClerk, 0, 1); Acquire(appClerk.newClerkIdLock); /* myLineID = appClerk.newClerkId; id = appClerk.newClerkId; appClerk.newClerkId++; */ myLineID = GetMV(appClerk.newClerkId, 0); id = myLineID; SetMV(appClerk.newClerkId, 0, id + 1); Release(appClerk.newClerkIdLock); firstTime = 1; start = 1; while(start == 1) { waitForLine(&appClerk, id, firstTime); currentCustomer = GetMV(appClerk.currentCustomer, id); Uprintf("ApplicationClerk #%d has received SSN %d from Customer #%d.\n", 60, id, currentCustomer,currentCustomer,0); /* appClerkCV = appClerk.clerkCV[myLineID]; appClerkLock = appClerk.clerkLock[myLineID]; */ appClerkCV = GetMV(appClerk.clerkCV, myLineID); appClerkLock = GetMV(appClerk.clerkLock, myLineID); /*input the socialSecurityNum into the completed applications*/ /*customersWithCompletedApps[appClerk.currentCustomer[id]] = 1;*/ SetMV(customersWithCompletedApps, currentCustomer, 1); yieldCalls = Rand(80, 21); for(i = 0; i < yieldCalls; i++) { /*delay in filing the application*/ Yield(); } Signal(appClerkCV, appClerkLock); Wait(appClerkCV, appClerkLock); /*currentCustomer = GetMV(appClerk.currentCustomer, id);*/ Uprintf("ApplicationClerk #%d has recorded a completed application for Customer #%d.\n", 76, id, currentCustomer,0,0); if(ifBribed == 1) { Uprintf("ApplicationClerk #%d has received $500 from Customer #%d.\n", 58, id, currentCustomer,0,0); /*appClerk.cashReceived+=500;*/ cash = GetMV(appClerk.cashReceived, 0) + 500; SetMV(appClerk.cashReceived, 0, cash); } firstTime = 0; Release(appClerkLock); } return 0; }
int waitForLine(struct Monitor *clerk, int myLineID, int firstTime) { int ifBribed; int senLineCount; int bribeLineCount; int lineCount; int senLineCV; int bribeLineCV; int lineCV; int start; int clerkLock; int clerkCV; int clerkType; int senWorking; Acquire(clerk->lineLock); ifBribed = 0; start = 1; clerkType = GetMV(clerk->clerkType, 0); while(start == 1) { bribeLineCount = GetMV(clerk->bribeLineCount, myLineID); lineCount = GetMV(clerk->lineCount, myLineID); senWorking = GetMV(senatorWorking, 0); if(senWorking != NULL){ senLineCount = GetMV(clerk->senLineCount, myLineID); if(senLineCount > 0) { /*Signal(clerk->senLineCV[myLineID], clerk->lineLock);*/ /*clerk->clerkState[myLineID] = 0;*/ senLineCV = GetMV(clerk->senLineCV, myLineID); Signal(senLineCV, clerk->lineLock); SetMV(clerk->clerkState, myLineID, 0); break; } else { /*clerk->clerkState[myLineID] = 1;*/ /*set state to break*/ SetMV(clerk->clerkState, myLineID, 1); Wait(clerk->breakCV, clerk->lineLock); continue; } } else if (bribeLineCount > 0) { /*if there is someone in the bribe line, signal them first*/ /* Signal(clerk->bribeLineCV[myLineID], clerk->lineLock); */ bribeLineCV = GetMV(clerk->bribeLineCV, myLineID); Signal(bribeLineCV, clerk->lineLock); ifBribed = 1; /* clerk->clerkState[myLineID] = 0; */ /*set state to busy*/ SetMV(clerk->clerkState, myLineID, 0); if(clerkType == 0) { Uprintf("ApplicationClerk #%d has signalled a Customer to come to their counter.\n", 72, myLineID, 0, 0, 0); } else if(clerkType == 1) { Uprintf("PictureClerk #%d has signalled a Customer to come to their counter.\n", 68, myLineID, 0, 0, 0); } else if(clerkType == 3) { Uprintf("Cashier #%d has signalled a Customer to come to their counter.\n", 63, myLineID, 0, 0, 0); } else if(clerkType == 2) { Uprintf("PassportClerk #%d has signalled a Customer to come to their counter.\n", 69, myLineID, 0, 0, 0); } break; } else if (lineCount > 0) { /*signal someone in normal line*/ /*Signal(clerk->lineCV[myLineID], clerk->lineLock);*/ lineCV = GetMV(clerk->lineCV, myLineID); Signal(lineCV, clerk->lineLock); /*clerk->clerkState[myLineID] = 0; */ /*set state to busy*/ SetMV(clerk->clerkState, myLineID, 0); if(clerkType == 0) { Uprintf("ApplicationClerk #%d has signalled a Customer to come to their counter.\n", 72, myLineID, 0, 0, 0); } else if(clerkType == 1) { Uprintf("PictureClerk #%d has signalled a Customer to come to their counter.\n", 68, myLineID, 0, 0, 0); } else if(clerkType == 3) { Uprintf("Cashier #%d has signalled a Customer to come to their counter.\n", 63, myLineID, 0, 0, 0); } else if(clerkType == 2) { Uprintf("PassportClerk #%d has signalled a Customer to come to their counter.\n", 69, myLineID, 0, 0, 0); } break; } else { /*no one is in either line, we must go to sleep*/ firstTime = 0; if (firstTime == 0) { if(clerkType == 0) { Uprintf("ApplicationClerk #%d is going on break.\n", 40, myLineID, 0, 0, 0); } else if(clerkType == 1) { Uprintf("PictureClerk #%d is going on break.\n", 36, myLineID, 0, 0, 0); } else if(clerkType == 3) { Uprintf("Cashier #%d is going on break.\n", 31, myLineID, 0, 0, 0); } else if(clerkType == 2) { Uprintf("PassportClerk #%d is going on break.\n", 37, myLineID, 0, 0, 0); } /*clerk->clerkState[myLineID] = 1;*/ /*set state to break*/ SetMV(clerk->clerkState, myLineID, 1); Wait(clerk->breakCV, clerk->lineLock); if(clerkType == 0) { Uprintf("ApplicationClerk #%d is coming off break.\n", 43, myLineID, 0, 0, 0); } else if(clerkType == 1) { Uprintf("PictureClerk #%d is coming off break.\n", 38, myLineID, 0, 0, 0); } else if(clerkType == 3) { Uprintf("Cashier #%d is coming off break.\n", 33, myLineID, 0, 0, 0); } else if(clerkType == 2) { Uprintf("PassportClerk #%d is coming off break.\n", 39, myLineID, 0, 0, 0); } } /*clerk->clerkState[myLineID] = 0;*/ /*set state to busy*/ SetMV(clerk->clerkState, myLineID, 0); continue; } } /*grab the clerkLock so we can properly signal the waiting customer to avoid a race condition and guarentee the correct order of events*/ clerkLock = GetMV(clerk->clerkLock, myLineID); /*Acquire(clerk->clerkLock[myLineID]);*/ Acquire(clerkLock); /*now we can let go of line lock since we properly acquired the clerk lock*/ Release(clerk->lineLock); clerkCV = GetMV(clerk->clerkCV, myLineID); /*Wait(clerk->clerkCV[myLineID], clerk->clerkLock[myLineID]);*/ Wait(clerkCV, clerkLock); return ifBribed; }
int main() { int lockId, lockStatus, cvId, cvStatus, mvId, mvStatus, mvValue, signalStatus; 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("\nAcquiring Lock\n",-1,-1,-1); lockStatus = Acquire(lockId); if(lockStatus == -1) { WriteToConsole("\nFailed to Acquire Lock. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nAcquired Lock\n\0", -1, -1, -1); } WriteToConsole("\nGetting MV value at index 1\n", -1, -1, -1); mvValue = GetMV(mvId, 1); if(mvValue == -1) { WriteToConsole("\nFailed to Get value from MV. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nGot value from MV at index 1: %d.\n\0", mvValue, -1, -1); } if(mvValue == 100) { signalStatus = Signal(cvId, lockId); if(signalStatus == -1) { WriteToConsole("\nFailed to Signal on the given Condition\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nSignalling on CV1 with Lock1\n\0", -1, -1, -1); } } WriteToConsole("\nSetting MV index 2 to 222\n", -1, -1, -1); mvStatus = SetMV(mvId, 2, 222); if(mvStatus == -1) { WriteToConsole("\nFailed to Set MV. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nMV set successfully.\n\0", -1, -1, -1); } WriteToConsole("\nReleasing Lock\n\0", -1, -1, -1); Release(lockId); if(lockStatus == -1) { WriteToConsole("\nFailed to Release Lock. Exiting Program\0n", -1, -1, -1); Exit(0); } else { WriteToConsole("\nReleased Lock\n\0", -1, -1, -1); } Exit(0); }