void *barber() {
while (1) {
/* Barber sleeps if there are no customers */
sem_wait(&customers);
/* Enters critical region */
sem_wait(&mutex);
/* Barber brings one customer to the barber chair */
waiting_customers--;
/* Barber is now ready to work */
sem_post(&barbers);
/* Leaves critical region */
sem_post(&mutex);
cut_hair();
}
}
void *customer() {
/* Enters critical region */
sem_wait(&mutex);
/* Customer waits if there's an empty waiting chair */
if (waiting_customers < N_CHAIRS) {
waiting_customers++;
/* Barber gets a wakeup */
sem_post(&customers);
/* Leaves critical region */
sem_post(&mutex);
/* Waits to be called by the barber */
sem_wait(&barbers);
get_haircut();
}
/* Customer leaves if barbershop is full*/
else {
/* Leaves critical region */
sem_post(&mutex);
leave_barbershop();
}
}