Exemplo n.º 1
0
void test()
{
	for (loop2=0; loop2<100; loop2++) 
		Yield();/*This is to ensure that signal is sent only after Wait request */
			
	SignalCV(i,j2+1);/*This prints error statement*/
	SignalCV(i,j);
	
	Exit(0);
}
Exemplo n.º 2
0
int main()
{
	mv = CreateMV("mv1");
	mv2 = CreateMV("mv2");
	
	lck = CreateLock("lock1");
	lck2 = CreateLock("lock2");
	
	cv = CreateCV("cv1");

	for (loop =0; loop<500; loop++) 
	{
		AcquireLock(lck);
		SignalCV(lck,cv);

		AcquireLock(lck2);
		ret = GetMV(mv2);
		ReleaseLock(lck2);
		
		if (ret != 21)/*This will wait only if the other one isnt finished*/
			WaitCV(lck,cv);

		mvEdit = GetMV(mv);
		Print("Client 2: Monitor variable was read to be %d..",mvEdit,-1,-1);

		mvEdit+=7;
	
		
		ret=SetMV(mv,mvEdit);
		mvEdit = GetMV(lck);
	
		Print("Client 2: Monitor variable was set to be %d..",mvEdit,-1,-1);
		
		ReleaseLock(lck);	

		Print("Client 2: %d Iterations done..",loop,-1,-1);
	}
	
	AcquireLock(lck2);
	ret = SetMV(mv2,21);
	
	/*this monitor variable is set to 21 here to signify 
	*one of the processes is over and the other will not
	*wait for it to signal it
	*/
	ReleaseLock(lck2);
	
	AcquireLock(lck);
	SignalCV(lck,cv);
	ReleaseLock(lck);

}
Exemplo n.º 3
0
int main()
{

	lock = CreateLock("lock");
	lock2 = CreateLock("lock2");

	cv1 = CreateCV("cv1");
	cv2=CreateCV("cv2");

	AcquireLock("lock");
	
	SignalCV("lock","cv1");
	
	Print("Client 2 is going to wait...",-1,-1,-1);
	
	WaitCV("lock","cv2");

	for (i=0; i<9; i++)
		Print("\n",-1,-1,-1);
	
	Print("Client 2 is now out of wait...",-1,-1,-1);

	ReleaseLock("lock");

	Exit(0);

}
Exemplo n.º 4
0
void main()
{
    lck = CreateLock("lock1");
    cv = CreateCV("cv1");
    Print("Client 2: Created Lock is %d and CV is %d\n", lck, cv, -1);

    AcquireLock("lock1");


    Print("Client 2: About to signal Client 1\n", -1, -1, -1);

    SignalCV("lock1","cv1");

    Print("Client 2: About to go on wait...\n", -1, -1, -1);


    WaitCV("lock1","cv1");
    for (i=0; i<10; i++)
        Print("\n",-1,-1,-1);

    Print("Client 2: Now out of wait...\n", -1, -1, -1);
    ReleaseLock("lock1");

    Exit(0);
}
Exemplo n.º 5
0
void main()
{
	lck = CreateLock("lock1");
	cv = CreateCV("cv1");
	Print("\nDistCVTest1_1: Created Lock is %d and CV is %d\n", lck, cv, -1);
	

	AcquireLock("lock1");

	
	Print("Client 1: About to go on wait...\n", -1, -1, -1);

	
	WaitCV("lock1","cv1");

	Print("Client 1: Now out of wait...\n", -1, -1, -1);

	for (num=0; num<10; num++)
		Print("\n",-1,-1,-1);
	
	Print("Client 1: About to signal Client 2\n", -1, -1, -1);
	
	SignalCV("lock1","cv1");

	ReleaseLock("lock1");
	AcquireLock("lock1");


	ReleaseLock("lock1");
	
	Exit(0);
	
	
}
Exemplo n.º 6
0
int main()
{

	lock = CreateLock("lock");
	cv2 = CreateCV("cv2");
	
	AcquireLock("lock");

	SignalCV("lock","cv2");

	Print("Client 3 will now destroy CV...",-1,-1,-1);
	
	DestroyCV("cv2");
	
	for (i=0; i<9; i++)
		Print("\n",-1,-1,-1);


	ReleaseLock("lock");

	AcquireLock("lock");	
	
	WaitCV("lock","cv2");
	
	Print("Client 3: This prints as CV has been destroyed...",-1,-1,-1);

	ReleaseLock("lock");

	Exit(0);

}
Exemplo n.º 7
0
int main()
{
	int lock;
	int cv;
	
	cv= CreateCV("lock",4);
	lock= CreateLock("cv",2);
	
	AcquireLock(lock);
	SignalCV(lock,cv);
	ReleaseLock(lock);
	
	Exit(0);
    
}
Exemplo n.º 8
0
/*
 * we set customer states here 
 * and call the associated wait 
 * and signal on CV as required
 * */
