/********************************************************************************
*																				*
*	ECES 338: Spring 2001 --- Recitation Wed: 8:30~Bill Coate					*
*																				*
*	Jacob Glenn (jpg2)															*
*																				*
*	Assignment #3 -- Semaphores	-- due: Thurs, Feb. 22, 2001					*
*																				*
********************************************************************************/


/* Problem #1:  Sleeping Barber													*
*********************************************************************************
* Customer:  																	*
*  	When customer enters store we have three possibilities with corresponding   *
*	actions:  (1) no empty chairs, customer leaves (2) some empty chairs,		*
*	customer sits down and waits to get haircut (3) no customers and barber		*
*	is sleeping, customer wakes barber and gets haircut.						*
*																				*
* Barber:																		*
*	Barber has two situations with corresponding actions:  (1)Customers waiting,*
*	cut hair in order that they came in (2) no customers, go to sleep.		    *
*********************************************************************************
* Assumptions:																	* 
*																				*
*	signal() function removes one process from the 'waiting' list and adds it   *
*	to the 'ready' list.  This means if there are processes waiting, no signals *
*	will be lost.																*
*																				*
*	wake_barber function will wake barber from sleep... continuing with code in *
*	barber function that appears after sleep call.								*
*********************************************************************************/	

/*constant*/
#define MAX_CUSTOMERS = 15            /*arbitrary number chosen for n assuming 
										n = number of waiting spots + 1 for 
										customer getting cut				*/

/*shared variable*/	
int num_cust = 0;		/*incremented by customer process, decremented by barber
						  process - keeps track of how many customers in shop*/

/* semaphore variable definitions */
mutex look_num_cust = 0;			/* Mutex allows only one process to access
									   num_cust variable at any given time */

semaphore cust_wait_barber = 0;		/*blocks all customers allowing them haircuts 
									  one at a time*/

semaphore cust_getting_cut = 0;		/*used by customer to tell when getting cut*/

semaphore cust_done = 0;			/*keep customer in barbers chair until done*/



/********************************************************************************
* This is the customer process.  The customer enters the shop and requests		*
* access to the num_cust shared variable.  When access granted customer checks	*
* to see if barber is asleep or their are empty chairs. If either situation 	*
* exists customer takes proper action.  otherwise customer leaves.				*
********************************************************************************/

Customer()
{
	wait(look_num_cust);		/*wait on permission to look at shared variable*/
	
	if(num_cust == 0)			/*if no customers in shop - wake barber */
	{	
		num_cust ++;
		signal(look_num_cust);			/*release num_cust*/
		wake_barber();					/*wake barber up*/
		signal(cust_getting_cut);		/*get cut*/
		wait(cust_done);				/*wait for signal that cut is done*/
		leave;							/*customer is done -- leave store*/
		
	}
	else if(num_cust < MAX_CUSTOMERS)	/*if number of customers is between 0 and 
										  n wait for barber to cut hair*/
	{
		num_cust++;
		signal(look_num_cust);			/*release num_cust*/
		wait(cust_wait_barber);			/*wait for barber*/
		signal(cust_getting_cut);		/*barber signaled customer -- get cut*/
		wait(cust_done);				/*wait for signal that cut is done */
		leave();						/*customer is done -- leave store*/
	}
	else if(num_cust == MAX_CUSTOMERS)
	{
		signal(look_num_cust);			/*release num_cust*/
		leave()							/*shop full -- go elsewhere*/
	}
}


/********************************************************************************
* This is the Barber process.  The barber will access the num_cust variable	via *
* the mutex.  If no customers barber will go to sleep.  If customers waiting,	*
* barber will signal cust_wait_barber to call next customer.  Then barber will  *
* wait for customer to be seated, cut hair, then signal that he is done.		*
********************************************************************************/

Barber()
{
	while(1)		/*barber loop*/
	{
		wait(look_num_cust);		/*wait on permission to look at shared variable*/
		if(num_cust == 0)
		{
			signal(look_num_cust);			/*release num_cust*/
			sleep();						/*enter sleep mode...when woken continue*/
			wait(cust_getting_cut);			/*waits for customer to sit in chair*/
			cut_hair();						/*cut customers hair*/
			wait(look_num_cust);			/*wait permission to shared variable*/
			signal(cut_done)				/*signal to customer they are done*/
			num_cust--;						/*decrement num_cust*/
			signal(look_num_cust);			/*release num_cust*/
		}
		else
		{
			signal(look_num_cust);			/*release num_cust*/
			signal(cust_wait_barber);		/*signal next waiting customer*/
			wait(cust_getting_cut);			/*waits for customer to sit in chair*/
			cut_hair();						/*cut customers hair*/
			wait(look_num_cust);			/*wait permission to shared variable*/
			signal(cut_done)				/*signal to customer they are done*/
			num_cust--;						/*decrement num_cust*/
			signal(look_num_cust);			/*release num_cust*/
		}
	}	/*end while*/
}

