12 // The maximum number of customer threads.
13 #define MAX_CUSTOMERS 25
15 // Define the semaphores.
17 // waitingRoom Limits the # of customers allowed
18 // to enter the waiting room at one time.
21 // barberChair ensures mutually exclusive access to
25 // barberPillow is used to allow the barber to sleep
26 // until a customer arrives.
29 // seatBelt is used to make the customer to wait until
30 // the barber is done cutting his/her hair.
33 // Flag to stop the barber thread when all customers
34 // have been serviced.
38 static void randwait(int secs)
42 // Generate a random number...
43 len = (int)((drand48() * secs) + 1);
47 static void* customer(void* number)
49 int num = *(int*)number;
51 // Leave for the shop and take some random amount of
54 printf("Customer %d leaving for barber shop.\n", num);
57 printf("Customer %d arrived at barber shop.\n", num);
59 // Wait for space to open up in the waiting room...
60 sem_wait(&waitingRoom);
62 printf("Customer %d entering waiting room.\n", num);
64 // Wait for the barber chair to become free.
65 sem_wait(&barberChair);
67 // The chair is free so give up your spot in the
69 sem_post(&waitingRoom);
71 // Wake up the barber...
73 printf("Customer %d waking the barber.\n", num);
74 sem_post(&barberPillow);
76 // Wait for the barber to finish cutting your hair.
80 sem_post(&barberChair);
82 printf("Customer %d leaving barber shop.\n", num);
86 static void* barber(void* junk)
88 // While there are still customers to be serviced...
89 // Our barber is omnicient and can tell if there are
90 // customers still on the way to his shop.
93 // Sleep until someone arrives and wakes you..
95 printf("The barber is sleeping\n");
96 sem_wait(&barberPillow);
98 // Skip this stuff at the end...
101 // Take a random amount of time to cut the
104 printf("The barber is cutting hair\n");
107 printf("The barber has finished cutting hair.\n");
109 // Release the customer when done cutting...
113 printf("The barber is going home for the day.\n");
119 int main(int argc, char* argv[])
122 pthread_t tid[MAX_CUSTOMERS];
124 int i, numCustomers, numChairs;
125 int Number[MAX_CUSTOMERS];
127 // Check to make sure there are the right number of
128 // command line arguments.
130 printf("Use: SleepBarber <Num Customers> <Num Chairs> <rand seed> <DEBUG>\n");
134 // Get the command line arguments and convert them
136 numCustomers = atoi(argv[1]);
137 numChairs = atoi(argv[2]);
138 RandSeed = atol(argv[3]);
139 DEBUG = atoi(argv[4]);
141 // Make sure the number of threads is less than the number of
142 // customers we can support.
143 if (numCustomers > MAX_CUSTOMERS) {
144 printf("The maximum number of Customers is %d.\n", MAX_CUSTOMERS);
148 printf("\nSleepBarber.c\n\n");
149 printf("A solution to the sleeping barber problem using semaphores.\n");
151 // Initialize the random number generator with a new seed.
154 // Initialize the numbers array.
155 for (i = 0; i < MAX_CUSTOMERS; i++) {
159 // Initialize the semaphores with initial values...
160 sem_init(&waitingRoom, 0, numChairs);
161 sem_init(&barberChair, 0, 1);
162 sem_init(&barberPillow, 0, 0);
163 sem_init(&seatBelt, 0, 0);
165 // Create the barber.
166 pthread_create(&btid, NULL, barber, NULL);
168 // Create the customers.
169 for (i = 0; i < numCustomers; i++) {
170 pthread_create(&tid[i], NULL, customer, (void*)&Number[i]);
173 // Join each of the threads to wait for them to finish.
174 for (i = 0; i < numCustomers; i++) {
175 pthread_join(tid[i], NULL);
178 // When all of the customers are finished, kill the
181 sem_post(&barberPillow); // Wake the barber so he will exit.
182 pthread_join(btid, NULL);