int main() { int i,j; char lockName[10]; int money; TestMe(33,0); TestMe(55,1); TestMe(66,2); TestMe(44,0); TestMe(77,0); TestMe(88,1); TestMe(99,1); TestMe(11,1); TestMe(22,1); TestMe(33,1); TestMe(40,1); i = CreateLock("TestLock",8); WriteNum(i);Write("\n",1,1); for(i=0;i<29;i++) { Concatenate("TestLock",sizeof("testlock"),i,lockName); j = CreateLock(lockName, sizeof(lockName)); WriteNum(j);Write("\n",1,1); } /* AcquireLock(i-1); ReleaseLock(i-1); money = CreateSharedInt("money",5,10); SetSharedInt(money,5,1); SetSharedInt(money,3,1); SetSharedInt(money,8,10); i = ArraySearch(money,3,1); WriteNum(i);Write("\n",1,1); i = ArraySearch(money,99,10); WriteNum(i);Write("\n",1,1); */ }
void TestFunc() { int id; AcquireLock(visitorCountLock); id = visitorCount++; ReleaseLock(visitorCountLock); Write("\nThread #",20 , 1); WriteNum(id);Write(" Forked \n",20,1); Write("\nYielding..\n",100,1); Yield(); Exit(0); }
/** @brief 将val转换为字符串并写入p所指定的内存中,采用指数方式,并返回写入长度,不足minlen时小数点后补0,d为进制数*/ int WriteFloatE(double val, wchar_t*p, int prec, int minlen, int d) { if (val == 0) { int i = 1; *p++ = L'0'; if (minlen > 1) { *p++ = '.'; i++; } while (i < minlen)p[i++] = L'0'; return i; } if (val != val) { int i = 0; do { *p++ = L'#'; } while (++i<minlen); return i; } bool nagtive = false; if (val < 0) { val = -val; if (minlen > 0)minlen--; *p++ = L'-'; nagtive = true; } int exp = (int)(log(val) / log((double)d)); if (exp != 0 && -exp == exp) { int i = 0; do { *p++ = L'∞'; } while (++i<minlen); return nagtive + i; } val *= pow((double)d, -exp); if (val >= d) { val /= d; exp++; } else if (val < 1) { val *= d; exp--; } int len = 1; *p++ = (wchar_t)val; val = (val - (int)val)*d; if (prec > 1) { if (val != 0) { *p++ = L'.'; do { *p++ = (wchar_t)val; val = (val - (int)val)*d; ++len; } while (val != 0 && len < prec); if (val * 2 >= d) {//舍入 wchar_t* pp = p - 1; while (*pp != L'.'&&++*pp == d) { *pp = 0; pp--; } if (*pp == L'.') { --p; --len; if (++*--pp == d) { *pp = 1; exp++; --p; } } } if (len != 1)++len; } for (wchar_t* pp = p - len; pp != p; ++pp) { if (*pp == L'.')continue; if (*pp >= 10) *pp += L'A'; else *pp += L'0'; } } wchar_t tmps[16]; int testlen = WriteNum(exp, tmps, exp < 0 ? 2 : 1, d); while (len + testlen + 1<minlen) { if (len++ != 1)*p++ = L'0'; else *p++ = L'.'; } *p++ = L'e'; for (minlen = 0; minlen < testlen; ++minlen)p[minlen] = tmps[minlen]; return len + testlen + 1 + nagtive; }
void President() { int presidentNumberOfCalls=0; /* keep count of total number of calls made by the president */ int checkCorrectOperatorP; int waitingTime; int condnToWait; int phoneToUse, gotPhone,i,j; int operatorToUse; int talkingTime; int phoneStatus_i; int num; char lockName[30]; char condName1[30]; char condName2[30]; int presidentStatus; int phoneStatus; char printing[50]; int lockID1, lockID2, lockID3, lockID4, lockID5, lockID6, condID1, condID2, condID3, condID4; int indOpLock[20], waitForOperVerCV[20], waitForCallerCV[20]; int activate, authMechanism, freeOperators, operatorStatus; /* Create or get access to some shared variables, locks, conditions */ presidentStatus = CreateSharedInt("presidentStatus",15,1); phoneStatus = CreateSharedInt("phoneStatus",11,NOOFPHONES); lockID1 = CreateLock("phoneLock",10); /* obtain a master lock for all phones */ freeOperators = CreateSharedInt("freeOperators",13,1); operatorStatus = CreateSharedInt("operatorStatus",14,Nop); activate = CreateSharedInt("activate",sizeof("activate"),Nop); authMechanism = CreateSharedInt("authMechanism",sizeof("authMechanism"),Nop); lockID2 = CreateLock("GlobalOpLock",sizeof("globaloplock")); /* obtain a master lock for all the operators */ lockID3 = CreateLock("visitorCountLock",17); /* obtain a lock to keep track of the number of visitors permitted to make a call */ lockID4 = CreateLock("NumSenators",12); lockID5 = CreateLock("NumVisitors",12); lockID6 = CreateLock("NumOperators",13); /* displayLock = CreateLock("DispLock",7); */ /* Lock **individualOperatorLock; */ /* obtain an individual lock for every operator */ condID1 = CreateCondition("presiNeedsPhone",16); /* condition variable for the condition that president needs phone */ condID2 = CreateCondition("senatorNeedsPhone",18); /* condition variable for the condition that senator needs phone */ condID3 = CreateCondition("visitorNeedsPhone",18); /* condition variable for the condition that visitor needs phone */ condID4 = CreateCondition("processCustomer",16); /* condition variable to allow president/senator/visitor to make a call */ for (i=0;i<Nop;i++) { Concatenate("OperatorLock",sizeof("OperatorLock"),i,lockName); Concatenate("waitForOpVer",sizeof("waitForOpVer"),i,condName1); Concatenate("waitForCaller",sizeof("waitForCaller"),i,condName2); indOpLock[i] = CreateLock(lockName,sizeof(lockName)); waitForOperVerCV[i] = CreateCondition(condName1,sizeof(condName1)); waitForCallerCV[i] = CreateCondition(condName2,sizeof(condName2)); } while(presidentNumberOfCalls<3) { WriteMe("President Going to speak for the ");WriteNum(presidentNumberOfCalls);WriteMe("th time\n"); condnToWait = FALSE; phoneToUse = 0; AcquireLock(lockID1); SetSharedInt(presidentStatus, 0, 1); /* presidentStatus = 1; */ /* loop for the president to keep waiting even if a single phone is busy */ do { /* for(i=0;i<NOOFPHONES;i++) { phoneStatus_i = GetSharedInt(phoneStatus,i); if(phoneStatus_i == BUSY) { condnToWait = TRUE; break; } } */ i = GetZeroIndex(phoneStatus); if(i==NOOFPHONES) condnToWait=FALSE; else condnToWait = TRUE; if(condnToWait==TRUE) WaitCV(condID1,lockID1); }while(condnToWait==TRUE); /* all phones are free now */ SetSharedInt(phoneStatus, phoneToUse, BUSY); /* phoneStatus[phoneToUse] = BUSY;*/ /* president has obtained a phone now */ ReleaseLock(lockID1); /* Need to get an operator */ AcquireLock(lockID2); /* loop to wait till an operator is available */ while(GetSharedInt(freeOperators,0)==0) WaitCV(condID4,lockID2); /* president has to wait if there are no free operators available */ /* Some operator is available. Though I don't know who it is yet, let us find out */ /* for(j=0;j<Nop;j++) { if(GetSharedInt(operatorStatus,j)==FREE) { operatorToUse = j; break; } } */ operatorToUse = GetOneIndex(operatorStatus); /* operator obtained */ /* check if the operator to whom president goes for authentication is same as the one who permits him/her to make a call. */ checkCorrectOperatorP = operatorToUse; AcquireLock(indOpLock[operatorToUse]); SetSharedInt(activate, operatorToUse, 2); SetSharedInt(operatorStatus, operatorToUse, BUSY); SetSharedInt(freeOperators, 0, GetSharedInt(freeOperators, 0) - 1); ReleaseLock(lockID2); SetSharedInt(authMechanism, operatorToUse, 1); /* 1 for President | 2 for Senators | 3 for Visitors */ /* If operator is sleeping, wake up */ SignalCV(waitForCallerCV[operatorToUse],indOpLock[operatorToUse]); while(GetSharedInt(activate,operatorToUse)==2) WaitCV(waitForOperVerCV[operatorToUse],indOpLock[operatorToUse]); ReleaseLock(indOpLock[operatorToUse]); if(GetSharedInt(activate,operatorToUse)==0) /* President is denied access to phone.But this will never happen as there is only one president and we assume that his/her ID is never faked */ { /* printf("President is denied access to Phone failing authentication!\n"); */ Write("President is denied access to Phone failing authentication!\n",sizeof("President is denied access to Phone failing authentication!\n"),1); } else if(GetSharedInt(activate, operatorToUse)==1) /* operator succesfully authenticates the identity of the president */ { /* Now Talk */ talkingTime = RandomFunction(20); /* randomly generate the amount of time the president is talking */ /* loop for the president to talk on the phone for the randomly generated time period */ for (j=1;j<=talkingTime;j++){ /*printf("President \t %d \t\t %d/%d units \t %d \t\t %d \t ACCEPTED NOTAPPLICABLE - verified by operator %d \n",phoneToUse+1,j,talkingTime,operatorToUse+1,presidentNumberOfCalls+1,checkCorrectOperatorP+1);*/ /*AcquireLock(displayLock); */ Write("President \t ",13,1); num = phoneToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("\t\t ",5,1); itoa(printing,10,j); Write(printing,sizeof(printing),1); Write("/",1,1); num=talkingTime; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" units \t ",10,1); num=operatorToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t\t ",6,1); num=presidentNumberOfCalls+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t ACCEPTED NOTAPPLICABLE - verified by operator ",56,1); num=checkCorrectOperatorP+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \n",3,1); /*ReleaseLock(displayLock);*/ /* Yield();*/ } /* president is done talking */ /* Set the status to be free */ AcquireLock(lockID1); SetSharedInt(phoneStatus, phoneToUse, FREE); SetSharedInt(presidentStatus,0,0); BroadcastCV(condID2,lockID1); /* wake up all the senators waiting to talk */ BroadcastCV(condID3,lockID1); /* wake up all the visitors waiting to talk */ ReleaseLock(lockID1); } /* president goes away */ /* president waits for a random amount of time before coming back to make the next call. Remember maximum number of calls allowed is 5 */ waitingTime = RandomFunction(4); for(j=0;j<waitingTime;j++) { Yield(); } presidentNumberOfCalls++; /* increment the number of calls made by the president */ } Exit(0); }
void Visitor() /* code block to perform visitor operation */ { int condnToWait,who,i,j, num; int talkingTime; int checkCorrectOperatorV; int phoneToUse,gotPhone; int thisActivate; int operatorToUse; int callPresident; /* Some common parameters */ char lockName[30]; char condName1[30]; char condName2[30]; int presidentStatus; int phoneStatus; char printing[50]; int lockID1, lockID2, lockID3, lockID4, lockID5, lockID6, condID1, condID2, condID3, condID4; int indOpLock[20], waitForOperVerCV[20], waitForCallerCV[20]; int activate, authMechanism, freeOperators, operatorStatus; int repositoryMoney; presidentStatus = CreateSharedInt("presidentStatus",15,1); phoneStatus = CreateSharedInt("phoneStatus",11,NOOFPHONES); freeOperators = CreateSharedInt("freeOperators",13,1); operatorStatus = CreateSharedInt("operatorStatus",14,Nop); activate = CreateSharedInt("activate",sizeof("activate"),Nop); authMechanism = CreateSharedInt("authMechanism",sizeof("authMechanism"),Nop); repositoryMoney = CreateSharedInt("repositoryMoney",sizeof("repositoryMoney"),Nop); lockID1 = CreateLock("phoneLock",10); /* obtain a master lock for all phones */ lockID2 = CreateLock("GlobalOpLock",12); /* obtain a master lock for all the operators */ lockID3 = CreateLock("visitorCountLock",17); /* obtain a lock to keep track of the number of visitors permitted to make a call */ lockID4 = CreateLock("NumSenators",12); lockID5 = CreateLock("NumVisitors",12); lockID6 = CreateLock("NumOperators",13); /* displayLock = CreateLock("DispLock",7); */ /* Lock **individualOperatorLock; */ /* obtain an individual lock for every operator */ condID1 = CreateCondition("presiNeedsPhone",16); /* condition variable for the condition that president needs phone */ condID2 = CreateCondition("senatorNeedsPhone",18); /* condition variable for the condition that senator needs phone */ condID3 = CreateCondition("visitorNeedsPhone",18); /* condition variable for the condition that visitor needs phone */ condID4 = CreateCondition("processCustomer",16); /* condition variable to allow president/senator/visitor to make a call */ for (i=0;i<Nop;i++) { Concatenate("OperatorLock",sizeof("OperatorLock"),i,lockName); Concatenate("waitForOpVer",sizeof("waitForOpVer"),i,condName1); Concatenate("waitForCaller",sizeof("waitForCaller"),i,condName2); indOpLock[i] = CreateLock(lockName,sizeof(lockName)); waitForOperVerCV[i] = CreateCondition(condName1,sizeof(condName1)); waitForCallerCV[i] = CreateCondition(condName2,sizeof(condName2)); } /* End of common parameters */ AcquireLock(lockID5); who = NumVisitor; NumVisitor++; ReleaseLock(lockID5); AcquireLock(lockID1); /* loop to check if the president or senator is waiting. If any one is waiting, then visitor has to wait before he/she can make a call. Otherwise visitor can go ahead */ do { condnToWait = TRUE; if(GetSharedInt(presidentStatus,0) == 1) condnToWait = TRUE; /* Check if some senator is already waiting! */ else if(CheckCondWaitQueue(condID2)==1) { /* Bad luck, there seems to be a senator. */ condnToWait = TRUE; } else { /* for(i=0;i<NOOFPHONES;i++) { if(GetSharedInt(phoneStatus,i)==FREE) { phoneToUse = i; SetSharedInt(phoneStatus,i,BUSY); condnToWait = FALSE; break; } } */ phoneToUse = GetOneIndex(phoneStatus); if(phoneToUse!=NOOFPHONES) condnToWait = FALSE; } if(condnToWait) WaitCV(condID3,lockID1); /* visitor waits if there is a president or a senator already waitng to make a call. */ }while(condnToWait); ReleaseLock(lockID1); /* Visitor has got a phone */ /* Need to get an operator now */ AcquireLock(lockID2); while(GetSharedInt(freeOperators,0)==0) WaitCV(condID4,lockID2); /* visitor has to wait if there are no free operators available */ /* Some operator is available. Though I don't know who it is. Let us find out. */ /* for(j=0;j<Nop;j++) { if(GetSharedInt(operatorStatus,j)==FREE) { operatorToUse = j; break; } } */ operatorToUse = GetOneIndex(operatorStatus); /* operator obtained */ checkCorrectOperatorV = operatorToUse; /* check if the operator to whom the visitor pays money is the same as the one the permits/denies the visitor to make a call */ AcquireLock(indOpLock[operatorToUse]); SetSharedInt(activate, operatorToUse, 2); SetSharedInt(operatorStatus, operatorToUse, BUSY); SetSharedInt(freeOperators, 0, GetSharedInt(freeOperators, 0) - 1); ReleaseLock(lockID2); SetSharedInt(authMechanism, operatorToUse, 3); /* 1 for President | 2 for Senators | 3 for Visitors */ SetSharedInt(repositoryMoney, operatorToUse, ((RandomFunction(100)-1)>80)?0:1); /* randomly generate whether the visitor pays $1 or not */ /* If operator is sleeping, wake up */ SignalCV(waitForCallerCV[operatorToUse],indOpLock[operatorToUse]); SignalCV(waitForCallerCV[operatorToUse],indOpLock[operatorToUse]); while(GetSharedInt(activate,operatorToUse)==2) WaitCV(waitForOperVerCV[operatorToUse],indOpLock[operatorToUse]); thisActivate=0; thisActivate=GetSharedInt(activate, operatorToUse); ReleaseLock(indOpLock[operatorToUse]); if (thisActivate==0) { /* visitor is denied access to phone beacause he/she didn't pay $1. */ j=0; talkingTime=0; /* printf("Visitor%d \t UNAVAILABLE \t %d/%d units \t %d \t NOTAPPLICABLE DENIED \t Money paid is $0 - verified by operator %d \n",who+1,j,talkingTime,operatorToUse+1,checkCorrectOperatorV+1); */ /* AcquireLock(displayLock); */ Write("Visitor ",8,1); num = who+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t UNAVAILABLE \t",100,1); num = j; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("/",1,1); num=talkingTime; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" units \t ",10,1); num=operatorToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t",6,1); Write(" NOTAPPLICABLE DENIED \t Money paid is $0 - verified by operator ",100,1); num=checkCorrectOperatorV+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \n",3,1); /*ReleaseLock(displayLock);*/ Yield(); /* printf("Access to Phone for visitor %d Denied by Operator %d!\n",who+1,operatorToUse+1); */ } else if (thisActivate==1) /* visitor has paid $1. Operator verifies and visitor is allowed to make a call */ { /* Now Talk */ talkingTime = RandomFunction(5); /* randomly generate the amount of time the visitor will talk on the phone */ /* loop for the visitor to talk on the phone for the randomly generated time period */ for (i=1;i<=talkingTime;i++){ /* printf("Visitor%d \t %d \t\t %d/%d units \t %d \t NOTAPPLICABLE ACCEPTED \t Money paid is $1 - verified by operator %d \n",who+1,phoneToUse+1,i,talkingTime,operatorToUse+1,checkCorrectOperatorV+1); */ /*AcquireLock(displayLock);*/ Write("Visitor ",8,1); num = who+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t",2,1); num = phoneToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("\t\t ",5,1); num = i; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("/",1,1); num=talkingTime; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" units \t ",10,1); num=operatorToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t",6,1); Write(" NOTAPPLICABLE ACCEPTED \t Money paid is $1 - verified by operator ",100,1); num=checkCorrectOperatorV+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \n",3,1); /*ReleaseLock(displayLock);*/ /*Yield();*/ } /* visitor is done talking */ /* Set the phone status to be free */ } AcquireLock(lockID1); SetSharedInt(phoneStatus,phoneToUse,FREE); if(GetSharedInt(presidentStatus,0)==0) /* president is not waking to talk */ { if(CheckCondWaitQueue(condID2)) SignalCV(condID2,lockID1); /* wake up the next senator waiting to talk */ else SignalCV(condID3,lockID1); /* if no senator is waiting, then wake up the next visitor waiting to talk */ } else /* president is waiting to talk, so senators and visitors will have to wait */ { callPresident = TRUE; /* for(i=0;i<NOOFPHONES;i++) if((i!=phoneToUse)&&(GetSharedInt(phoneStatus,i)==BUSY)) // check if even a single phone is busy other than the phone just used by the visitor which he/she sets to free { callPresident = FALSE; break; }*/ i = ArraySearch(phoneStatus, phoneToUse, BUSY); if(i!=NOOFPHONES) callPresident = FALSE; if(callPresident==TRUE) SignalCV(condID1,lockID1); /* if all phones are free, then no one is talking currently and so, signal the president */ } /* visitor goes away and does not return. Remember visitors can make a maximum of just one call */ ReleaseLock(lockID1); WriteMe("Visitor ");WriteNum(who + 1);WriteMe("Leaving\n"); Exit(0); }
void Operator() { /* loop for the operator thread to run continuously */ int who; AcquireLock(lockID6); who = NumOperator; NumOperator++; ReleaseLock(lockID6); while(1) { AcquireLock(lockID2); if(CheckCondWaitQueue(condID4)) /* checks if anyone (president/senator/visitor) is waiting for an operator */ { SignalCV(condID4,lockID2); /* signal the waiting person */ } /* I (operator) am free. So make my status as free so that some customer might be able to use me. */ operatorStatus[who]=FREE; freeOperators++; /* Acquire lock specific to me */ AcquireLock(indOpLock[who]); ReleaseLock(lockID2); /* Initialize some values */ authenticationMechanism[who]=0; while(authenticationMechanism[who]==0) /* wait till some one is waiting for the operator - 1->President, 2->Senator, 3->Visitor */ WaitCV(waitForCallerCV[who],indOpLock[who]); switch(authenticationMechanism[who]) /* process the customer based on whether the authenticationMechanism value is 1 or 2 or 3 */ { case 0: /* printf("Illegal\n"); */ break; case 1: /* president is talking to operator */ activate[who]=1; /* allow him/her to talk */ break; case 2: /* senator is talking to operator */ if(repositoryID[who]>=1000) { activate[who]=1; /* allow him/her to talk on verification of ID */ } else { activate[who]=0; /* deny access to phone for senator is ID verifiaction fails */ } break; case 3: /* visitor is talking to operator */ if(repositoryMoney[who]==1) { activate[who]=1; /* allow him/her to talk if the visitor pays $1 */ moneyReserve[who]++; /* increment the amount of money collected by the current operator */ AcquireLock(lockID3); visitorAcceptCount++; /* increment the number of visitors permitted to make a call */ ReleaseLock(lockID3); } else if(repositoryMoney[who]==0) { activate[who]=0; /* deny the visitor to make a call bacause he/she failed to pay $1 */ } break; default: /* printf("Unknown Authentication Type\n"); */ } SignalCV(waitForOperVerCV[who],indOpLock[who]); ReleaseLock(indOpLock[who]); } Exit(0); } void Summary() /* print the number of visitors, money collected by each operator and total money */ { int notTheEnd = FALSE; int i,totalMoney=0; int j,k; do { notTheEnd = FALSE; for(k=0;k<(Ns+Nv+1)*100;k++) /* yield the summary thread until all the other threads have finished executing */ Yield(); if( !CheckCondWaitQueue(condID1) || !CheckCondWaitQueue(condID2) || !CheckCondWaitQueue(condID3) ) { AcquireLock(lockID1); for(i=0;i<NOOFPHONES;i++) { if((phoneStatus[i]==BUSY)) { notTheEnd = TRUE; break; } } ReleaseLock(lockID1); /* WriteMe("\n\nSummary\n"); WriteMe("~~~~~~~\n"); */ if(!notTheEnd) { if ((typeOfTest!=9)&&(typeOfTest!=10)&&(typeOfTest!=15)&&(typeOfTest!=16)) { WriteMe("\n\nSummary\n"); WriteMe("\n----------\n"); WriteMe("Total number of Visitors : ");WriteNum(Nv); Write("\n",1,1); WriteMe("Number of Visitors Accepted : ");WriteNum(visitorAcceptCount);WriteMe("\n"); WriteMe("Number of Visitors Denied : ");WriteNum(Nv - visitorAcceptCount);WriteMe("\n"); for(j=0;j<Nop;j++) { WriteMe("Money collected by Operator ");WriteNum(j+1);WriteMe(" is ");WriteNum(moneyReserve[j]); WriteMe("\n"); totalMoney += moneyReserve[j]; } WriteMe("Total money collected by all operators is ");WriteNum(totalMoney);WriteMe("\n"); WriteMe("It can be seen that number of visitors accepted is equal to the total money collected by all the operators.\n\n"); } if ((typeOfTest!=13)&&(typeOfTest!=14)&&(typeOfTest!=16)&&(typeOfTest!=17)) { Write("The president talks continuously with no interruption from any senator thread or visitor thread till the end of the \nmaximum time units per call. This is a clear indication that when the president talks, no other person is talking.\n",900,1); } Write("\nThe number in the operator column of each thread matches the operator number by whom it was verified (under the \nremarks column) which is again a clear indication that every senator or visitor or president talk exactly to one operator before \nmaking a call. In other words, the senator is verified by the operator to whom he/she submits his/her ID and the \nvisitor is verified by the operator to whom he/she paid money.\n\n\n",1000,1); break; } } else Yield(); }while (typeOfTest!=1); /* Delete all locks and condition variables */ DeleteLock(lockID1); DeleteLock(lockID2); DeleteLock(lockID3); DeleteLock(lockID4); DeleteLock(lockID5); DeleteLock(lockID6); DeleteLock(displayLock); DeleteCondition(condID1); DeleteCondition(condID2); DeleteCondition(condID3); DeleteCondition(condID4); /* delete[] operatorStatus, repositoryMoney, repositoryID, activate, printing, msg; */ Exit(0); }
void Senator() /* code block to perform senator operation */ { /* senator ID is randomly generated during forking and we assume that a senator with ID greater than 1000 has an authentiate ID */ int senatorNumberOfCalls=0; /* keep count of maximum number of calls made by a senator */ int operatorToUse; int checkCorrectOperatorS; int ID,i,j; int talkingTime; int condnToWait; int phoneToUse, gotPhone; int thisActivate; int callPresident; int randomWaitingTime; AcquireLock(lockID4); ID = NumSenator + 100*((RandomFunction(2)-1)?10:1); NumSenator++; ReleaseLock(lockID4); while(senatorNumberOfCalls<10) { AcquireLock(lockID1); condnToWait = TRUE; /* loop to check if president is waiting. If yes, then senator has to wait. Otherwise senator can obtain a phone */ do { if(presidentStatus==1) condnToWait = TRUE; else { for(i=0;i<NOOFPHONES;i++) { if(phoneStatus[i]==FREE) { phoneStatus[i]=BUSY; gotPhone = TRUE; phoneToUse = i; condnToWait = FALSE; break; } } } if(condnToWait==TRUE) WaitCV(condID2,lockID1); /* senator waits if the president is already waiting to make a call */ }while(condnToWait==TRUE); ReleaseLock(lockID1); /* Senator has got a Phone */ /* Need to get an operator now */ AcquireLock(lockID2); while(freeOperators==0) WaitCV(condID4,lockID2); /* senator has to wait if there are no free operators avaialble */ /* Some operator available. Let us find out who it is */ for(j=0;j<Nop;j++) { if(operatorStatus[j]==FREE) { operatorToUse = j; break; } } /* operator obtained*/ checkCorrectOperatorS = operatorToUse; /* check if the operator to whom the senator ID is submitted is the same as the one that permits/denies the senator to talk */ AcquireLock(indOpLock[operatorToUse]); operatorStatus[operatorToUse]=BUSY; freeOperators--; ReleaseLock(lockID2); authenticationMechanism[operatorToUse] = 2; /* 1 for President | 2 for Senators | 3 for Visitors */ repositoryID[operatorToUse] = ID; /* If operator is sleeping, wake up */ SignalCV(waitForCallerCV[operatorToUse],indOpLock[operatorToUse]); WaitCV(waitForOperVerCV[operatorToUse],indOpLock[operatorToUse]); thisActivate = activate[operatorToUse]; ReleaseLock(indOpLock[operatorToUse]); if(thisActivate==0) /* senator is denied access to phone beacuse of fake ID */ { j=0; talkingTime=0; /* printf("Senator%d \t UNAVAILABLE \t %d/%d units \t %d \t\t %d \t DENIED \t ID is less than 1000 - verified by operator %d \n",ID+1,j,talkingTime,operatorToUse+1,senatorNumberOfCalls+1,checkCorrectOperatorS+1); */ AcquireLock(displayLock); Write("Senator ",8,1); num = ID+1; /* itoa(printing,10,num); Write(printing,sizeof(printing),1);*/ WriteNum(num); Write(" \t UNAVAILABLE \t",100,1); num = j; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("/",1,1); num=talkingTime; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" units \t ",10,1); num=operatorToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t\t ",6,1); num=senatorNumberOfCalls+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t DENIED \t ID is less than 1000 - verified by operator ",100,1); num=checkCorrectOperatorS+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \n",3,1); ReleaseLock(displayLock); } else if(thisActivate==1) /* Senator has an authenticate ID. Operator verifies and senator is allowed to make a call */ { /* Now Talk */ talkingTime = RandomFunction(10); /* randomly generate the amount of time the senator will talk on the phone */ /* loop for the senator to talk on the phone for the randomly generated time period */ for (i=1;i<=talkingTime;i++){ /* printf("Senator%d \t %d \t\t %d/%d units \t %d \t\t %d \t ACCEPTED \t ID is greater than 1000 - verified by operator %d \n",ID+1,phoneToUse+1,i,talkingTime,operatorToUse+1,senatorNumberOfCalls+1,checkCorrectOperatorS+1); */ AcquireLock(displayLock); Write("Senator ",8,1); num = ID+1; /*itoa(printing,10,num); Write(printing,sizeof(printing),1);*/ WriteNum(num); Write(" \t",2,1); num = phoneToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("\t\t ",5,1); num = i; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write("/",1,1); num=talkingTime; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" units \t ",10,1); num=operatorToUse+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t\t ",6,1); num=senatorNumberOfCalls+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \t ACCEPTED \t ID is greater than 1000 - verified by operator ",100,1); num=checkCorrectOperatorS+1; itoa(printing,10,num); Write(printing,sizeof(printing),1); Write(" \n",3,1); ReleaseLock(displayLock); Yield(); } } senatorNumberOfCalls++; /* increment the number of calls made by the senator */ /* senator is done talking */ /* Set the phone status to be free */ AcquireLock(lockID1); phoneStatus[phoneToUse]=FREE; if(presidentStatus==0) /* president is not waiting to talk */ { if(CheckCondWaitQueue(condID2)) SignalCV(condID2,lockID1); /* wake up the next senator waiting to talk */ else SignalCV(condID3,lockID1); /* if no senator is waiting then wake up the next visitor waiting to talk */ } else /* president is waiting to talk, so senators and visitors will have to wait */ { callPresident = TRUE; for(i=0;i<NOOFPHONES;i++) if((i!=phoneToUse)&&(phoneStatus[i]==BUSY)) /* check if even a single phone is busy other than the phone just used by the senator which he/she sets to free */ { callPresident = FALSE; break; } if(callPresident==TRUE) SignalCV(condID1,lockID1); /* if all phones are free, then no one is talking currently and so, signal the president */ } ReleaseLock(lockID1); /* senator goes away */ /* senator waits for a random amount of time before coming back to make the next caa. Remember maximum number of calls allowed per senator is 10. */ randomWaitingTime = RandomFunction(3); for(j=0;j<randomWaitingTime;j++) Yield(); } Exit(0); }