/*-------------------------------------------------------------------------------*/




/*Problem #2:  Multiple Sleeping Barbers										*
*********************************************************************************
*Customers:  3 types															*
*																				*
* Judy Customers - these customers will come in and check to see if the shop is *
* full, if it is then they will leave.  If its not full then they will check to *
* see if Judy is sleeping, if so they will wake her.  If not then they take a   *
* seat and wait to be cut. If tina offers to cut them they refuse and go to the *
* Judy line.																	*
*																				*
* Tina Customers - these customers will come in and check to see if the shop is *
* full, if it is then they will leave.  If its not full then they will check to *
* see if Tina is sleeping, if so they will wake her.  If not then they take a   *
* seat and wait to be cut. If Judy offers to cut them they refuse and go to the *
* Tina line.																	*
*																				*
* Don't Care Customers - these customers will come in and check to see if the   *
* shop is full, if it is then they will leave.  If its not full then they will	*
* check to see if Tina is sleeping, if so they will wake her. If not then they  *
* will check to see if judy is sleeping, if so they will wake her.  If neither  *
* barber is sleeping then the customer takes a seat and waits for service.		*
*																				*
* Judy - Judy checks to see if there are any customers in her line, if any she  *
* calls first one for haircut.  If none, she checks the waiting line, if		*
* customer accepts Judy she cuts their hair, else they go to Tina line and she  *
* asks the next customer.  If she runs out of customers she goes to sleep.		*
*																				*
* Tina - Tina checks to see if there are any customers in her line, if any she  *
* calls first one for haircut.  If none, she checks the waiting line, if		*
* customer accepts Tina she cuts their hair, else they go to Judy line and she  *
* asks the next customer.  If she runs out of customers she goes to sleep.		*
*********************************************************************************
* Assumptions:																	* 
*																				*
*	signal() function removes one process from the 'waiting' list and adds it   *
*	to the 'ready' list.  This means if there are processes waiting, no signals *
*	will be lost.																*
*																				*
*	wake functions will wake stated barber from sleep... continuing with code in*
*	barber function that appears after sleep call.								*
*********************************************************************************/	




/*constant*/
#define MAX_CUSTOMERS = 15            /*arbitrary number chosen for n assuming 
										n = number of waiting spots + 2 for 
										customers getting cut				*/

/*shared variable*/	
int tot_cust = 0;		/*incremented by customer process, decremented by barber
						  process - keeps track of how many customers in shop*/

int Judy_cust = 0;		/*number of customers in Judy's line -- incremented by
					      Tina, decremented by Judy*/

int Tina_cust = 0;		/*number of customers in Tina's line -- incremented by
					      Judy, decremented by Tina*/

/* semaphore variable definitions */
mutex look_shared_var = 0;			/* Mutex allows only one process to access
									   shared variables at any given time */

semaphore cust_wait_next = 0;		/* semaphore controls waiting line */

semaphore cust_wait_judy = 0;		/* blocks all customers in judy line allowing
									   them haircuts one at a time*/

semaphore cust_wait_tina = 0;		/*blocks all customers in tina line allowing
									   them haircuts one at a time*/

semaphore cust_judy_cut = 0;		/*used by customer to tell when judy cuts them*/

semaphore cust_tina_cut = 0;		/*used by customer to tell when tina cuts them*/

semaphore cust_judy_done = 0;		/*keep customer in judy's chair until done*/

semaphore cust_tina_done = 0;		/*keep customer in tina's chair until done*/



/********************************************************************************
* Judy customer process: customer enters the store and accesses shared variables*
* if judy is asleep, wakes judy and gets cut.  If shop is full customer goes	*
* elsewhere.  Otherwise customer enters general waiting line.  When either		*
* barber queues customer... customer enters judys line and waits for judy to cut*
* when cut is complete customer leaves store.									*
********************************************************************************/

