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.
37 static void randwait(int secs)
41 // Generate a random number...
42 len = (int)((drand48() * secs) + 1);
46 static void* customer(void* number)
48 int num = *(int*)number;
50 // Leave for the shop and take some random amount of
52 printf("Customer %d leaving for barber shop.\n", num);
54 printf("Customer %d arrived at barber shop.\n", num);
56 // Wait for space to open up in the waiting room...
57 sem_wait(&waitingRoom);
58 printf("Customer %d entering waiting room.\n", num);
60 // Wait for the barber chair to become free.
61 sem_wait(&barberChair);
63 // The chair is free so give up your spot in the
65 sem_post(&waitingRoom);
67 // Wake up the barber...
68 printf("Customer %d waking the barber.\n", num);
69 sem_post(&barberPillow);
71 // Wait for the barber to finish cutting your hair.
75 sem_post(&barberChair);
76 printf("Customer %d leaving barber shop.\n", num);
80 static void* barber(void* junk)
82 // While there are still customers to be serviced...
83 // Our barber is omnicient and can tell if there are
84 // customers still on the way to his shop.
87 // Sleep until someone arrives and wakes you..
88 printf("The barber is sleeping\n");
89 sem_wait(&barberPillow);
91 // Skip this stuff at the end...
94 // Take a random amount of time to cut the
96 printf("The barber is cutting hair\n");
98 printf("The barber has finished cutting hair.\n");
100 // Release the customer when done cutting...
103 printf("The barber is going home for the day.\n");
109 int main(int argc, char* argv[])
112 pthread_t tid[MAX_CUSTOMERS];
114 int i, numCustomers, numChairs;
115 int Number[MAX_CUSTOMERS];
117 // Check to make sure there are the right number of
118 // command line arguments.
120 printf("Use: SleepBarber <Num Customers> <Num Chairs> <rand seed>\n");
124 // Get the command line arguments and convert them
126 numCustomers = atoi(argv[1]);
127 numChairs = atoi(argv[2]);
128 RandSeed = atol(argv[3]);
130 // Make sure the number of threads is less than the number of
131 // customers we can support.
132 if (numCustomers > MAX_CUSTOMERS) {
133 printf("The maximum number of Customers is %d.\n", MAX_CUSTOMERS);
137 printf("\nSleepBarber.c\n\n");
138 printf("A solution to the sleeping barber problem using semaphores.\n");
140 // Initialize the random number generator with a new seed.
143 // Initialize the numbers array.
144 for (i = 0; i < MAX_CUSTOMERS; i++) {
148 // Initialize the semaphores with initial values...
149 sem_init(&waitingRoom, 0, numChairs);
150 sem_init(&barberChair, 0, 1);
151 sem_init(&barberPillow, 0, 0);
152 sem_init(&seatBelt, 0, 0);
154 // Create the barber.
155 pthread_create(&btid, NULL, barber, NULL);
157 // Create the customers.
158 for (i = 0; i < numCustomers; i++) {
159 pthread_create(&tid[i], NULL, customer, (void*)&Number[i]);
162 // Join each of the threads to wait for them to finish.
163 for (i = 0; i < numCustomers; i++) {
164 pthread_join(tid[i], NULL);
167 // When all of the customers are finished, kill the
170 sem_post(&barberPillow); // Wake the barber so he will exit.
171 pthread_join(btid, NULL);