int main() { int i; initTheater(); while(theaterDone == 0) { if(theaterFull || (GetMV(totalCustomers,0)-(GetMV(totalCustomersServed,0)+GetMV(numSeatsOccupied,0))) <= (MAX_SEATS-GetMV(numSeatsOccupied,0)) || 1) { Acquire(movieStatusLock); movieStatus = 1; Release(movieStatusLock); Print("The MovieTechnician has started the movie.\n", -1, -1, -1); movieLength = 50; while(movieLength > 0) { Yield(); movieLength--; } Acquire(movieStatusLock); movieStatus = 2; Release(movieStatusLock); Print("The MovieTechnician has ended the movie.\n", -1, -1, -1); Acquire(movieFinishedLock); Broadcast(movieFinishedLockCV, movieFinishedLock); Release(movieFinishedLock); /* Free all seats for next movie */ for(i=0; i<NUM_ROWS; i++) SetMV(freeSeatsInRow, NUM_COLS, i); theaterFull = 0; movieStarted = 0; SetMV(numSeatsOccupied, 0, 0); SetMV(totalTicketsTaken, 0, 0); /* Tell Customers to go to theater */ if(GetMV(totalCustomers,0) != GetMV(totalCustomersServed,0)) { Acquire(customerLobbyLock); Broadcast(customerLobbyCV, customerLobbyLock); Release(customerLobbyLock); } Print("The MovieTechnician has told all customers to leave the theater room.\n", -1, -1, -1); } if(movieStatus == -1) { Yield(); } else { while(((GetMV(numSeatsOccupied,0) < MAX_SEATS) && (GetMV(numSeatsOccupied,0) < GetMV(totalCustomers,0))) || ((GetMV(numSeatsOccupied,0) == 0) && (GetMV(totalCustomersServed,0) == 0))) { /* Print("THERE ARE %d CUSTOMERS AND %d OF THE BITCHES ARE IN THEIR SEATS\n", GetMV(totalCustomers,0), GetMV(numSeatsOccupied,0), -1);*/ Yield(); } /* Acquire(movieStatusLock); Wait(movieStatusLockCV, movieStatusLock); /* Wait until manager says it's time to start the movie */ /* Release(movieStatusLock);*/ } if((GetMV(totalCustomers, 0) <= 0) && (GetMV(totalCustomersServed, 0) > 0)) { Print("\n Everyone has left, Manager is closing the theater. Good work boys!\n\n", -1, -1 ,-1); break; } } Exit(0); return 0; }
void Customer() { int CustID=-1; int myOT=-1; int i=0; int j=0; int NoOfEatIn=0; Acquire(CustCountLock); CustID = CustCount++; Release(CustCountLock); Acquire(custLineLock); Acquire(OTStatusUpdateLock); for(i=0;i<PersonCount.NoOfOT;i++) { if(FREE == OrderTakerStatus[i]) /*checking for a free order taker*/ { OrderTakerStatus[i] = BUSY;/*make ith (my) OT busy*/ myOT=i; break; } } i=0; Release(OTStatusUpdateLock); /*Here either I know who is my OT or there are no OT for me*/ if(-1==myOT) /*get in line*/ { custLineLength++; /*Print1("Customer[%d] has NO OrderTakers, Snoozing!\n", CustID);*/ Wait(custWaitingCV,custLineLock); /*Print1("Waiting Customer[%d] is signalled by OT\n", CustID);*/ } Release(custLineLock); /*Get a waiting OT*/ Acquire(OTStatusUpdateLock); for(j=0;j<PersonCount.NoOfOT;j++) { if (WAITING == OrderTakerStatus[j]) { myOT=j; OrderTakerStatus[j]=BUSY; break; } } j=0; Release(OTStatusUpdateLock); /*Allow OT to manipluate CustLineLength*/ /*By here, Customer is aware of his OT and ready to order his/her food*/ Acquire(SigCustListLock); SigCustList[CustID]=myOT; Release(SigCustListLock); Print2("Customer[%d] is giving order to OrderTaker[%d]\n",CustID,myOT); Acquire(CustDataLock); if(CustData[CustID].Food[0] == 0) Print1("Customer[%d] is NOT ordering 6-dollar burger\n", CustID); else Print1("Customer[%d] is ordering 6-dollar burger\n", CustID); if(CustData[CustID].Food[1] == 0) Print1("Customer[%d] is NOT ordering 3-dollar burger\n", CustID); else Print1("Customer[%d] is ordering 3-dollar burger\n", CustID); if(CustData[CustID].Food[2] == 0) Print1("Customer[%d] is NOT ordering veggie burger\n", CustID); else Print1("Customer[%d] is ordering veggie burger\n", CustID); if(CustData[CustID].Food[3] == 0) Print1("Customer[%d] is NOT ordering French fries\n", CustID); else Print1("Customer[%d] is ordering French fries\n", CustID); if(CustData[CustID].Food[4] == 0) Print1("Customer[%d] is NOT ordering Soda\n", CustID); else Print1("Customer[%d] is ordering Soda\n", CustID); Release(CustDataLock); NoOfEatIn = (2*(PersonCount.NoOfCust))/3; if(CustID <= NoOfEatIn ) { Print1("Customer [%d] chooses to Eat-in the food\n",CustID); } else { Print1("Customer [%d] chooses to To-go the food\n",CustID); } /*Wake-up the WAITING OT who is waiting for my order.*/ Acquire(OrderTakerLock[myOT]); Signal(OTWaitingCV[myOT],OrderTakerLock[myOT]); /*Print1("Customer[%d] is waiting for OT to respond for orders(2nd wait)\n", CustID);*/ Wait(OTWaitingCV[myOT],OrderTakerLock[myOT]);/* Waiting for my order to be processed*/ Print3("Customer [%d] is given token number [%d] by the OrderTaker [%d]\n",CustID,CustID,myOT); Release(OrderTakerLock[myOT]); /*Customer-Manager interaction If Customer is Eat-in type Wait for Manager to check if there is table for me Need lock and CV to go on wait*/ Acquire(CustDataLock); if (CustData[CustID].TypeOfFood == 0) { Release(CustDataLock); Acquire(TblNeededCustLock); TblNeededCustList[myOT][CustID]=1; /*1 indicates that customer needs table*/ Release(TblNeededCustLock); Acquire(NeedTableLock[CustID]); Print1("Customer %d is waiting for the table\n", CustID); Wait(NeedTableCV[CustID],NeedTableLock[CustID]); Signal(NeedTableCV[CustID],NeedTableLock[CustID]);/*Send ack to Mgr*/ Print1("Eat IN Customer %d got the table\n", CustID); /* Manager signals only when there are free tables*/ /* Here Customers are sure of allocated Table*/ Release(NeedTableLock[CustID]); /*Customer Waits for Waiter Eat-in Customer whose food is ready, seated and waiting for waiters to serve them*/ Acquire(NeedWaitersLock[CustID]); Print1("Customer [%d] is waiting for the waiter to serve the food\n",CustID); Wait(NeedWaitersCV[CustID],NeedWaitersLock[CustID]); /*Waiters signals only when Customer Food is ready Here Customers are sure of served food and they must leave!*/ Print1("Customer [%d] is served by the waiter",CustID); Release(NeedWaitersLock[CustID]); Print1("\nCustomer[%d] order is leaving the restaurant after having food\n\n\n",CustID ); } else { Release(CustDataLock); Print2("\nCustomer[%d] is leaving the restaurant after Order Taker[%d] packed the food\n\n\n",CustID,myOT ); } CustLeft++; Acquire(AliveCustCountLock); AliveCustCount--; Release(AliveCustCountLock); /*Print1("Customer %d is exiting\n",CustID);*/ Exit(1); }
int IsCustomerFoodReady (int CID) { int CustFoodIsReady = 0; int CustID = CID; Acquire(CustDataLock); Acquire(CookedFoodLock); Acquire(FoodToBeCookedUpdateLock); switch(CustID%5) { case 0: if ( CustData[CustID].Food[0] < CookedFood[0] && \ CustData[CustID].Food[1] < CookedFood[1] &&\ CustData[CustID].Food[2] < CookedFood[2] && \ CustData[CustID].Food[3] < CookedFood[3] && \ CustData[CustID].Food[4] < CookedFood[4]) { CustFoodIsReady = 1; CookedFood[0]--; CookedFood[1]--; CookedFood[2]--; CookedFood[3]--; CookedFood[4]--; } else { FoodToBeCooked[0]++; FoodToBeCooked[1]++; FoodToBeCooked[2]++; FoodToBeCooked[3]++; } break; case 1: if ( CustData[CustID].Food[0] < CookedFood[0]&&\ CustData[CustID].Food[2] < CookedFood[2]&&\ CustData[CustID].Food[4] < CookedFood[4]) { CustFoodIsReady = 1; CookedFood[0]--; CookedFood[2]--; CookedFood[4]--; } else { FoodToBeCooked[0]++; FoodToBeCooked[2]++; } break; case 2: if( CustData[CustID].Food[1]<CookedFood[1]&&\ CustData[CustID].Food[4] < CookedFood[4]) { CustFoodIsReady = 1; CookedFood[1]--; CookedFood[4]--; } else { FoodToBeCooked[1]++; } break; case 3: if( CustData[CustID].Food[0] <CookedFood[0]&&\ CustData[CustID].Food[2] < CookedFood[2]&&\ CustData[CustID].Food[4] < CookedFood[4]) { CustFoodIsReady = 1; CookedFood[0]--; CookedFood[2]--; CookedFood[4]--; } else { FoodToBeCooked[0]++; FoodToBeCooked[2]++; } break; default: CustFoodIsReady = -1; break; }/*end of switch*/ Release(CookedFoodLock); Release(FoodToBeCookedUpdateLock); Release(CustDataLock); return(CustFoodIsReady); }
void acquirelock_t2() { Acquire(l); Print("Acquired lock %i\n", 18, l * 1000, 0); Exit(0); }
void Manager() { int MyMgrID=1; int MyCustID; int MyCookSpec; int MyCookStat; int RmvedCookCnt=0; int RmvedCook[200]; int a=0; int b=0; int c=0; int d=0; int i=0; int myOTId=PersonCount.NoOfOT; int CustFoodIsReady=0; while (1) { /*Manager should not take customers untill OT finished interaction with them*/ Acquire(TblNeededCustLock); for(a=0;a<MAX_SIZE;a++) /*a-OTID b-CUSTID*/ { for(b=0;b<MAX_SIZE;b++) { /*Print2("a=[%d]b=[%d] in for loop\n",a,b); Print1("TblNeededCustList[a][b]=%d outside for loop\n",TblNeededCustList[a][b]);*/ if (TblNeededCustList[a][b]==1) { /*There are atleast some customer either waiting for food to be prepared or with food prepared and waiting for table*/ Acquire(TotalTablesUpdateLock); MyCustID=b; /* There is a table for Customers Alert the customer about this*/ if ( TotalTables>0 ) { TotalTables--; Release(TotalTablesUpdateLock); /*Update this table so that waiter is aware of seated customers*/ Acquire(TableAllotedCustLock); for(c=0;c<MAX_SIZE;c++) { if(TableAllotedCustList[c]==-1) { TableAllotedCustList[c]=MyCustID; TblNeededCustList[a][b]=0; /* There is a table for Customers Alert the customer about this*/ Acquire(NeedTableLock[MyCustID]); Print2("Customer [%d] receives token number [%d] from the Manager\n",MyCustID,MyCustID); Print1("Customer[%d] is informed by the Manager - the restaurant is NOT full\n",MyCustID); Print1("Customer [%d] is seated at table\n",MyCustID); Signal(NeedTableCV[MyCustID],NeedTableLock[MyCustID]); Wait(NeedTableCV[MyCustID],NeedTableLock[MyCustID]); Release(NeedTableLock[MyCustID]); break; } } Release(TableAllotedCustLock); } /* if there are no tables, just give them the token number*/ else { /*pritnf("Customer [%d] receives token number [%d] from the Manager\n",MyCustID,MyCustID);*/ Release(TotalTablesUpdateLock); Print1("Customer[%d] is informed by the Manager-the restaurant is full\n",MyCustID); Print1("Customer [%d] is waiting to sit on the table\n", MyCustID); } } } } a=b=c=0; Release(TblNeededCustLock); /************************************************************* * Manager as an OT START **************************************************************/ i=0; for(i=0;i<200;i++) { Acquire(custLineLock); if(custLineLength>3*PersonCount.NoOfOT) { } Release(custLineLock); } /************************************************************* * Manager as an OT END **************************************************************/ /* Manager as food packer*/ Acquire(EatInCustWaitingForFoodLock); i=0; for(i=0;i<MAX_SIZE;i++) { if(EatInCustWaitingForFood[i]==1) { EatInCustWaitingForFood[i]=0; MyCustID=i; break; } } i=0; /*For (each) this customer, check if his order is ready for each type of food*/ if(MyCustID!=-1) { CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1;/* 1 means food is grabed and 0 means otherwise*/ Release(GrabedFoodLock); Print1("Manager packed food for Customer[%d]\n", MyCustID); MyCustID=-1; Release(EatInCustWaitingForFoodLock); } else { /*list of customers to whom token should be handled but shouldnt grab the food*/ /*this Q is as good as saying who gave token number to whom when they wanna wait*/ EatInCustWaitingForFood[MyCustID]=1; Print1("Manager gives token number to Customer[%d]\n", MyCustID); MyCustID=-1; Release(EatInCustWaitingForFoodLock); } } else { Release(EatInCustWaitingForFoodLock); Acquire(ToGoCustWaitingForFoodLock); i=0; for(i=0;i<MAX_SIZE;i++) { if(ToGoCustWaitingForFood[i]==1) { ToGoCustWaitingForFood[i]=0; MyCustID=i; break; } } i=0; if(MyCustID!=-1) { CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1;/* 1 means food is grabed and 0 means otherwise*/ Release(GrabedFoodLock); Print1("Manager packed the food for Customer[%d]\n", MyCustID); MyCustID=-1; Release(ToGoCustWaitingForFoodLock); } else { /*list of customers to whom token should be handled but shouldnt grab the food /*this Q is as good as saying who gave token number to whom when they wanna wait*/ ToGoCustWaitingForFood[MyCustID]=1; Print1("Manager gives token number to Customer[%d]\n",MyCustID); MyCustID=-1; Release(ToGoCustWaitingForFoodLock); } } else { Release(ToGoCustWaitingForFoodLock); /*Nothing to do /*Set OT status to FREE*/ } } /*End of Manager as food packer*/ /* Manager - Waiter Interaction */ Acquire(TableAllotedCustLock); for(d=0;d<MAX_SIZE;d++) { if(-1!=TableAllotedCustList[d]) /*List is not empty*/ { Acquire(WaitersQLock); Broadcast(WaitersCV,WaitersQLock); Print("Manager calls back all Waiters from break\n"); /*Print(" Manager broadcasted\n");*/ Release(WaitersQLock); break; } } d=0; Release(TableAllotedCustLock); i=0; for (i=0;i<200;i++) { /*Print1("Manager yeilding%d\n",i);*/ Yield(); } i=0; /* Manager - Waiter Interaction End*/ Acquire(AliveCustCountLock); if(AliveCustCount==0) { Release(AliveCustCountLock); Acquire(SleepingWaiterCountLock); /*Print1("Above while SleepingWaiterCount%d\n",SleepingWaiterCount);*/ if (SleepingWaiterCount!=0) { while(SleepingWaiterCount!=0) { /*Print("inside while");*/ Acquire(WaitersQLock); Broadcast(WaitersCV,WaitersQLock); Release(WaitersQLock); Release(SleepingWaiterCountLock); /*Print("Manager Broadcast\n");*/ for (i=0;i<200;i++) { /*Print1("Manager yeilding%d\n",i);*/ Yield(); } Acquire(SleepingWaiterCountLock); } } Release(SleepingWaiterCountLock); } else Release(AliveCustCountLock); Acquire(AliveCustCountLock); if(AliveCustCount==0) { Release(AliveCustCountLock); Acquire(ActiveOTCountLock); if(ActiveOTCount!=0) { /*Print1("Mgr waiting for OT, AcOT=%d\n",ActiveOTCount);*/ while(ActiveOTCount!=0) { Release(ActiveOTCountLock); for (i=0;i<200;i++) { /*Print1("Manager yeilding%d\n",i);*/ Yield(); } Acquire(ActiveOTCountLock); } } Release(ActiveOTCountLock); if(DestroyAllLockCV==1) { /*Print1("SleepingWaiterCount%d\n",SleepingWaiterCount);*/ Print("End of Simulation!!\n"); Exit(1); } else { for (i=0;i<200;i++) { /*Print1("Manager yeilding%d\n",i);*/ Yield(); } } } else { Release(AliveCustCountLock); } } }
static void doCashierClerk(int* index, int* cashDollars) { int myClerk, x, rando; while(TRUE) { myClerk = -1; tprintf("Senator [%d]: Going to the CashClerk\n",*index,0,0,"",""); /*cashLineLock->Acquire(); */ Acquire(cashLineLock); regCashLineLength++; tprintf("Senator [%d]: Waiting in the Regular Line for next available CashClerk\n",*index,0,0,"",""); /*regCashLineCV->Wait(cashLineLock); */ Wait(regCashLineCV, cashLineLock); tprintf("Senator[%d]: Finding available CashClerk...\n",*index,0,0,"",""); for(x = 0; x < MAX_CASH_CLERKS; x++) { if(cashClerkStatuses[x] == CLERK_AVAILABLE) { myClerk = x; tprintf("Senator [%d]: Going to chill with CashClerk[%d]\n",*index,myClerk,0,"",""); cashClerkStatuses[myClerk] = CLERK_BUSY; break; } else tprintf("Senator [%d]: CashClerk[%d] is unavailable\n",*index,x,0,"",""); } if (myClerk == -1) { printf("Senator [%d]: Woke up with a CashClerk index of -1, halting the machine for now.",*index,0,0,"",""); Halt(); } /*cashClerkLocks[myClerk]->Acquire(); cashLineLock->Release(); */ Acquire(cashClerkLocks[myClerk]); Release(cashLineLock); cashClerkSSNs[myClerk] = *index; printf("Senator [%d] goes to CashClerk[%d]\n",*index,myClerk,0,"",""); /*interact with clerk */ /*cashClerkCVs[myClerk]->Signal(cashClerkLocks[myClerk]); cashClerkCVs[myClerk]->Wait(cashClerkLocks[myClerk]);*/ Signal(cashClerkCVs[myClerk], cashClerkLocks[myClerk]); Wait(cashClerkCVs[myClerk], cashClerkLocks[myClerk]); /*pay for passport. If it's not processed, get back in line*/ if(!cashPunish[myClerk]) { printf("Senator [%d] gets [valid] certification by Cashier[%d]\n",*index,myClerk,0,"",""); printf("Senator [%d] pays $100 to Cashier[%d] for their passport\n",*index,myClerk,0,"",""); cashClerkMoney[myClerk] += 100; *cashDollars-=100; /*cashClerkCVs[myClerk]->Signal(cashClerkLocks[myClerk]); cashClerkCVs[myClerk]->Wait(cashClerkLocks[myClerk]);*/ Signal(cashClerkCVs[myClerk], cashClerkLocks[myClerk]); Wait(cashClerkCVs[myClerk], cashClerkLocks[myClerk]); tprintf("Senator [%d]: Passport paid for like a pro. CashDollars = [$%d]\n", *index, *cashDollars,0,"",""); /*cashClerkLocks[myClerk]->Release();*/ Release(cashClerkLocks[myClerk]); printf("Senator [%d] leaves the passport office...\n",*index,0,0,"",""); /*customerOfficeLock->Acquire();*/ Acquire(entryLock); senatorsInOffice--; /*customerOfficeLock->Release();*/ Release(entryLock); break; } /*cashClerkLocks[myClerk]->Release();*/ Release(cashClerkLocks[myClerk]); printf("Senator [%d] gets [invalid] certification by Cashier[%d]\n",*index,myClerk,0,"",""); printf("Senator [%d] is punished to wait by Cashier[%d]\n",*index,myClerk,0,"",""); tprintf("Senator [%d]: NOT READY!? Going back to the end of the line...\n",*index,0,0,"",""); rando = Rand()%80+20; for(x = 0; x < rando; x++) Yield(); } }
main() { int myLine; int lineCondition; int bribeLineCondition; int clerkCondition; int breakLock; int breakCondition; int clerkLock; int customerID; setup(); Acquire(counterLock); myLine = Get(numCashiers,0); Set(numCashiers,0,myLine+1); Release(counterLock); lineCondition = Get(cashLineCV,myLine); bribeLineCondition = Get(cashBribeLineCV,myLine); clerkCondition = Get(cashClerkCV,myLine); breakLock = Get(cashBreakLock,myLine); breakCondition = Get(cashBreakCV,myLine); clerkLock = Get(cashClerkLock,myLine); /* On duty while there are still customers who haven't completed process */ while(Get(customersFinished,0) < NUM_CUSTOMERS + NUM_SENATORS) { Acquire(cashierLock); if (Get(cashLineCount,myLine) > 0) { Print("Cashier %i has signalled a customer to come to their counter\n", 62, myLine * 1000, 0); Signal(lineCondition, cashierLock); Set(cashState,myLine,BUSY); } else { if(Get(storeJustOpened,0)>=NUM_CLERKS*4) { Print("Cashier %i is going on break\n", 30, myLine * 1000, 0); Release(cashierLock); Acquire(breakLock); Set(cashState,myLine,ONBREAK); /* Mesa style monitor, wait to be woken up by manager */ if(Get(cashState,myLine) == ONBREAK) { Wait(breakCondition, breakLock); } Print("Cashier %i is coming off break\n", 32, myLine * 1000, 0); Release(breakLock); Acquire(cashierLock); if (Get(cashLineCount,myLine) > 0) { Print("Cashier %i has signalled a customer to come to their counter\n", 62, myLine * 1000, 0); Signal(lineCondition, cashierLock); Set(cashState,myLine,BUSY); } else Set(cashState,myLine,AVAILABLE); } else { Set(cashState,myLine,AVAILABLE); Set(storeJustOpened,0,Get(storeJustOpened,0)+1); } } /* entering transaction so switch locks */ Acquire(clerkLock); Release(cashierLock); /* wait for customer to pay */ Wait(clerkCondition, clerkLock); customerID = Get(cashCustomer,myLine); Print("Cashier %i has received SSN from Customer %i\n", 46, myLine * 1000 + customerID, 0); Print("Cashier %i has verified that Customer %i has been certified by a PassportClerk\n", 80, myLine * 1000 + customerID, 0); /* taking payment */ Set(custMoney,customerID, Get(custMoney,customerID)-100); Set(cashMoney,myLine,Get(cashMoney,myLine)+100); Print("Cashier %i has received the $100 from Customer %i after certification\n", 71, myLine * 1000 + customerID, 0); Set(passportGiven,customerID, TRUE); Signal(clerkCondition, clerkLock); Print("Cashier %i has provided Customer %i their completed passport\n", 62, myLine * 1000 + customerID, 0); Print("Cashier %i has recorded that Customer %i has been given their completed passport\n", 82, myLine * 1000 + customerID, 0); Release(clerkLock); } Exit(0); }
int chooseCustomerFromLine(int myLine, char* clerkName, int clerkNameLength) { int testFlag = false; int clerkBribeLineCnt, clerkLineCnt, senatorLineCnt, senatorDoneMon, clerkState, allCustDone; do { testFlag = false; /* TODO: -1 used to be NULL. Hung needs to figure this out */ senatorLineCnt = GetMonitor(senatorLineCount, 0); senatorDoneMon = GetMonitor(senatorDone, 0); if((senatorLineCnt > 0 ) || (senatorLineCnt > 0 && senatorDoneMon == 1)) { /* CL: chooses senator line first */ PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is required by the senator\n", 29);/*HUNG LINE*/ Acquire(clerkSenatorCVLock[myLine]); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" clerkSenatorCVLock acquired\n", 30);/*HUNG LINE*/ Signal(clerkSenatorCVLock[myLine], clerkSenatorCV[myLine]); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" clerkSenatorCVLock signal\n", 29);/*HUNG LINE*/ /*Wait for senator here, if they need me*/ Wait( clerkSenatorCVLock[myLine], clerkSenatorCV[myLine]); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" clerkSenatorCVLock waited e\n", 30);/*HUNG LINE*/ if(senatorLineCnt == 0){ Acquire(clerkLineLock); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" was unused by senator\n", 23);/*HUNG LINE*/ SetMonitor(clerkStates, myLine, AVAILABLE); Release(clerkSenatorCVLock[myLine]); }else if(senatorLineCnt > 0 && senatorDoneMon == 1){ SetMonitor(clerkStates, myLine, AVAILABLE); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is being used by senator\n", 26);/*HUNG LINE*/ Release(clerkSenatorCVLock[myLine]); }else{ SetMonitor(clerkStates, myLine, BUSY); testFlag = true; } PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" clerkSenatorCVLock waited f\n", 30);/*HUNG LINE*/ }else{ PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is not required by the senator\n", 33);/*HUNG LINE*/ Acquire(clerkLineLock); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" acquired clerkLinelock\n", 24);/*HUNG LINE*/ clerkBribeLineCnt = GetMonitor(clerkBribeLineCount, myLine); clerkLineCnt = GetMonitor(clerkLineCount, myLine); if(clerkBribeLineCnt > 0) { PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is servicing a customer from bribe line\n", 41); Signal(clerkLineLock, clerkBribeLineCV[myLine]); SetMonitor(clerkStates, myLine, BUSY); /*redundant setting*/ } else if(clerkLineCnt > 0) { PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is servicing a customer from regular line\n", 43); Signal(clerkLineLock, clerkLineCV[myLine]); SetMonitor(clerkStates, myLine, BUSY); /*redundant setting*/ }else{ Acquire(breakLock[myLine]); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is going on break\n", 19); SetMonitor(clerkStates, myLine, ONBREAK); Release(clerkLineLock); Wait(breakLock[myLine], breakCV[myLine]); /*PrintNum(breakLock[myLine]); PrintNl();*/ PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" is waking up from a break now.\n", 33); PrintNl();/*HUNG LINE*/ SetMonitor(clerkStates, myLine, AVAILABLE); Release(breakLock[myLine]); allCustDone = GetMonitor(allCustomersAreDone, 0); if(allCustDone == 1) { Exit(0); } } } clerkState = GetMonitor(clerkStates, myLine); } while(clerkState!= BUSY); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" Acquring clerkLock\n", 21);/*HUNG LINE*/ Acquire(clerkLock[myLine]); PrintString(clerkName, clerkNameLength); PrintNum(myLine); PrintString(" Clerklock acquired\n", 21);/*HUNG LINE*/ Release(clerkLineLock); /*wait for customer*/ if(testFlag){ Signal(clerkSenatorCVLock[myLine], clerkSenatorCV[myLine]); Release(clerkSenatorCVLock[myLine]); } Wait(clerkLock[myLine], clerkCV[myLine]); /*Do my job -> customer waiting*/ clerkState = GetMonitor(customerData, myLine); return clerkState; }
/* Setup for previous test */ void destroy_Lock_Using_Lock_Setup(){ Write("Acquiring unused lock_10 to check destroy_Lock\n", sizeof("Acquiring unused lock_10 to check destroy_Lock\n"), ConsoleOutput); Acquire(lock_10); }
static CAT_INLINE u8 *Resize(u8 *ptr, u32 new_trailing_bytes) { if (!ptr) return Acquire(new_trailing_bytes); return Resize(Promote(ptr), new_trailing_bytes); }
int main() { int lockStatus, cvId, 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); } 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 by the server.", -1, -1, -1); } WriteToConsole("\nAcquiring Lock.\n\0", -1, -1, -1); lockStatus = Acquire("lock1",5); if(lockStatus == -1) { WriteToConsole("\nError Occurred while Acquiring Lock. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nLock acquired.\n\0", -1, -1, -1); } WriteToConsole("\nCalling Wait on lock1 and cv1.\n\0",-1, -1,-1); waitStatus = Wait("cv1",3, "lock1",5); if(waitStatus == -1) { WriteToConsole("\nError, Not the Lock owner. Exiting Program.\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nServer waiting on the given condition. Now start client 2\n\0", -1, -1, -1); } WriteToConsole("\nReleasing Lock.\n\0", -1, -1, -1); lockStatus = Release("lock1",5); if(lockStatus == -1) { WriteToConsole("\nError Occurred while Releasing Lock. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nLock released.\n\0", -1, -1, -1); } Exit(0); }
int main(){ Acquire(0); Acquire(1); }
int main() { int lockStatus, cvStatus, mvStatus, mvId, mvValue; WriteToConsole("\nCreating a Lock\n\0", -1, -1, -1); lockStatus = CreateLock("lock1",5); if(lockStatus == -1) { WriteToConsole("\nError Occurred while creating Lock. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nLock Created by the server.\n\0", -1, -1, -1); } WriteToConsole("\nCreating a MV\n\0", -1, -1, -1); mvStatus = CreateMV("mv1",3, 5); if(mvStatus == -1) { WriteToConsole("\nError Occurred while creating MV. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nMV Created by the server.\n\0", -1, -1, -1); } WriteToConsole("\nAcquiring lock to access Monitor Variable\n\0", -1, -1, -1); lockStatus = Acquire("lock1",5); if(lockStatus == -1) { WriteToConsole("\nError Occurred while acquiring Lock. Exiting program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nLock acquired by the server.\n\0", -1, -1, -1); } WriteToConsole("\nSetting MV to 100\n", -1, -1, -1); mvStatus = SetMV("mv1",3, 1, 100); if(mvStatus == -1) { WriteToConsole("\nFailed to Set MV. Exiting Program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nMV set successfully.\n\0", -1, -1, -1); } WriteToConsole("\nTrying to fetch value at invalid mv Index : %d\n\0", 21, -1,-1); mvValue = GetMV("mv1",3, 20); if(mvValue == -1) { WriteToConsole("\nInvalid mvIndex, Failed to fetch value at given index. Exiting Program\n\0", -1, -1, -1); Exit(0); } else { WriteToConsole("\nAbnormal Execution\n\0", -1, -1, -1); } Exit(0); }
int main(int argc, char* argv[]){ // test 1 : pipe, lock and cvar int lock_id, cvar_id, pipe_id; int pid,status; int condition=0; char *test = "Yalnix Works"; char *buffer = (char*)malloc(1024); TracePrintf(1, "init main: test pipe, lock, cvar.\n"); LockInit(&lock_id); CvarInit(&cvar_id); PipeInit(&pipe_id); TracePrintf(1, "init main: Lockid %d.\n", lock_id); TracePrintf(1, "init main: Cvarid %d.\n", cvar_id); TracePrintf(1, "init main: Pipeid %d.\n", pipe_id); pid = Fork(); if (pid == 0) { TracePrintf(1,"init main: child \n"); Acquire(lock_id); TracePrintf(1,"init main: child acquire the lock\n"); condition=1; TracePrintf(1,"init main: child condition %d\n",condition); PipeWrite(pipe_id, &condition,sizeof(int)); TracePrintf(1,"init main: child change the condition and write it to pipe\n"); TracePrintf(1,"init main: child cvar signal\n"); CvarSignal(cvar_id); Release(lock_id); TracePrintf(1,"init main: child write to pipe: %s\n",test); PipeWrite(pipe_id,test,20); TracePrintf(1,"init main: child release the lock\n"); Exit(0); } else{ TracePrintf(1,"init main: parent\n"); Acquire(lock_id); TracePrintf(1,"init main: parent acquire the lock\n"); while(!condition){ TracePrintf(1,"init main: parent cvar wait, condition %d\n",condition); CvarWait(cvar_id,lock_id); PipeRead(pipe_id,&condition,sizeof(int)); TracePrintf(1,"init main: parent read the condition from pipe, condition %d\n",condition); } TracePrintf(1,"init main: parent wake up\n"); Release(lock_id); TracePrintf(1,"init main: parent release the lock\n"); PipeRead(pipe_id,buffer,20); TracePrintf(1,"init main: parent read from pipe: %s\n",buffer); } Reclaim(lock_id); Reclaim(cvar_id); Reclaim(pipe_id); Exit(0); }
static void doPicClerk(int* index, int* cashDollars) { bool privLine = FALSE; int myClerk, x; while(TRUE) { myClerk = -1; tprintf("Senator [%d]: Going to the PicClerk\n",*index,0,0,"",""); /*appPicLineLock->Acquire(); */ Acquire(appPicLineLock); tprintf("Senator [%d]: What line should I choose for the PicClerk?\n",*index,0,0,"",""); /*check for senator*/ if(*cashDollars > 100) /*get in a privledged line*/ { privLine = TRUE; tprintf("Senator [%d]: Priveleged line, baby. CashDollars = $%d\n",*index,*cashDollars,0,"",""); privPicLineLength++; tprintf("Senator [%d]: Waiting in the Priveledged Line for next available PicClerk\n",*index,0,0,"",""); /*privPicLineCV->Wait(appPicLineLock); */ Wait(privPicLineCV, appPicLineLock); } else /*get in a normal line*/ { tprintf("Senator [%d]: Regular line, suckas. CashDollars = $%d\n",*index,*cashDollars,0,"",""); regPicLineLength++; tprintf("Senator [%d]: Waiting in the Regluar Line for next available PicClerk\n",*index,0,0,"",""); /*regPicLineCV->Wait(appPicLineLock); */ Wait(regPicLineCV, appPicLineLock); } tprintf("Senator [%d]: Finding available PicClerk...\n",*index,0,0,"",""); for(x = 0; x < MAX_PIC_CLERKS; x++) { if(picClerkStatuses[x] == CLERK_AVAILABLE) { myClerk = x; tprintf("Senator [%d]: Going to chill with PicClerk[%d]\n",*index,myClerk,0,"",""); picClerkStatuses[myClerk] = CLERK_BUSY; break; } else tprintf("Senator [%d]: PicClerk [%d] is unavailable\n",*index,x,0,"",""); } if (myClerk == -1) { printf("Senator [%d]: Woke up with a PicClerk index of -1, halting the machine for now.",*index,0,0,"",""); Halt(); } /*picClerkLocks[myClerk]->Acquire(); appPicLineLock->Release();*/ Acquire(picClerkLocks[myClerk]); Release(appPicLineLock); picClerkSSNs[myClerk] = *index; if(privLine) { printf("Senator [%d] is willing to pay $500 to PictureClerk[%d] for moving ahead in line\n",*index, myClerk,0,"",""); picClerkMoney[myClerk] += 500; picClerkBribed[myClerk] = TRUE; *cashDollars -= 500; } tprintf("Senator [%d] Goes to PictureClerk[%d]\n",*index,myClerk,0,"",""); /*interact with clerk, loop enabled picture to be taken multiple times*/ while(happyWithPhoto[myClerk] == FALSE) { tprintf("Senator [%d]: Getting my picture taken...\n",*index,0,0,"",""); /*picClerkCVs[myClerk]->Signal(picClerkLocks[myClerk]); picClerkCVs[myClerk]->Wait(picClerkLocks[myClerk]); */ Signal(picClerkCVs[myClerk], picClerkLocks[myClerk]); Wait(picClerkCVs[myClerk], picClerkLocks[myClerk]); /*did I like my picture?*/ if(Rand()%10 > 5) { happyWithPhoto[myClerk] = TRUE; printf("Senator [%d] [likes] the picture provided by PictureClerk[%d]\n", *index,myClerk,0,"",""); } else { printf("Senator [%d] [dislikes] the picture provided by PictureClerk[%d]\n", *index,myClerk,0,"",""); printf("The picture of Senator [%d] will be taken again.\n", *index,0,0,"",""); } /*picClerkCVs[myClerk]->Signal(picClerkLocks[myClerk]); picClerkCVs[myClerk]->Wait(picClerkLocks[myClerk]); */ Signal(picClerkCVs[myClerk], picClerkLocks[myClerk]); Wait(picClerkCVs[myClerk], picClerkLocks[myClerk]); } happyWithPhoto[myClerk] = FALSE; /*needs to be reset for future customers*/ tprintf("Senator [%d]: Picture taken. Like a boss.\n",*index,0,0,"",""); /*picClerkLocks[myClerk]->Release(); */ Release(picClerkLocks[myClerk]); tprintf("Senator [%d]: Done and done.\n",*index,0,0,"",""); break; } }
/* Setup for next test */ void destroy_CV_Setup(){ Write("Acquiring lock_9 and waiting on cv_9\n", sizeof("Acquiring lock_9 and waiting on cv_9\n"), ConsoleOutput); Acquire(lock_9); Wait(cv_9, lock_9); Exit(0); }
static void doPassPortClerk(int *index, int* cashDollars){ bool privLined = FALSE; bool bribed = FALSE; int myClerk, x, rando; while(TRUE) { myClerk = -1; tprintf("Senator [%d]: Going to the PassportClerk\n",*index,0,0,"",""); tprintf("Senator [%d]: Acquiring PassLineLock...\n",*index,0,0,"",""); /*passLineLock->Acquire();*/ Acquire(passLineLock); printf("Senator [%d]: What line should I choose for the PassportClerk?\n",*index,0,0,"",""); /*check for senator*/ if(*cashDollars > 100 || privLined) /*get in a privledged line*/ { privLined = TRUE; tprintf("Senator [%d]: Priveleged line, baby. CashDollars = $%d\n",*index,*cashDollars,0,"",""); privPassLineLength++; tprintf("Senator [%d]: Waiting in the Priveledged Line for next available PassportClerk\n",*index,0,0,"",""); /*privPassLineCV->Wait(passLineLock);*/ Wait(privPassLineCV, passLineLock); } else /*get in a normal line*/ { tprintf("Senator [%d]: Regular line, suckas. CashDollars = $%d\n",*index,*cashDollars,0,"",""); regPassLineLength++; tprintf("Senator [%d]: Waiting in the Regular Line for next available PassportClerk\n",*index,0,0,"",""); /*regPassLineCV->Wait(passLineLock); */ Wait(regPassLineCV, passLineLock); } tprintf("Senator [%d]: Finding available PassClerk...\n",*index,0,0,"",""); for(x = 0; x < MAX_PASS_CLERKS; x++) { if(passClerkStatuses[x] == CLERK_AVAILABLE) { myClerk = x; tprintf("Senator [%d]: Going to chill with PassClerk[%d]\n",*index,myClerk,0,"",""); passClerkStatuses[myClerk] = CLERK_BUSY; break; } else tprintf("Senator [%d]: PassClerk[%d] is unavailable\n",*index,x,0,"",""); } if (myClerk == -1) { printf("Senator [%d]: Woke up with a PassClerk index of -1, halting the machine for now.",*index,0,0,"",""); Halt(); } /*passClerkLocks[myClerk]->Acquire();*/ Acquire(passClerkLocks[myClerk]); /*passLineLock->Release(); */ Release(passLineLock); passClerkSSNs[myClerk] = *index; if(privLined && !bribed) { bribed = TRUE; printf("Senator [%d] is willing to pay $500 to PassportClerk[%d] for moving ahead in line\n",*index,myClerk,0,"",""); passClerkMoney[myClerk] += 500; passClerkBribed[myClerk] = TRUE; *cashDollars -= 500; } printf("Senator [%d] goes to PassportClerk[%d]\n",*index,myClerk,0,"",""); /*interact with clerk */ /*passClerkCVs[myClerk]->Signal(passClerkLocks[myClerk]); passClerkCVs[myClerk]->Wait(passClerkLocks[myClerk]);*/ Signal(passClerkCVs[myClerk], passClerkLocks[myClerk]); Wait(passClerkCVs[myClerk], passClerkLocks[myClerk]); /*get passport from clerk, if not ready, go to the back of the line?*/ if(!passPunish[myClerk]) { printf("Senator [%d] is [certified] by PassportClerk[%d]\n",*index,myClerk,0,"",""); tprintf("Senator [%d]: Passport. GET!.\n", *index,0,0,"",""); tprintf("Senator [%d]: Done and done.\n",*index,0,0,"",""); /*passClerkLocks[myClerk]->Release();*/ Release(passClerkLocks[myClerk]); tprintf("Senator [%d]: Going to next clerk...\n",*index,0,0,"",""); break; } /*passClerkLocks[myClerk]->Release();*/ Release(passClerkLocks[myClerk]); printf("Senator [%d] is [not certified] by PassportClerk[%d]\n",*index,myClerk,0,"",""); printf("Senator [%d] is being forced to wait by PassportClerk[%d]\n",*index,myClerk,0,"",""); rando = Rand()%80+20; for(x = 0; x < rando; x++) Yield(); } }
/* Default acquire test acquiring lock_2 */ void acquire_Lock_Test(){ Write("Testing Acquire on lock_2\n", sizeof("Testing Acquire on lock_2\n"), ConsoleOutput); Acquire(lock_2); }
void RL1() { char* name = "test"; int lock; int lock2; int cv; int wait; Printx("\nTest4.1 is creating lock at index 2\n",37,1); lock = CreateLock(name,4); if (lock == -1) Printx("Lock not created\n",17,1); else Printx("Lock created successfully at index %d\n",38,lock*10000000); Printx("\nTest4.1 is creating lock at index 3\n",37,1); lock2 = CreateLock(name,4); if (lock2 == -1) Printx("Lock not created\n",17,1); else Printx("Lock created successfully at index %d\n",38,lock2*10000000); Printx("\nTest4.1 is acquiring lock at index 2\n",38,1); lock = Acquire(lock); if (lock == -1) Printx("Lock not acquired\n",18,1); else Printx("Lock acquired successfully from index %d\n",41,lock*10000000); Printx("\nTest4.1 is acquiring lock at index 3\n",38,1); lock2 = Acquire(lock2); if (lock2 == -1) Printx("Lock not acquired\n",18,1); else Printx("Lock acquired successfully from index %d\n",41,lock2*10000000); Printx("\nTest4.1 is creating CV at index 0\n",35,1); cv = CreateCondition(name,4); if (cv == -1) Printx("CV not created\n",15,1); else Printx("CV created successfully at index %d\n",36,cv*10000000); Printx("\nTest4.1 is waiting with CV 0 and Lock 3\n",41,1); wait = Wait(cv,lock2); if (wait == -1) Printx("CV did not successfully wait\n",29,1); else Printx("CV is successfully waiting\n",27,1); Printx("\nTest4.1 is releasing lock at index 3\n",38,1); lock = Release(3); if (lock == -1) Printx("Lock not released\n",18,1); else Printx("Lock at index %d released successfully\n",39,lock*10000000); Printx("\nTest4.1 is releasing lock at index 2\n",38,1); lock = Release(2); if (lock == -1) Printx("Lock not released\n",18,1); else Printx("Lock at index %d released successfully\n",39,lock*10000000); Exit(0); }
/* Test to make sure that bad index will be blocked before the syscall */ void acquire_Lock_Bad_Index(){ Write("Testing Acquire on negative index\n", sizeof("Testing Acquire on negative index\n"), ConsoleOutput); Acquire(-1); Write("Testing Acquire on out of bounds index\n", sizeof("Testing Acquire on out of bounds index\n"), ConsoleOutput); Acquire(12); }
/*------------------------------------------------------------------------------*/ void AkindDI::JoyAdd( void ) { m_pDInput->EnumDevices( DIDEVTYPE_JOYSTICK, JoyCallback, this, DIEDFL_ATTACHEDONLY ); Acquire(); }
/* Test to make sure that already deleted lock cannot be acquired */ void acquire_Lock_Already_Deleted(){ Write("Testing Acquire on on deleted lock, lock_1\n", sizeof("Testing Acquire on on deleted lock, lock_1\n"), ConsoleOutput); Acquire(lock_1); }
void patient() { int myIndex,k; int patientWaitingCount; Acquire(patientIndexLock); myIndex=GetMV(globalPatientIndexMV,0); SetMV(globalPatientIndexMV,0,myIndex+1); Release(patientIndexLock); Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" has entered the Doctor's Office Waiting Room.\n"); Release(printLock); Acquire(wrnLineLock); if(GetMV(wrnStatusMV,0)==BUSY) { /*If wrn is busy, enter into line*/ /*Printf((unsigned int)" wrnStatus=>BUSY\n");*/ Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" gets in line of the Waiting Room Nurse to get registration form.\n"); Release(printLock); patientWaitingCount=GetMV(patientWaitingCountMV,0); SetMV(patientWaitingCountMV,0,patientWaitingCount+1); Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" waits in line to get form\n"); Release(printLock); Wait(patientWaitingCV,wrnLineLock); } else if(GetMV(wrnStatusMV,0)==FREE) { /* If wrn is FREE, make wrnStatus = BUSY*/ Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" finds no one is in line\n"); SetMV(wrnStatusMV,0,BUSY); } Release(wrnLineLock); Acquire(wrnLock); SetMV(patientTaskMV,0,GETFORM); SetMV(patientGettingFormMV,0,myIndex); Signal(wrnWaitingCV,wrnLock); Release(wrnLock); /* Allocate random values of age & name to patient*/ Acquire(infoLock); /*SetMV(patientES_age,myIndex,random()%100 + 1); SetMV(patientES_name,myIndex,random()%26 + 1); SetMV(patientES_pid,myIndex,myIndex);*/ Release(infoLock); for(k=0;k<10;k=k+1) { Yield(); } Acquire(wrnLineLock); if(GetMV(wrnStatusMV,0) == BUSY) { /*Printf((unsigned int)"wrnStatus => Busy\n");*/ Acquire(printLock); Printf1((unsigned int)"Adult Patient:",myIndex); Printf((unsigned int)" gets in line of the Waiting Room Nurse to submit registration form.\n"); Release(printLock); patientWaitingCount=GetMV(patientWaitingCountMV,0); SetMV(patientWaitingCountMV,0,patientWaitingCount+1); Wait(patientWaitingCV,wrnLineLock); } else if(GetMV(wrnStatusMV,0) == FREE) { /*Printf((unsigned int)"wrnStatus=>Free\n");*/ SetMV(wrnStatusMV,0,BUSY); } Release(wrnLineLock); Acquire(wrnLock); SetMV(patientTaskMV,0, GIVEFORM); SetMV(patientGivingFormMV,0, myIndex); Acquire(printLock); Printf1((unsigned int)"Adult patient:",myIndex); Printf((unsigned int)" submits the filled form to the Waiting Room Nurse.\n"); Release(printLock); Signal(wrnWaitingCV,wrnLock); Release(wrnLock); }
/* Default test to check if signal syscall works */ void signal_Test(){ Write("Testing Signal on cv_2\n", sizeof("Testing Signal on cv_2\n"), ConsoleOutput); Acquire(lock_2); Signal(cv_2, lock_2); Release(lock_2); }
void CarlJrSimulation (void) { /*************************************************************************************** Intialization of Global Counters ***************************************************************************************/ int NoOfEatIn=0; int NoOfToGo=0; int iInitCntr=0; int jInitCntr=0; int iOTCtr=0; int iCustCtr=0; PersonCount.NoOfOT=0; PersonCount.NoOfCust=0; PersonCount.NoOfCooks=0; PersonCount.NoOfWaiters=0; PersonCount.NoOfMgr=1; /*************************************************************************************** Intialization of Global Variables ***************************************************************************************/ custLineLength=0; CustCount=0; OTcount=0; CookCount=0; WaiterCount=0; MoneyInRstrnt=0; TotalTables=25; CustLeft=0; SleepingWaiterCount=0; ActiveOTCount=0; DestroyAllLockCV=1; /*************************************************************************************** Intialization of Global Arrays ***************************************************************************************/ for(iInitCntr=0;iInitCntr<MAX_SIZE;iInitCntr++) { SigCustList[iInitCntr]=EatInCustWaitingForFood[iInitCntr]=ToGoCustWaitingForFood[iInitCntr]=\ ToGoGrabedFood[iInitCntr]=TableAllotedCustList[iInitCntr]=-1; OrderTakerStatus[iInitCntr]=FREE; } iInitCntr=0; for(iInitCntr=0;iInitCntr<MAX_SIZE;iInitCntr++) { for(jInitCntr=0;jInitCntr<MAX_SIZE;jInitCntr++) { GrabedFood[iInitCntr][jInitCntr]=MgrCookStyleLst[iInitCntr][jInitCntr]=TblNeededCustList[iInitCntr][jInitCntr]=-1; } } iInitCntr=0; jInitCntr=0; /*CookStyle[4][40]={"$6 Burger","$3 Burger","Veggie Burger","French Fries"};*/ ManagerInstrToStop[4]; CookedFood[5]=100000000; for(iInitCntr=0;iInitCntr<5;iInitCntr++) { FoodToBeCooked[iInitCntr]=FoodLevelCheckIteration[iInitCntr]=0; Inventory[iInitCntr]=250; CookedFood[iInitCntr]=100; } iInitCntr=0; /*Print("Before locks\n");*/ /*************************************************************************************** Intialization of locks ***************************************************************************************/ CustDataLock=CreateLock("CustDataLock", 12); InventoryLock=CreateLock("InventoryLock", 13); MgrCookStatUpdateLock=CreateLock("MgrCookStatUpdateLock", 21); CookQLock=CreateLock("InventoryLock", 13); SigCustListLock=CreateLock("SigCustListLock", 15); GrabedFoodLock=CreateLock("GrabedFoodLock", 14); EatInCustWaitingForFoodLock=CreateLock("EatInCustWaitingForFoodLock", 27); ToGoCustWaitingForFoodLock=CreateLock("ToGoCustWaitingForFoodLock", 26); ToGoGrabedFoodLock=CreateLock("ToGoGrabedFoodLock", 18); MgrCookStyleLstLock=CreateLock("MgrCookStyleLstLock", 19); MonitorAmtOfMoneyLock=CreateLock("MonitorAmtOfMoneyLock", 21); OTStatusUpdateLock=CreateLock("OTStatusUpdateLock", 18); CookedFoodLock=CreateLock("CookedFoodLock", 14); TableAllotedCustLock=CreateLock("TableAllotedCustLock", 20); WaitersQLock=CreateLock("WaitersQLock", 12); TotalTablesUpdateLock=CreateLock("TotalTablesUpdateLock", 21); TblNeededCustLock=CreateLock("TblNeededCustLock", 17); FoodToBeCookedUpdateLock=CreateLock("FoodToBeCookedUpdateLock", 24); CustCountLock=CreateLock("CustCountLock", 13); OTCountLock=CreateLock("OTCountLock", 11); WaiterCountLock=CreateLock("WaiterCountLock",15); custLineLock=CreateLock("custLineLock",12); AliveCustCountLock=CreateLock("AliveCustCountLock",18); SleepingWaiterCountLock=CreateLock("SleepingWaiterCountLock",23); ActiveOTCountLock=CreateLock("ActiveOTCountLock",17); iInitCntr=0; for(iInitCntr=0;iInitCntr<MAX_SIZE;iInitCntr++) { NeedInventoryLock[iInitCntr]=CreateLock("NeedInventoryLock",17); OrderTakerLock[iInitCntr]=CreateLock("OrderTakerLock",14); NeedTableLock[iInitCntr]=CreateLock("NeedTableLock",12); NeedWaitersLock[iInitCntr]=CreateLock("NeedWaitersLock",15); } /*Print1("MAX LOCK COUNT =%d\n",NeedWaitersLock[iInitCntr]);*/ iInitCntr=0; /*************************************************************************************** Intialization of all Condition Variables ***************************************************************************************/ CookQCV=CreateCondition("CookQCV",7); custWaitingCV=CreateCondition("custWaitingCV",13); WaitersCV=CreateCondition("WaitersCV",9); iInitCntr=0; for(iInitCntr=0;iInitCntr<MAX_SIZE;iInitCntr++) { OTWaitingCV[iInitCntr]=CreateCondition("OTWaitingCV",11); NeedInventoryCV[iInitCntr]=CreateCondition("NeedInventoryCV",15); NeedTableCV[iInitCntr]=CreateCondition("NeedTableCV",11); NeedWaitersCV[iInitCntr]=CreateCondition("NeedWaitersCV",13); } iInitCntr=0; /*Print("Please enter the number of Customers:"); PersonCount.NoOfCust=Scan("%d"); Print("Please enter the number of Order Takers:"); PersonCount.NoOfOT=Scan("%d"); Print("Please enter the number of Waiters:"); PersonCount.NoOfWaiters=Scan("%d"); Print("Please enter the number of Cooks:"); PersonCount.NoOfCooks=Scan("%d");*/ PersonCount.NoOfCust=20; PersonCount.NoOfOT=4; PersonCount.NoOfWaiters=3; PersonCount.NoOfCooks=4; Acquire(AliveCustCountLock); AliveCustCount=PersonCount.NoOfCust; Release(AliveCustCountLock); Acquire(ActiveOTCountLock); ActiveOTCount=PersonCount.NoOfOT; Release(ActiveOTCountLock); /* create OrderTaker threads */ for(iOTCtr=0; iOTCtr<PersonCount.NoOfOT; iOTCtr++) { Fork(OrderTaker); } /* create Customer threads */ for(iCustCtr=0; iCustCtr<PersonCount.NoOfCust; iCustCtr++) { /*Logic to determine eat-in or To-go type*/ NoOfEatIn = (2*(PersonCount.NoOfCust))/3; NoOfToGo = (PersonCount.NoOfCust) - NoOfEatIn; if(iCustCtr <= NoOfEatIn ) { Init_CustomerData(0, iCustCtr); } else { Init_CustomerData(1, iCustCtr); } Fork(Customer); } /*Create Waiters*/ iInitCntr=0; for (iInitCntr=0; iInitCntr<PersonCount.NoOfWaiters; iInitCntr++) { Fork(Waiter); } iInitCntr=0; /*Create Cooks*/ for (iInitCntr=0; iInitCntr<PersonCount.NoOfCooks; iInitCntr++) { /*Fork(Cook);*/ } iInitCntr=0; /*Create Manager*/ Fork(Manager); }
void SenatorRun() { int cashDollars; int clerkStatus; int index; int tid; tid = GetThreadID(); Acquire(customerSenatorUIDLock); index = customerSenatorUID++; Release(customerSenatorUIDLock); tprintf("Senator[%d]: Acquiring customerOfficeLock\n", index,0,0,"",""); /*customerOfficeLock->Acquire();*/ Acquire(entryLock); if (customersInOffice > 0){ tprintf("Senator[%d]: There are other Customers in office, going to Senator waiting room\n", index,0,0,"",""); senatorsInWaitingRoom++; tprintf("Senator[%d]: In the waiting room, taking a nap...\n", index,0,0,"",""); /*senatorWaitingRoomCV->Wait(senatorWaitingRoomLock);*/ Wait(senatorWaitingRoomCV, entryLock); tprintf("Senator[%d]: Waking up, going to the passport office!\n", index,0,0,"",""); senatorsInWaitingRoom--; } tprintf("Senator [%d]: Entering the passport office...\n",index,0,0,"",""); /*senator start up code*/ cashDollars = ((Rand() % 4) * 500) + 100; printf("Senator [%d] has money = [$%d] ... tid = %d\n",index,cashDollars,tid,"",""); senatorsInOffice++; Release(entryLock); /*choose line*/ tprintf("Senator [%d]: Deciding between AppClerk and PictureClerk...\n", index,0,0,"",""); if(cashDollars > 100) /*find priveledged line with shortest length*/ { /*appPicLineLock->Acquire();*/ Acquire(appPicLineLock); printf("Senator [%d] finds the minimum [priveledged] queue for [ApplicationClerk/PictureClerk]\n", index,0,0,"",""); if(privAppLineLength<=privPicLineLength) { /*appPicLineLock->Release();*/ Release(appPicLineLock); printf("Senator [%d] goes to [ApplicationClerk] first.\n",index,0,0,"",""); doAppClerk(&index, &cashDollars); doPicClerk(&index, &cashDollars); } else { /*appPicLineLock->Release();*/ Release(appPicLineLock); printf("Senator [%d] goes to [PictureClerk] first.\n",index,0,0,"",""); doPicClerk(&index, &cashDollars); doAppClerk(&index, &cashDollars); } } else/*find regular line with shortest length*/ { /*appPicLineLock->Acquire();*/ Acquire(appPicLineLock); printf("Senator [%d] finds the minimum [regular] queue for [ApplicationClerk/PictureClerk]\n", index,0,0,"",""); if(regAppLineLength<=regPicLineLength) { /*appPicLineLock->Release();*/ Release(appPicLineLock); printf("Senator [%d] goes to [ApplicationClerk].\n",index,0,0,"",""); doAppClerk(&index, &cashDollars); doPicClerk(&index, &cashDollars); } else { /*appPicLineLock->Release();*/ Release(appPicLineLock); printf("Senator [%d] goes to [PictureClerk].\n",index,0,0,"",""); doPicClerk(&index, &cashDollars); doAppClerk(&index, &cashDollars); } } /*hit up the passport clerk*/ doPassPortClerk(&index, &cashDollars); /*proceed to cashier*/ doCashierClerk(&index, &cashDollars); Exit(0); }
void OrderTaker() { int MyCustID = -1; int CustFoodIsReady =-1; int Poped_CID = -1; int Poped_OT = -1; int i=0; int myOTId=0; Acquire(OTCountLock); myOTId = OTcount++; Release(OTCountLock); /*Print1("Created %d OT\n", myOTId);*/ while(1) { Acquire(AliveCustCountLock); if(AliveCustCount==0) { Acquire(ActiveOTCountLock); ActiveOTCount--; Release(ActiveOTCountLock); Release(AliveCustCountLock); Exit(1); } else Release(AliveCustCountLock); Acquire(custLineLock); /*Are there any waiting customers?*/ if(custLineLength > 0) { Signal(custWaitingCV,custLineLock); custLineLength--; Release(custLineLock); Acquire(OTStatusUpdateLock); OrderTakerStatus[myOTId]=WAITING; Release(OTStatusUpdateLock); } /*Is there any food to bag?*/ else { Release(custLineLock); Acquire(EatInCustWaitingForFoodLock); for(i=0;i<MAX_SIZE;i++) { if(EatInCustWaitingForFood[i]==1) { EatInCustWaitingForFood[i]=0; MyCustID=i; break; } } i=0; /*For (each) this customer, check if his order is ready for each type of food*/ if(MyCustID!=-1) { CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1;/* 1 means food is grabed and 0 means otherwise*/ Release(GrabedFoodLock); Print2("OrderTaker[%d] packed the food for Customer[%d]\n",myOTId, MyCustID); MyCustID=-1; Release(EatInCustWaitingForFoodLock); } else { /*list of customers to whom token should be handled but shouldnt grab the food*/ /*this Q is as good as saying who gave token number to whom when they wanna wait*/ EatInCustWaitingForFood[MyCustID]=1; Print1("OrderTaker[%d] gives token number to Customer[%d]\n", MyCustID); MyCustID=-1; Release(EatInCustWaitingForFoodLock); } } else { Release(EatInCustWaitingForFoodLock); Acquire(ToGoCustWaitingForFoodLock); i=0; for(i=0;i<MAX_SIZE;i++) { if(ToGoCustWaitingForFood[i]==1) { ToGoCustWaitingForFood[i]=0; MyCustID=i; break; } } i=0; if(MyCustID!=-1) { CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1;/* 1 means food is grabed and 0 means otherwise*/ Release(GrabedFoodLock); Print2("OrderTaker[%d] gives food to the Customer[%d]\n",myOTId, MyCustID); MyCustID=-1; Release(ToGoCustWaitingForFoodLock); } else { /*list of customers to whom token should be handled but shouldnt grab the food /*this Q is as good as saying who gave token number to whom when they wanna wait*/ ToGoCustWaitingForFood[MyCustID]=1; Print2("OrderTaker[%d] gives token number to Customer[%d]\n",myOTId, MyCustID); MyCustID=-1; Release(ToGoCustWaitingForFoodLock); } } else { Release(ToGoCustWaitingForFoodLock); /*Nothing to do /*Set OT status to FREE*/ Acquire(OTStatusUpdateLock); OrderTakerStatus[myOTId]=FREE; Release(OTStatusUpdateLock); Acquire(AliveCustCountLock); if(AliveCustCount==0) { Acquire(ActiveOTCountLock); ActiveOTCount--; Release(ActiveOTCountLock); Release(AliveCustCountLock); Exit(1); } else Release(AliveCustCountLock); } } } /*Now, OT is ready to take order /* Take interaction lock*/ Acquire(OrderTakerLock[myOTId]); /*release hold on CustLineLen /*OT should wait until customer signals him with orders*/ /*Print1(" %d OT waiting for customers to signal\n", myOTId);*/ Wait(OTWaitingCV[myOTId],OrderTakerLock[myOTId]); /*Print1(" %d OT waiting for customers is siganlled!! Yay!!\n", myOTId);*/ /*When I'm here, Customer has signaled that his order is ready*/ /*Compute the money and handle the token number.*/ /*check which customer signalled me?*/ Acquire(SigCustListLock); /*Print2("Acquiried SigCustListLock%d by %d\n",SigCustListLock,myOTId);*/ i=0; for(i=0;i<MAX_SIZE;i++) { /*Print("Inside For loop\n");*/ if(SigCustList[i]==myOTId) { MyCustID=i; SigCustList[i]=-1; break; } } i=0; /*Print2("Releasing SigCustListLock%d by %d\n",SigCustListLock,myOTId);*/ /*Print1("Cust ID after I poped out %d\n",MyCustID);*/ Acquire(MonitorAmtOfMoneyLock); Acquire(CustDataLock); MoneyInRstrnt = (6 * CustData[MyCustID].Food[0] + 3 * CustData[MyCustID].Food[1] + 4 * CustData[MyCustID].Food[2] \ + 2 * CustData[MyCustID].Food[3] + CustData[MyCustID].Food[4]); Release(MonitorAmtOfMoneyLock); Release(SigCustListLock); Release(CustDataLock); Print2("OrderTaker[%d] is taking order of the Customer [%d]\n", myOTId, MyCustID); /*Order Processing /* Check the customer style of eating - eat in or to-go?*/ Acquire(CustDataLock); if( CustData[MyCustID].TypeOfFood == 0) { Release(CustDataLock); /*Eat - in customers /*If type of food is only Soda, token is handled /*and add to GrabedFood*/ if( MyCustID%5==4) { /*Update the list which waiter will look to serve customers*/ Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1; Release(GrabedFoodLock); } /* Customers with multiple orders*/ else { /*For (each) this customer, check if his order is ready for each type of food*/ CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(GrabedFoodLock); GrabedFood[myOTId][MyCustID]=1;/* 1 means food is grabed and 0 means otherwise*/ Release(GrabedFoodLock); Print2("OrderTaker[%d] gives token number to Customer[%d]\n",myOTId, MyCustID); } else { /*list of customers to whom token should be handled but shouldnt grab the food /*this Q is as good as saying who gave token number to whom when they wanna wait*/ Acquire(EatInCustWaitingForFoodLock); EatInCustWaitingForFood[MyCustID]=1; Release(EatInCustWaitingForFoodLock); Print2("OrderTaker[%d] gives token number to Customer[%d]\n",myOTId, MyCustID); } } } /* Customers of type togo*/ else { Release(CustDataLock); if( MyCustID%5 == 4)/* If customer needs only Soda*/ { /*Signal this waiting customer and "wait" for him to acknowledge*/ /*If acknowledged, signal the waiting customer and Customer threads finishes*/ } /* Customers with multiple orders*/ else { /*For (each) this customer, check if his order is ready for each type of food*/ CustFoodIsReady = IsCustomerFoodReady (MyCustID); /* if CustFoodIsReady = 1, Food is ready and can be grabed*/ if (CustFoodIsReady==1) { Acquire(ToGoGrabedFoodLock); ToGoGrabedFood[MyCustID]=1; Release(ToGoGrabedFoodLock); Print2("OrderTaker[%d] packed the food for Customer[%d]\n",myOTId, MyCustID); /*Signal(OTWaitingCV[myOTId],OrderTakerLock[myOTId]); Release(OrderTakerLock[myOTId]); continue;*/ } else { /*list of customers to whom token should be handled but shouldnt grab the food*/ /*this Q is as good as saying who gave token number to whom when they wanna wait*/ Acquire(ToGoCustWaitingForFoodLock); ToGoCustWaitingForFood[MyCustID]=1; Release(ToGoCustWaitingForFoodLock); Print2("OrderTaker[%d] gives token number to Customer[%d]\n",myOTId, MyCustID); } } } /*Order processed Signal the customer to get out of sleep!*/ Signal(OTWaitingCV[myOTId],OrderTakerLock[myOTId]); Release(OrderTakerLock[myOTId]); Acquire(custLineLock); /*Are there any waiting customers?*/ if(custLineLength==0) { Release(custLineLock); Acquire(ActiveOTCountLock); ActiveOTCount--; Release(ActiveOTCountLock); /*Print1("\t\t\t\n\nOT %d is exiting\n\n\n\n",myOTId);*/ Exit(1); } else Release(custLineLock); }/*end of while()*/ }/*end of OrderTaker()*/
static void doAppClerk(int* index, int* cashDollars) { bool privLine = FALSE; int myClerk, x; while(TRUE) { myClerk = -1; tprintf("Senator [%d]: Going to the AppClerk\n",*index,0,0,"",""); /*appPicLineLock->Acquire(); */ Acquire(appPicLineLock); tprintf("Senator [%d]: What line should I choose for the AppClerk?\n",*index,0,0,"",""); /*check for senator*/ if(*cashDollars > 100) /*get in a privledged line*/ { privLine = TRUE; tprintf("Senator [%d]: Priveleged line, baby. CashDollars = $%d\n",*index,*cashDollars,0,"",""); privAppLineLength++; tprintf("Senator [%d]: Waiting in the Priveledged Line for next available AppClerk\n",*index,0,0,"",""); /*privAppLineCV->Wait(appPicLineLock); //wait for clerk */ Wait(privAppLineCV, appPicLineLock); } else /*get in a normal line*/ { tprintf("Senator [%d]: Regular line, suckas. CashDollars = $%d\n",*index,*cashDollars,0,"",""); regAppLineLength++; tprintf("Senator [%d]: Waiting in the Regular Line for next available AppClerk\n",*index,0,0,"",""); /*regAppLineCV->Wait(appPicLineLock); //wait for clerk */ Wait(regAppLineCV, appPicLineLock); } tprintf("Senator [%d]: Finding available AppClerk...\n",*index,0,0,"",""); for(x = 0; x < MAX_APP_CLERKS; x++) { if(appClerkStatuses[x] == CLERK_AVAILABLE) /*find available clerk*/ { myClerk = x; tprintf("Senator [%d]: Going to chill with AppClerk [%d]\n",*index,myClerk,0,"",""); appClerkStatuses[myClerk] = CLERK_BUSY; break; } else tprintf("Senator [%d]: AppClerk [%d] is unavailable\n",*index,x,0,"",""); } if (myClerk == -1) { printf("Senator [%d]: Woke up with a AppClerk index of -1, halting the machine for now.",*index,0,0,"",""); Halt(); } /*appClerkLocks[myClerk]->Acquire(); appPicLineLock->Release();*/ Acquire(appClerkLocks[myClerk]); Release(appPicLineLock); if(privLine) { printf("Senator [%d] is willing to pay $500 to ApplicationClerk [%d] for moving ahead in line\n",*index, myClerk,0,"",""); appClerkMoney[myClerk] += 500; appClerkBribed[myClerk] = TRUE; *cashDollars -= 500; } tprintf("Senator [%d]: Interacting with AppClerk [%d]\n",*index,myClerk,0,"",""); /*interact with clerk */ printf("Senator [%d] gives application to ApplicationClerk [%d] = [SSN: %d].\n", *index,myClerk,*index,"",""); appClerkSSNs[myClerk] = *index; /*appClerkCVs[myClerk]->Signal(appClerkLocks[myClerk]);*/ Signal(appClerkCVs[myClerk], appClerkLocks[myClerk]); /*appClerkCVs[myClerk]->Wait(appClerkLocks[myClerk]);*/ Wait(appClerkCVs[myClerk], appClerkLocks[myClerk]); printf("Senator [%d] is informed by ApplicationClerk [%d] that the application has been filed.\n", *index, myClerk,0,"",""); tprintf("Senator [%d]: Done and done.\n",*index,0,0,"",""); /*appClerkLocks[myClerk]->Release();*/ Release(appClerkLocks[myClerk]); tprintf("Senator [%d]: Going to next clerk...\n",*index,0,0,"",""); break; } }
void Waiter() { int MyWaiterID=0; int NoOfCustServed=0; int i=0; int j=0; int k=0; Acquire(WaiterCountLock); MyWaiterID = WaiterCount++; Release(WaiterCountLock); /*Print1("Created %d Waiter\n", MyWaiterID);*/ while(1) { /*Check if there are customers seated and thier food is ready Looping myself */ Acquire(GrabedFoodLock); /*See for which customers food is grabed */ for(i=0;i<MAX_SIZE;i++) { if(NoOfCustServed>0) { NoOfCustServed=0; break; } for(j=0;j<MAX_SIZE;j++) { if(GrabedFood[i][j]==1) { if(NoOfCustServed>0) { break; } Acquire(TableAllotedCustLock); for(k=0;k<MAX_SIZE;k++) /* see if that customer is seated */ { if(TableAllotedCustList[k]==j) { Acquire(NeedWaitersLock[j]); /*Customer is ready to be served*/ Signal(NeedWaitersCV[j],NeedWaitersLock[j]); Release(NeedWaitersLock[j]); Print3("Waiter [%d] got token number [%d] for Customer [%d]\n",MyWaiterID,j,j); Print2("Waiter [%d] validates the token number for Customer [%d]\n",MyWaiterID,j); Print2("Waiter [%d] serves food to Customer [%d]\n",MyWaiterID,j); TableAllotedCustList[k]=-1; /*Print2("TableAllotedCustList[%d]=%d\n",k,TableAllotedCustList[k]);*/ GrabedFood[i][j]=-1; NoOfCustServed++; Acquire(TotalTablesUpdateLock); TotalTables++; Release(TotalTablesUpdateLock); break; } } Release(TableAllotedCustLock); } } if(NoOfCustServed>0) { NoOfCustServed=0; break; } } i=j=k=0; Release(GrabedFoodLock); for( i=0; i<20;i++) { /*currentThread->Yield();*/ } /* There are no customers waiting, Waiters go on break */ Acquire(WaitersQLock); Print1("Waiter[%d] going on break\n", MyWaiterID); Acquire(SleepingWaiterCountLock); SleepingWaiterCount++; Release(SleepingWaiterCountLock); Wait(WaitersCV,WaitersQLock); Acquire(SleepingWaiterCountLock); SleepingWaiterCount--; Release(SleepingWaiterCountLock); Release(WaitersQLock); Acquire(AliveCustCountLock); /*Print1("AliveCustCount%d\n",AliveCustCount);*/ if(AliveCustCount==0) { /*Print1("Waiter %d is exiting\n",MyWaiterID);*/ Release(AliveCustCountLock); Exit(1); } else { Print1(" Waiter[%d] returned from break\n", MyWaiterID); Release(AliveCustCountLock); } }/*end of while()*/ } /*end of waiter() */
void startAppClerk() { int i, id; Acquire(DataLock); id = numActiveAppClerks; numActiveAppClerks++; Release(DataLock); while(true) { Acquire(AppClerkLineLock); if (SenatorArrived) { } else if (queue_size(&AppClerks[id].bribeLine) != 0) { Signal(AppClerkLineLock, AppClerks[id].bribeLineCV); AppClerkBribeMoney += 500; AppClerks[id].state = 1; AppClerks[id].currentCustomer = queue_pop(&AppClerks[id].bribeLine); Printf("ApplicationClerk %d has received $500 from Customer %d\n", sizeof("ApplicationClerk %d has received $500 from Customer %d\n"), id*1000+AppClerks[id].currentCustomer); } else if (queue_size(&AppClerks[id].line) != 0) { Signal(AppClerkLineLock, AppClerks[id].lineCV); AppClerks[id].state = 1; AppClerks[id].currentCustomer = queue_pop(&AppClerks[id].line); Printf("ApplicationClerk %d has signalled a Customer to come to their counter\n", sizeof("ApplicationClerk %d has signalled a Customer to come to their counter\n"), id); } else { Acquire(AppClerks[id].lock); AppClerks[id].state = 2; Printf("ApplicationClerk %d is going on break\n", sizeof("ApplicationClerk %d is going on break\n"), id); if(numCustomers == 0) break; Release(AppClerkLineLock); Wait(AppClerks[id].lock, AppClerks[id].cv); Printf("ApplicationClerk %d is coming off break\n", sizeof("ApplicationClerk %d is coming off break\n"), id); Signal(AppClerks[id].lock, AppClerks[id].cv); AppClerks[id].state = 0; Release(AppClerks[id].lock); continue; } Acquire(AppClerks[id].lock); Release(AppClerkLineLock); Wait(AppClerks[id].lock, AppClerks[id].cv); Printf("ApplicationClerk %d has received SSN %d from Customer %d\n", sizeof("ApplicationClerk %d has received SSN %d from Customer %d\n"), id*1000000+AppClerks[id].currentCustomer*1000+AppClerks[id].currentCustomer); Release(AppClerks[id].lock); for(i =20; i<100; ++i){ Yield(); } Acquire(AppClerks[id].lock); Signal(AppClerks[id].lock, AppClerks[id].cv); Printf("ApplicationClerk %d has recorded a completed application for Customer %d\n", sizeof("ApplicationClerk %d has recorded a completed application for Customer %d\n"), id*1000+AppClerks[id].currentCustomer); Wait(AppClerks[id].lock, AppClerks[id].cv); if (SenatorArrived) { Acquire(SenatorLock); Wait(SenatorLock, AppClerks[id].senatorCV); Release(SenatorLock); } AppClerks[id].currentCustomer = -1; Release(AppClerks[id].lock); } Exit(0); }