Judy_customer()
{
	wait(look_shared_var);		/*wait on permission to look at shared variables*/
	
	if((tot_cust - tina_cust) == 0)		/*if judy is asleep - wake her */
	{	
		tot_cust ++;					/*increment tot_cust and judy_cust*/
		judy_cust ++;	
		signal(look_shared_var);		/*release shared variables*/
		wake_judy();					/*wake judy up*/
		signal(cust_judy_cut);			/*get cut*/
		wait(cust_judy_done);			/*wait for signal that cut is done*/
		leave;							/*customer is done -- leave store*/
		
	}
	else if((tot_cust < MAX_CUSTOMERS))	/*if number of customers is between 0 and 
										  n wait for barber to cut hair*/
	{
		tot_cust++;						/*increment tot_cust*/
		signal(look_shared_var);		/*release shared variables*/
		wait(cust_wait_next);			/*wait for barber*/
		wait(look_shared_var);			/*request access to shared variables*/
		judy_cust ++;					/*increment judy's customers */
		signal(look_shared_var)			/* release shared variables */
		wait(cust_wait_judy);			/*wait in judy's line regardless of which
										  barber queued customer.  Judy will
										  check her line before calling next cust.*/
		signal(cust_judy_cut);			/*Judy signaled customer -- get cut*/
		wait(cust_judy_done);			/*wait for signal that cut is done */
		leave();						/*customer is done -- leave store*/
	}
	else if(tot_cust == MAX_CUSTOMERS)
	{
		signal(look_shared_var);		/*release shared variablest*/
		leave()							/*shop full -- go elsewhere*/
	}
}


/********************************************************************************
* Tina customer process: customer enters the store and accesses shared variables*
* if tina is asleep, wakes tina and gets cut.  If shop is full customer goes	*
* elsewhere.  Otherwise customer enters general waiting line.  When either		*
* barber queues customer... customer enters tinas line and waits for tina to cut*
* when cut is complete customer leaves store.									*
********************************************************************************/			

Tina_customer()
{
	wait(look_shared_var);		/*wait on permission to look at shared variables*/
	
	if((tot_cust - judy_cust) == 0)		/*if tina is asleep - wake her */
	{	
		tot_cust ++;					/*increment tot_cust and tina_cust*/
		tina_cust ++;	
		signal(look_shared_var);		/*release shared variables*/
		wake_tina();					/*wake tina up*/
		signal(cust_tina_cut);			/*get cut*/
		wait(cust_tina_done);			/*wait for signal that cut is done*/
		leave;							/*customer is done -- leave store*/
		
	}
	else if((tot_cust < MAX_CUSTOMERS))	/*if number of customers is between 0 and 
										  n wait for barber to cut hair*/
	{
		tot_cust++;						/*increment tot_cust*/
		signal(look_shared_var);		/*release shared variables*/
		wait(cust_wait_next);			/*wait for barber*/
		wait(look_shared_var);			/*request access to shared variables*/
		tina_cust ++;					/*increment tina's customers */
		signal(look_shared_var)			/* release shared variables */
		wait(cust_wait_tina);			/*wait in tina's line regardless of which
										  barber queued customer.  Tina will
										  check her line before calling next cust.*/
		signal(cust_tina_cut);			/*Tina signaled customer -- get cut*/
		wait(cust_tina_done);			/*wait for signal that cut is done */
		leave();						/*customer is done -- leave store*/
	}
	else if(tot_cust == MAX_CUSTOMERS)
	{
		signal(look_shared_var);		/*release shared variablest*/
		leave()							/*shop full -- go elsewhere*/
	}
}