void changeCustState(int debugId, int custId, int grpId, int state)
{
	int currentState;
	AcquireLock(custLock[grpId][custId]);
	currentState = getMv(&mtCb.custGrp[grpId].cust[custId].state);
	setMv(&mtCb.custGrp[grpId].cust[custId].state, state);

       /*reactions to different states*/
	if(
			state == WAIT || 
			state == WAIT_FOR_TC || 
			state == WAIT_FOR_TICKET_BUYER_FROM_TC ||
			state == WAIT_ENGAGED_WITH_TC || 
			state == WAIT_FOR_CC || 
			state == WAIT_FOR_TICKET_BUYER_FROM_CC ||
			state == WAIT_ENGAGED_WITH_CC || 
			state == WAIT_FOR_CC_BEING_FIRST || 
			state == WAIT_FOR_TC_BEING_FIRST || 
			state == WAIT_IN_LOBBY_AS_TICKET_NOT_TAKEN_BY_TT || 
			state == WAIT_FOR_TICKET_BUYER_FROM_TT ||
			state == WAIT_FOR_TICKET_BUYER_TILL_SEATS_TAKEN || 
			state == WAIT_AS_TICKET_BUYER_HEADING_FOR_BATHROOM || 
			state == WAIT_AS_OVER || 
			state == SEAT_TAKEN 
	  )
	{
		WaitCV(custLock[grpId][custId],custCondVar[grpId][custId]);/*if the state is set to be some sort of WAIT we call wait on CV*/
	}
	else if(
			state == SIGNAL_BY_TC || 
			state == SIGNAL || 
			state == SIGNAL_ENGAGED_WITH_TC ||
			state == SIGNAL_BY_CC || 
			state == SIGNAL_ENGAGED_WITH_CC || 
			state == READY_TO_LEAVE_MOVIE_ROOM || 
			state == NEW_MOVIE_STARTING_TICKET_MAY_BE_TAKEN || 
			state == SIGNAL_TO_CHANGE_TO_ANY_OTHER_QUEUE 
	       )
	{
		SignalCV(custLock[grpId][custId],custCondVar[grpId][custId]);/*we stop waiting and signal customer to wake up*/
	}
	ReleaseLock(custLock[grpId][custId]); 
}
Exemplo n.º 9
0
void main()
{
	lck = CreateLock("lock1");
	cv = CreateCV("cv1");
	Yield();
	Print("\n Client 5: Created Lock is %d and cv is %d\n", lck, cv, -1);
	
	AcquireLock("lock1");
	
	Print("\n Client 5: This will now signal and awake the Client 4\n", -1, -1, -1);
	SignalCV("lock1","cv1");
	
	Print("Client 5: This will now exit..\n", -1, -1, -1);

	ReleaseLock("lock1");

	Exit(0);

	
}
Exemplo n.º 10
0
int main()
{

	cv = CreateCV("cv_1");
	lock = CreateLock("lock1");

	AcquireLock("lock1");
	
	SignalCV("lock1","cv_1");
	ReleaseLock("lock1");

	AcquireLock("lock");

	WaitCV("lock1","cv_1");
	Print("\nClient 2 is out of wait..\n",-1,-1,-1);
	ReleaseLock("lock1");
	
	Exit(0);

}
Exemplo n.º 11
0
/*
 * removing a customer from a queue
 * */
