1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
sem_t customerReady;
sem_t barberReady;
sem_t accessWRSeats;
unsigned int numberOfAvailableSeats = 0;
unsigned int numberOfCustomers = 0;
int HOURS = 1;
int amountTime = 0;
void *barber(void*){
while(amountTime < HOURS*60){
sem_wait(&customerReady);//Check to see if there are any customers ready and if not, sleep
sem_wait(&accessWRSeats); //Wake up and and try to access the # of available seats and if not, sleep
//Enter Critical Section
//If there are any customers in the waiting room
//If there aren't any customers in the waiting room
cout << "\nBarber: number of customers: " << numberOfCustomers;
numberOfCustomers--;
numberOfAvailableSeats++;//Take a customer from the wait room to the barber chair
//Cut hair here
sem_post(&accessWRSeats);//Release lock on available seats; chairs are unlocked
sem_post(&barberReady);//Finish cutting hair and is ready to cut again
amountTime++;
}
cout << "EXITING BARBER!" << endl;
pthread_exit(NULL);
}
void *customer(void *){
while(amountTime < HOURS* 60){
sem_wait(&accessWRSeats); //Request access to the number of available seats in the waiting room
//If there are seats available, do not leave (wait)
if(numberOfAvailableSeats > 0){
numberOfCustomers++;//customer enters the barber shop
cout << "\n\tCustomer: number of Customers: " << numberOfCustomers;
numberOfAvailableSeats--;//Sit down in a chair
sem_post(&customerReady);//Signal that there is a customer ready to be served
sem_post(&accessWRSeats);//release lock on seats; lock not needed
sem_wait(&barberReady);//Wait until the barber is ready
}
//no free seats
else{
cout << "\nCustomer:no fucking seats , number of available seats is:" << numberOfAvailableSeats << endl;
cout << "\n\tNumber of available seats:" << numberOfAvailableSeats;
cout << "\tNumber of Customers:%d\n" << numberOfCustomers << endl;
sem_post(&accessWRSeats); //release the lock on the seats and leave without a haircut
}
amountTime++;
}
cout << "EXITING CUSTOMER!" << endl;
pthread_exit(NULL);
}
int main(int argc, char** argv){
//get the number of seats available from the command line
if(argc == 2){
numberOfAvailableSeats = atoi(argv[1]);
}
else {
cout << "Please enter the number of seats you would like to simulate." << endl;
cin >> numberOfAvailableSeats;
}
/*Semaphore initialization*/
sem_init(&barberReady,0,1);
sem_init(&customerReady,0,0);
sem_init(&accessWRSeats,0,1);
pthread_t barberThread;
pthread_t customerThread;
int rc1 = 0; int rc2 = 0;
rc1 = pthread_create(&barberThread, NULL, barber, NULL);
rc2 = pthread_create(&customerThread, NULL, customer, NULL);
if(rc1 != 0)
cout << "Unable to create barber thread." << endl;
if(rc2 != 0)
cout << "Unable to create customer thread." << endl;
/*Customer_thread first blocked*/
rc2 = pthread_join(customerThread,NULL);
rc1 = pthread_join(barberThread,NULL);
printf("rc1: %d, rc2: %d\n", rc1, rc2);
cout << "EXITING PROGRAM" << endl;
return 0;
}
|