Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b44a934f1b3c305aeabc36c3bc64d24f3a09682b
[simgrid.git] / teshsuite / mc / mcmini / barber_shop_ok.c
1 #ifdef _REENTRANT
2 #undef _REENTRANT
3 #endif
4 #define _REENTRANT
5
6 #include <pthread.h>
7 #include <semaphore.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 // The maximum number of customer threads.
13 #define MAX_CUSTOMERS 25
14
15 // Define the semaphores.
16
17 // waitingRoom Limits the # of customers allowed
18 // to enter the waiting room at one time.
19 sem_t waitingRoom;
20
21 // barberChair ensures mutually exclusive access to
22 // the barber chair.
23 sem_t barberChair;
24
25 // barberPillow is used to allow the barber to sleep
26 // until a customer arrives.
27 sem_t barberPillow;
28
29 // seatBelt is used to make the customer to wait until
30 // the barber is done cutting his/her hair.
31 sem_t seatBelt;
32
33 // Flag to stop the barber thread when all customers
34 // have been serviced.
35 int allDone = 0;
36
37 static void randwait(int secs)
38 {
39   int len;
40
41   // Generate a random number...
42   len = (int)((drand48() * secs) + 1);
43   sleep(len);
44 }
45
46 static void* customer(void* number)
47 {
48   int num = *(int*)number;
49
50   // Leave for the shop and take some random amount of
51   // time to arrive.
52   printf("Customer %d leaving for barber shop.\n", num);
53   randwait(5);
54   printf("Customer %d arrived at barber shop.\n", num);
55
56   // Wait for space to open up in the waiting room...
57   sem_wait(&waitingRoom);
58   printf("Customer %d entering waiting room.\n", num);
59
60   // Wait for the barber chair to become free.
61   sem_wait(&barberChair);
62
63   // The chair is free so give up your spot in the
64   // waiting room.
65   sem_post(&waitingRoom);
66
67   // Wake up the barber...
68   printf("Customer %d waking the barber.\n", num);
69   sem_post(&barberPillow);
70
71   // Wait for the barber to finish cutting your hair.
72   sem_wait(&seatBelt);
73
74   // Give up the chair.
75   sem_post(&barberChair);
76   printf("Customer %d leaving barber shop.\n", num);
77   return NULL;
78 }
79
80 static void* barber(void* junk)
81 {
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.
85   while (!allDone) {
86
87     // Sleep until someone arrives and wakes you..
88     printf("The barber is sleeping\n");
89     sem_wait(&barberPillow);
90
91     // Skip this stuff at the end...
92     if (!allDone) {
93
94       // Take a random amount of time to cut the
95       // customer's hair.
96       printf("The barber is cutting hair\n");
97       randwait(3);
98       printf("The barber has finished cutting hair.\n");
99
100       // Release the customer when done cutting...
101       sem_post(&seatBelt);
102     } else {
103       printf("The barber is going home for the day.\n");
104     }
105   }
106   return NULL;
107 }
108
109 int main(int argc, char* argv[])
110 {
111   pthread_t btid;
112   pthread_t tid[MAX_CUSTOMERS];
113   long RandSeed;
114   int i, numCustomers, numChairs;
115   int Number[MAX_CUSTOMERS];
116
117   // Check to make sure there are the right number of
118   // command line arguments.
119   if (argc != 4) {
120     printf("Use: SleepBarber <Num Customers> <Num Chairs> <rand seed>\n");
121     exit(-1);
122   }
123
124   // Get the command line arguments and convert them
125   // into integers.
126   numCustomers = atoi(argv[1]);
127   numChairs    = atoi(argv[2]);
128   RandSeed     = atol(argv[3]);
129
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);
134     exit(-1);
135   }
136
137   printf("\nSleepBarber.c\n\n");
138   printf("A solution to the sleeping barber problem using semaphores.\n");
139
140   // Initialize the random number generator with a new seed.
141   srand48(RandSeed);
142
143   // Initialize the numbers array.
144   for (i = 0; i < MAX_CUSTOMERS; i++) {
145     Number[i] = i;
146   }
147
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);
153
154   // Create the barber.
155   pthread_create(&btid, NULL, barber, NULL);
156
157   // Create the customers.
158   for (i = 0; i < numCustomers; i++) {
159     pthread_create(&tid[i], NULL, customer, (void*)&Number[i]);
160   }
161
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);
165   }
166
167   // When all of the customers are finished, kill the
168   // barber thread.
169   allDone = 1;
170   sem_post(&barberPillow); // Wake the barber so he will exit.
171   pthread_join(btid, NULL);
172 }