Cust* queueRemoveCust(int debugId, mtQueue *queue)
{
	/*We acquire a lock on the queue and then modify it. A customer is removed and is ready to be served.*/
	Cust *cust;
	int currCustGrp;
	int currCust;

	AcquireLock(queueLock[queue->queueType][queue->queueId]);
	
	SignalCV(queueLock[queue->queueType][queue->queueId],queueCondVar[queue->queueType][queue->queueId]);
	ReleaseLock(queueLock[queue->queueType][queue->queueId]);
	if( queue->queueType == TC_QUEUE )
	{
		while(getMv(&mtCb.tc[queue->queueId].msgFromCust) != CUST_REMOVED)
		{ 
			YIELD();
		}
		
		setMv(&mtCb.tc[queue->queueId].msgFromCust, INVALID_MSG);
		currCustGrp = getMv(&mtCb.tc[queue->queueId].currentCustGrpId);
		currCust = getMv(&mtCb.tc[queue->queueId].currentCustId);
		cust = &mtCb.custGrp[currCustGrp].cust[currCust];
	}


	AcquireLock(queueLock[queue->queueType][queue->queueId]);

	if(currCust >= 0 || currCustGrp >= 0)
	{/*if there were customers in line, we take them out and reduce count of number of customers in queue. After that, we release lock and return pointer to customer*/
		getMv(&queue->numCust);
		setMv(&queue->numCust, queue->numCust.value - 1);
		ReleaseLock(queueLock[queue->queueType][queue->queueId]);
		return cust;
	}
	else 
	{/*if there were no customers in queue we simply release lock and return a Null signifying no customers*/

		ReleaseLock(queueLock[queue->queueType][queue->queueId]);
		return NULL;
	}
}
Exemplo n.º 12
0
int main()
{

	cv = CreateCV("cv_1");
	lock = CreateLock("lock1");


	AcquireLock("lock1");
	SignalCV("lock1","cv_1");
	ReleaseLock("lock1");

	DestroyCV("cv_1");
	
	AcquireLock("lock1");
	
	WaitCV("lock1","cv_1");
	Print("This prints as CV has been destroyed so no wait occurs..",-1,-1,-1);
	ReleaseLock("lock1");

	Exit(0);
}
Exemplo n.º 13
0
Arquivo: senate.c Projeto: kprav/CPP
void Visitor() /* code block to perform visitor operation */
{
  int condnToWait,who,i,j;
  int talkingTime;
  int checkCorrectOperatorV;
  int phoneToUse,gotPhone;
  int thisActivate;
  int operatorToUse;
  int callPresident;
  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(presidentStatus == 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(phoneStatus[i]==FREE)
		{
		  phoneToUse = i;
		  phoneStatus[phoneToUse]=BUSY;
		  condnToWait = FALSE;
		  break;
		}
	    }	  
	}
      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(freeOperators==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(operatorStatus[j]==FREE)
	{
	  operatorToUse = j;
	  break;
	}
    }
  /* 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]);
  activate[operatorToUse]=0;
  operatorStatus[operatorToUse]=BUSY;
  freeOperators--;
  ReleaseLock(lockID2);
  authenticationMechanism[operatorToUse] = 3; /* 1 for President | 2 for Senators | 3 for Visitors */
  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]);
  WaitCV(waitForOperVerCV[operatorToUse],indOpLock[operatorToUse]);
  thisActivate=0;
  thisActivate=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);
  phoneStatus[phoneToUse]=FREE;
  if(presidentStatus==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)&&(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;
	  }
      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);	
  Exit(0);
}
Exemplo n.º 14
0
int main(){
	
	int departmentOfChoice,goodsFlag=-1,mySalesID,i,goodsIndex,mvValue1,mvValue2,mvValue3;
	
	initialize();
	
	/* Retrieving index */
	AcquireLock(goodsIndexLock);
	Print("\nInside goodsloader"); 
	goodsIndex=GetMV(globalGoodsIndex);
	SetMV(globalGoodsIndex,goodsIndex+1);
	ReleaseLock(goodsIndexLock);
	
	while(1){
		AcquireLock(salesGoodsLock);
		AcquireLock(goodsloaderLock[goodsIndex]);
		if(GetMV(goodsStatus[goodsIndex])==2){ /* Salesman signalled to restock */
			ReleaseLock(salesGoodsLock);
			SignalCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
			WaitCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
			AcquireLock(storeroomLock);
			mvValue1 = GetMV(itemToBeRestocked_FromSalesmanToGoodsloader[goodsIndex]);
			AcquireLock(itemLock[mvValue1]);
			departmentOfChoice=mvValue1/10;
			mvValue2=GetMV(salesGoodsId[goodsIndex]);
			if(departmentOfChoice==0)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [0] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==1)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [1] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==2)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [2] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else if(departmentOfChoice==3)
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [3] to restock item%d",goodsIndex,mvValue2,mvValue1);
			else
				Print3("\nGoodsloader [%d] is informed by DepartmentSalesman [%d] of department [4] to restock item%d",goodsIndex,mvValue2,mvValue1);
			
			Print2("\nGoodsloader [%d] is in the StockRoom and got item%d",goodsIndex,mvValue1);
			
			
			SetMV(quantityOnShelf[mvValue1],10);
			Print3("\nGoodsloader [%d] restocked item%d in Department [%d]",goodsIndex,mvValue1,departmentOfChoice);
			Print1("\nGoodsloader [%d] leaves StockRoom",goodsIndex);
			ReleaseLock(storeroomLock);
			ReleaseLock(itemLock[mvValue1]);
			AcquireLock(waitingLock[departmentOfChoice]);
			mvValue3 = GetMV(goodsWaitingLineCount[departmentOfChoice]);
			SetMV(goodsWaitingLineCount[departmentOfChoice],++mvValue3);
			mvValue2=GetMV(salesGoodsId[goodsIndex]);
			AcquireLock(salesmanLock[departmentOfChoice][mvValue2]);
			SignalCV(salesmanLock[departmentOfChoice][mvValue2],salesWaitingCV[departmentOfChoice][mvValue2]);
			ReleaseLock(salesmanLock[departmentOfChoice][mvValue2]);
			WaitCV(waitingLock[departmentOfChoice],goodsWaitingCV[departmentOfChoice]);
			
			mySalesID=-1;
			for(i=0;i<salesmanCount;i++){
				if(GetMV(salesStatus[departmentOfChoice][i]) == 5){
					mySalesID=i;
					SetMV(salesStatus[departmentOfChoice][mySalesID],1);
					break;
				}
			}
			AcquireLock(salesmanLock[departmentOfChoice][mySalesID]);
			ReleaseLock(waitingLock[departmentOfChoice]);
			mvValue1 = GetMV(itemToBeRestocked_FromSalesmanToGoodsloader[goodsIndex]);
			SetMV(restockedItem[departmentOfChoice][mySalesID],mvValue1);
			SetMV(restockingGoodsloader[departmentOfChoice][mySalesID],goodsIndex);
			SignalCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			WaitCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			SignalCV(salesmanLock[departmentOfChoice][mySalesID],salesWaitingCV[departmentOfChoice][mySalesID]);
			ReleaseLock(salesmanLock[departmentOfChoice][mySalesID]);
		}
		else{
			ReleaseLock(salesGoodsLock);
		}
		AcquireLock(trolleyEndLock);		
		if(GetMV(trolleyEndCount)>0){ /* If trolley is present at the exit, replace the trolley */
			mvValue1 = GetMV(trolleyEndCount);
			SetMV(trolleyEndCount,--mvValue1);
			ReleaseLock(trolleyEndLock);
			AcquireLock(trolleyFirstLock);
			mvValue1 = GetMV(trolleyFirstCount);
			SetMV(trolleyFirstCount,++mvValue1);
			Print1("\nGoodsloader [%d] replaced the trolley",goodsIndex);
			mvValue1 = GetMV(trolleyWaitingCount);
			if(mvValue1>0){
				SetMV(trolleyWaitingCount,--mvValue1);
				SignalCV(trolleyFirstLock,trolleyWaitingCV);
			}
			ReleaseLock(trolleyFirstLock);
		}
		else{
			ReleaseLock(trolleyEndLock);
		}
		AcquireLock(salesGoodsLock);	
		SetMV(goodsStatus[goodsIndex],0);
		ReleaseLock(salesGoodsLock);
		
		Print1("\nGoodsloader [%d] is waiting for orders to restock",goodsIndex);
		WaitCV(goodsloaderLock[goodsIndex],goodsloaderCV[goodsIndex]);
		ReleaseLock(goodsloaderLock[goodsIndex]);
	}
	Exit(0);
}
Exemplo n.º 15
0
/*
 * enclosing call to Signal function of CV of ticket clerk on lock
 * */
