The Sleeping Barber Problem


Waiting Room
  •  
  •  
  •  
  •  
  •  
Barber's Chair
State
  • Barber
  • Customers
  • Mutex
  • Waiting
  • Chairs
What's going on?
Barber Active
void *barber() {
  while (1) {
    sem_wait(&customers);
    sem_wait(&mutex);
    waiting_customers--;
    sem_post(&barbers);
    sem_post(&mutex);
    cut_hair();
  }
}
Customer Active
void *customer() {
  sem_wait(&mutex);
  if (waiting_customers < N_CHAIRS) {      
    waiting_customers++;
    sem_post(&customers);
    sem_post(&mutex);
    sem_wait(&barbers);
    get_haircut();
  }
  else {
    sem_post(&mutex);
  }
  leave_barbershop();
}
sleep