/********************************************************************************
* Careless customer process: customer enters the store and accesses shared		*
* variables. Checks both barbers to see if either is asleep.  If one of them is *
* sleeping then wake them and get cut.  If shop is full customer goes elsewhere.*
* Otherwise customer enters general waiting line.  When either	barber queues	*
* customer... customer requests shared variable access, when it is received		*
* customer checks each of the barbers lines.  One of them will be empty and then*
* the customer goes to that line,waits to be called, they get cut, and leave.   *
********************************************************************************/
Careless_customer()
{
	wait(look_shared_var);		/*wait on permission to look at shared variables*/

	if((tot_cust - tina_cust) == 0){		/*if judy is asleep - wake her */
		tot_cust ++;					/*increment tot_cust and judy_cust*/
		judy_cust ++;	
		signal(look_shared_var);		/*release shared variables*/
		wake_judy();					/*wake judy up*/
		signal(cust_judy_cut);			/*get cut*/
		wait(cust_judy_done);			/*wait for signal that cut is done*/
		leave;							/*customer is done -- leave store*/
	}
	else if((tot_cust - judy_cust) == 0){	/*if tina is asleep - wake her */
		tot_cust ++;					/*increment tot_cust and tina_cust*/
		tina_cust ++;	
		signal(look_shared_var);		/*release shared variables*/
		wake_tina();					/*wake tina up*/
		signal(cust_tina_cut);			/*get cut*/
		wait(cust_tina_done);			/*wait for signal that cut is done*/
		leave;							/*customer is done -- leave store*/
	}
	else if((tot_cust < MAX_CUSTOMERS))	/*if number of customers is between 0 and 
	{									  n wait for barber to cut hair*/
		tot_cust++;						/*increment tot_cust*/
		signal(look_shared_var);		/*release shared variables*/
		wait(cust_wait_next);			/*wait for barber to signal opening*/
		wait(look_shared_var);			/*when signaled request shared var access*/
		if(judy_cust == 0){
			judy_cust++;				/*increment judy line*/
			signal(look_shared_var);	/*release shared variables*/
			wait(cust_wait_judy);		/*wait for barber to call to chair*/ 
			signal(cust_judy_cut);		/*barber signaled -- get cut*/
			wait(cust_judy_done);		/*wait for signal that cut is done */
			leave();					/*customer is done -- leave store*/
		}
		else if(tina_cust == 0){
			tina_cust++;				/*increment judy line*/
			signal(look_shared_var);	/*release shared variables*/
			wait(cust_wait_tina);		/*wait for barber to call to chair*/ 
			signal(cust_tina_cut);		/*barber signaled -- get cut*/
			wait(cust_tina_done);		/*wait for signal that cut is done */
			leave();					/*customer is done -- leave store*/
		}
	}
	else if(tot_cust == MAX_CUSTOMERS){
		signal(look_shared_var);		/*release shared variablest*/
		leave()							/*shop full -- go elsewhere*/
	}
}


/********************************************************************************
* Judy Barber firsts checks if she has any customers in her line.  If not then  *
* she checks to see if there are customers in the general line, seeing none she *
* goes to sleep.  If there are customers in the general line she queues them.   *
* if the customer is a judy customer or a careless customer they go to the judy *
* line.  If it is a Tina customer they go to the tina line.  Then Judy checks   *
* her line to see if it is still empty, if it isn't she signals the customer in *
* her line.  Otherwise she checks to see if there are still people in the		*
* general line.  if none she sleeps.  if some then she queues the next process  *
* and repeats this until she sleeps or gets a customer.							*
********************************************************************************/
Judy_barber()
{
	while(1)		/*barber loop*/
	{
		wait(look_shared_var);	/*wait on permission to look at shared variables*/
		if(judy_cust == 0){
			if((tot_cust - tina_cust) == 0){
				signal(look_shared_var);	/*release shared_var*/
				sleep();					/*enter sleep mode...when woken continue*/
				wait(cust_judy_cut);		/*waits for customer to sit in chair*/
				cut_hair();					/*cut customers hair*/
				wait(look_shared_var);		/*wait permission to shared variables*/
				signal(cust_judy_done)		/*signal to customer they are done*/
				tot_cust--;					/*decrement tot_cust and judy_cust*/
				judy_cust--;	
				signal(look_shared_var);	/*release shared_var*/
			}
			else{
				int i=0;
				while(i!=1)
				{
					signal(look_shared_var);	/*release shared_var*/
					signal(cust_wait_next);		/*signal next waiting customer*/
					wait(look_shared_var);		/*wait for shared variables*/
					if(judy_cust >0)			/*check to see if customer judy 
					{							  gets customer*/
						signal(look_shared_var);	/*release shared variables*/
						signal(cust_wait_judy);		/*call customer to chair*/
						wait(cust_judy_cut);		/*wait till customer sits*/
						cut_hair();					/*cut customers hair*/
						wait(look_shared_var);		/*wait permission to shared variable*/
						signal(cut_judy_done);		/*signal to customer they are done*/
						judy_cust--;				/*decrement judy_cust and tot_cust*/
						tot_cust--;	
						signal(look_shared_var);	/*release shared variables*/
						i=1;						/*break out of while loop*/
					}
					if((tot_cust - tina_cust) == 0)	/*if no more customers*/
					{
						signal(look_shared_var);	/*release shared variables*/
						i=1;						/*break out of while loop*/
					}
				} 
			}
		}
	}	/*end while*/
}



