7 #define NUM_CUSTOMERS 10
8 #define MAX_FREESEATS 2
12 sem_t seasem
; /* mutual exclusion for 'freeseats' */
14 unsigned int freeseats
= MAX_FREESEATS
;
16 /* function prototypes */
17 void *barthread(void *arg
);
18 void *custhread(void *arg
);
22 pthread_t custid
[NUM_CUSTOMERS
];
25 /* initialize the semaphores */
26 sem_init(&barsem
, 0, 0);
27 sem_init(&cussem
, 0, 0);
28 sem_init(&seasem
, 0, 1);
30 /* create the barber thread */
31 if (pthread_create(&bartid
, NULL
, barthread
, NULL
)) {
32 perror("pthread_create() error");
36 /* create the customer threads */
37 for (i
=0; i
<NUM_CUSTOMERS
; i
++) {
38 if (pthread_create(&custid
[i
], NULL
, custhread
, NULL
)) {
39 perror("pthread_create() error");
44 pthread_join(bartid
, NULL
); /* wait for the barber to retire :) */
48 void *barthread(void *arg
) {
56 printf("The barber is cutting hair\n");
61 void *custhread(void *arg
) {
62 printf("Customer has arrived\n");
71 printf("No free seats - customer leaving\n");