void DeletePipe(Pipe_t *p) { DeleteCondition(p->cond_get); DeleteCondition(p->cond_put); DeleteMonitor(p->monitor); Free(p->buf); Free(p); }
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); }