void signalToTcLock(int debugId, int tcId1)
{
	SignalCV(tcLock[tcId1],tcCondVar[tcId1]); 
}
Exemplo n.º 16
0
Arquivo: senate.c Projeto: kprav/CPP
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);
}
Exemplo n.º 17
0
Arquivo: senate.c Projeto: kprav/CPP
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);
}
Exemplo n.º 18
0
/*
 * Macro is signalling concession clerk on CV with lock
 * */
void signalToCcLock(int debugId, int ccId1)
{
	SignalCV(ccLock[ccId1],ccCondVar[ccId1]); 
}
Exemplo n.º 19
0
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);
}
Exemplo n.º 20
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);
}
Exemplo n.º 21
0
Arquivo: senate.c Projeto: kprav/CPP
void President() /* code block to perform president operation */
{
  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;
  while(presidentNumberOfCalls<5)
    {
      condnToWait = FALSE;
      phoneToUse = 0;
      AcquireLock(lockID1);
      presidentStatus = 1;
      /* loop for the president to keep waiting even if a single phone is busy */
      do
	  {
		  for(i=0;i<NOOFPHONES;i++)
		    {
		     if(phoneStatus[i]==BUSY)
				{
			  condnToWait = TRUE;
			  break;
				}
		    }
		if(i==NOOFPHONES)
		   condnToWait=FALSE;
		  if(condnToWait==TRUE)
		  WaitCV(condID1,lockID1);
	}while(condnToWait==TRUE);
      /* all phones are free now */
      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(freeOperators==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(operatorStatus[j]==FREE)
	    {
	      operatorToUse = j;
	      break;
	    }
	 }
      /* 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]);
      activate[operatorToUse]=0;
      operatorStatus[operatorToUse]=BUSY;
      freeOperators--;
	  ReleaseLock(lockID2);
      authenticationMechanism[operatorToUse] = 1; /* 1 for President | 2 for Senators | 3 for Visitors */
	 
      /* If operator is sleeping, wake up */

      SignalCV(waitForCallerCV[operatorToUse],indOpLock[operatorToUse]);
      WaitCV(waitForOperVerCV[operatorToUse],indOpLock[operatorToUse]); 
      ReleaseLock(indOpLock[operatorToUse]);  
      if(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(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);
	  phoneStatus[phoneToUse]=FREE;
	  presidentStatus = 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(7);
      for(j=0;j<waitingTime;j++)
	{
	  Yield();
	}
      presidentNumberOfCalls++; /* increment the number of calls made by the president */
    }
  Exit(0);
}