/********************************************************************************
* Tina Barber firsts checks if she has any customers in her line.  If not then  *
* she checks to see if there are customers in the general line, seeing none she *
* goes to sleep.  If there are customers in the general line she queues them.   *
* if the customer is a tina customer or a careless customer they go to the tina *
* line.  If it is a Judy customer they go to the judy line.  Then Tina checks   *
* her line to see if it is still empty, if it isn't she signals the customer in *
* her line.  Otherwise she checks to see if there are still people in the		*
* general line.  if none she sleeps.  if some then she queues the next process  *
* and repeats this until she sleeps or gets a customer.							*
********************************************************************************/
Tina_barber()
{
	while(1)		/*barber loop*/
	{
		wait(look_shared_var);	/*wait on permission to look at shared variables*/
		if(tina_cust == 0){
			if((tot_cust - judy_cust) == 0){
				signal(look_shared_var);	/*release shared_var*/
				sleep();					/*enter sleep mode...when woken continue*/
				wait(cust_tina_cut);		/*waits for customer to sit in chair*/
				cut_hair();					/*cut customers hair*/
				wait(look_shared_var);		/*wait permission to shared variables*/
				signal(cust_tina_done)		/*signal to customer they are done*/
				tot_cust--;					/*decrement tot_cust and tina_cust*/
				tina_cust--;	
				signal(look_shared_var);	/*release shared_var*/
			}
			else{
				int i=0;
				signal(look_shared_var);	/*release shared_var*/
				while(i != 1)
				{
					signal(look_shared_var);	/*release shared_var*/
					signal(cust_wait_next);		/*signal next waiting customer*/
					wait(look_shared_var);		/*wait for shared variables*/
					if(tina_cust >0)			/*check to see if customer tina 
					{							  gets customer*/
						signal(look_shared_var);	/*release shared variables*/
						signal(cust_wait_tina);		/*call customer to chair*/
						wait(cust_tina_cut);		/*wait till customer sits*/
						cut_hair();					/*cut customers hair*/
						wait(look_shared_var);		/*wait permission to shared variable*/
						signal(cut_tina_done);		/*signal to customer they are done*/
						tina_cust--;				/*decrement tina_cust and tot_cust*/
						tot_cust--;	
						signal(look_shared_var);	/*release shared variables*/
						i=1;						/*break out of while loop*/
					}
					if((tot_cust - judy_cust) == 0)	/*if no more customers*/
					{
						signal(look_shared_var);	/*release shared variables*/
						i=1;						/*break out of while loop*/
					}
				} 
			}
		}
	}	/*end while*/
}	

/*-------------------------------------------------------------------------------*/




/* Problem #3:  Savings Account Problem											*
*********************************************************************************
* My solution to the savings account problem consists of 3 process types.  A    *
* withdraw process, a deposit process, and a teller process.					*
*																				*
* The Teller will run continuously handling transactions in a fifo order.  If   *
* a withdraw process requests an amount greater than the account balance the    *
* process will be blocked until there are sufficient funds available.  Withdraw *
* candidates will wait in fifo order.											*
*																				*
* The withdraw process will consist of a request for a given amount of money.   *
* If the amount is less than the account balance it will be received, otherwise *
* the process will be blocked and wait until there are sufficient funds.		*
*																				*
* The deposit process will submit the deposit amount to the teller where it will*
* be added to the account balance and the deposit process will end.				*
*********************************************************************************
* Assumptions:																	*
*																				*
*	signal() function removes one process from the 'waiting' list and adds it   *
*	to the 'ready' list.  This means if there are processes waiting, no signals *
*	will be lost.																*
*																				*
*	assume that all withdraw processes stacked on wait_withdraw come off in FIFO*
********************************************************************************/


/*shared variables*/	
double account_bal = 0;			/*running balance, added to by depositors, 
								  subtracted from by withdrawees.*/

double withdraw = 0;			/*withdraw request amount.*/

double deposit = 0;				/*deposit submission amount*/

int dep_cust;

int with_cust;


/*semaphore variable definitions*/
mutex access_account = 0;				/*mutex allows only one process to access 
										  shared variables at a time*/

semaphore wait_transaction = 0;			/*only allows one transaction at a time*/

semaphore wait_withdraw = 0;			/*blocks all withdrawees allowing them to 
										  withdraw one at a time (FIFO) order */

semaphore ready_withdraw = 0;			/* blocks withdrawee at front of line until
										   proper funds are available */

