Extra
Credit
#include
<iostream.h>
#define
CUTS 30
#define
HAIRCUTS 10
#define
MAXWAIT 5
// full
of timing problems
class
BarberShop
{ int chair, bell, waitCount, cutting; // flags
void getCust();
void cutHair();
void waitTilCalled();
void waitTilCut();
public:
BarberShop(){chair=waitCount=cutting=bell=0;}
void barber();
void customer();
};
void
BarberShop::barber()
{ int
headCount = 0; // # of customers today
while(headCount < CUTS)
{ if (waitCount) // customers waiting
{ getCust(); // bring customer in from lobby
waitCount--; // ***
chair++; // seat
customer in chair
cutHair();
headCount++;
chair--; // customer leaves
}
else // no customers, take a nap
{ while(!bell); // sleep - busy wait
bell = 0; // turn off bell
}
}
}
void
BarberShop::customer()
{ int
haircuts = 0; // # of haircuts received
while(haircuts < HAIRCUTS)
{ if (waitCount < MAXWAIT) // enter lobby
{
if (waitCount == 0 && chair == 0) // nobody there
{
bell = 1; // wake
barber
waitCount ++; // ***
waitTilCalled(); // sit in lobby til called
waitTilCut(); // sit in chair while haircut
haircuts++;
}
// leave shop
}
// grow hair
}
}
int main(int
argc, char **argv)
{ Barbershop bs;
// fork off 1 barber & N customers
return 0;
}