// Flag to stop the barber thread when all customers
// have been serviced.
int allDone = 0;
+int DEBUG = 0;
static void randwait(int secs)
{
// Leave for the shop and take some random amount of
// time to arrive.
- printf("Customer %d leaving for barber shop.\n", num);
+ if (DEBUG)
+ printf("Customer %d leaving for barber shop.\n", num);
randwait(5);
- printf("Customer %d arrived at barber shop.\n", num);
+ if (DEBUG)
+ printf("Customer %d arrived at barber shop.\n", num);
// Wait for space to open up in the waiting room...
sem_wait(&waitingRoom);
- printf("Customer %d entering waiting room.\n", num);
+ if (DEBUG)
+ printf("Customer %d entering waiting room.\n", num);
// Wait for the barber chair to become free.
sem_wait(&barberChair);
sem_post(&waitingRoom);
// Wake up the barber...
- printf("Customer %d waking the barber.\n", num);
+ if (DEBUG)
+ printf("Customer %d waking the barber.\n", num);
sem_post(&barberPillow);
// Wait for the barber to finish cutting your hair.
// Give up the chair.
sem_post(&barberChair);
- printf("Customer %d leaving barber shop.\n", num);
+ if (DEBUG)
+ printf("Customer %d leaving barber shop.\n", num);
return NULL;
}
while (!allDone) {
// Sleep until someone arrives and wakes you..
- printf("The barber is sleeping\n");
+ if (DEBUG)
+ printf("The barber is sleeping\n");
sem_wait(&barberPillow);
// Skip this stuff at the end...
// Take a random amount of time to cut the
// customer's hair.
- printf("The barber is cutting hair\n");
+ if (DEBUG)
+ printf("The barber is cutting hair\n");
randwait(3);
- printf("The barber has finished cutting hair.\n");
+ if (DEBUG)
+ printf("The barber has finished cutting hair.\n");
// Release the customer when done cutting...
sem_post(&seatBelt);
} else {
- printf("The barber is going home for the day.\n");
+ if (DEBUG)
+ printf("The barber is going home for the day.\n");
}
}
return NULL;
// Check to make sure there are the right number of
// command line arguments.
- if (argc != 4) {
- printf("Use: SleepBarber <Num Customers> <Num Chairs> <rand seed>\n");
+ if (argc != 5) {
+ printf("Use: SleepBarber <Num Customers> <Num Chairs> <rand seed> <DEBUG>\n");
exit(-1);
}
numCustomers = atoi(argv[1]);
numChairs = atoi(argv[2]);
RandSeed = atol(argv[3]);
+ DEBUG = atoi(argv[4]);
// Make sure the number of threads is less than the number of
// customers we can support.