semaphore transaction_complete = 0;		/*used by teller to notify deposit process 
										  of completion*/


/********************************************************************************
* Teller process is fairly simple it runs continuously, but doesn't to anything *
* until it is told to process a transaction.  Then it checks the type, withdraw *
* or deposit.  After every transaction completes the Teller checks if there are *
* waiting withdraws.  If so, it signals them to try to withdraw.				*
********************************************************************************/
Teller()
{
	while(1)
	{
		wait(wait_transaction);
		wait(access_account);			/*wait for shared variable access*/
		if(trans_type == 'd')			/* if deposit*/
		{
			account_bal = account_bal + deposit;	/*add money to account*/
			trans_type = 'n';						/*reset type variable*/
			deposit = 0;							/*reset deposit variable*/
			signal(transaction_complete);
			if(num_waiting > 0)
			{
				signal(access_account);				/*release shared variables*/
				signal(ready_withdraw);				/*signal waiting withdrawee(s)*/
			}
			else {signal(access_account)}			/*release shared variables*/
		}
		else if(trans_type == 'w')					/*if withdraw*/
		{		
			account_bal = account_bal - withdraw;	/*subtract money from account*/
			trans_type = 'n';						/*reset type variable*/
			withdraw = 0;							/*reset withdraw variable*/
			signal(transaction_complete);
			if(num_waiting > 0)
			{
				signal(access_account);				/*release shared variables*/
				signal(ready_withdraw);				/*signal waiting withdrawee(s)*/
			}
			else {signal(access_account)}			/*release shared variables*/
			
		}
	}
}

										  
/********************************************************************************
* Deposit process is very simple.  Accesses acount, sets deposit amount and     *
* transaction type, releases shared variables, calls transaction then leaves.	*
********************************************************************************/
Deposit()
{	
	wait(access_account);						/*wait for shared variable access*/
	deposit = money;			/*set shared variable deposit = money to be deposited*/
	trans_type = 'd';							/*set transaction type*/
	signal(access_account);						/*release shared variables*/
	signal(wait_transaction);					/*call the transaction*/
	wait(transaction_complete);					/* wait for completion*/
	leave();									
}



/********************************************************************************
* The withdraw process checks if anybody is waiting.  If they are then the      *
* withdraw is just added to the waiting list.  It is assumed that they are added*
* and removed in FIFO order.  If there is no waiting withdraws the process looks*
* to see if there is enough money in the account to make the withdraw.  If so it*
* does, otherwise it enqueues the withdraw in the waiting list.					*
********************************************************************************/
Withdraw()
{
	wait(access_account);			/*wait for shared variable access*/
	if(num_waiting > 0)				/* if peops are waiting to withdraw*/
	{
		num_waiting++;							/*increment num_waitin*/
		signal(access_account);					/*release shared variables*/
		wait(wait_withdraw);					/*get in line*/
	}
	if(num_waiting ==0)
	{						/*if nobody waiting*/
		withdraw = money;						/*set withdraw amount*/
		if(withdraw <= account_bal)				/*if amount not to big*/
		{
			trans_type = 'w';					/*set type variable*/
			signal(access_account);				/*release shared variables*/
			signal(wait_transaction);			/*signal transaction*/
			wait(transaction_complete);			/*wait for procedure to finish*/
			leave();
		}
		else{
			num_waiting++;						/*increment num_waiting*/
			signal(access_account);				/*release shared variables*/
			wait(ready_withdraw);				/* wait for deposits*/
			wait(access_account);				/*wait for shared variable access*/
			for(i=num_waiting; i==0; i--)	/*loop to empty out waiting withdraws*/
			{
				if(withdraw <= account_bal)     /*is there enough money?*/
				{
					trans_type = 'w';			/*set type*/
					signal(access_account);		/*release shared variables*/
					signal(wait_transaction);	/*signal to start withdraw*/
					wait(transaction_complete);	/*wait for money*/
					leave();					/* leave */
					num_waiting--;				/* decrement num_waiting*/
					signal(wait_withdraw);		/*signal new first in line*/
					wait(access_account);		/*wait for shared variable access*/
					withdraw = money;			/* set withdraw amount*/
				}
				else{
					while(withdraw > account_bal)	/* while transaction can't 
					{									complete... loop*/
						signal(access_account);	/*release shared variables*/
						wait(ready_withdraw);	/*wait for signal to withdraw*/
						wait(access_account);	/*wait for shared variable access*/
					}
				}
			}
		}
	}
}
				




	
										  
