The Sleeping-Barber Problem

The Classic Concurrency Problem

A barbershop consists of a waiting room with n > 0 chairs and the barber room containing the barber chair. If there are no customers to be served, then the barber sleeps. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, then the customer wakes up the barber and the shaving process begins. After they finish, the customer leaves and the barber starts shaving another customer, if there are any in the waiting room. Otherwise, he goes to sleep.

Answer

Explanation:

Following are the Semaphores:

  • Customers: Counts waiting customers;
  • Barbers: Number of idle barbers (0 or 1);
  • mutex: Used for mutual exclusion;
  • Cutting: Ensures that the barber won’t cut another customer’s hair before the previous customer leaves;

Shared data variable:

count_cust: Counts waiting customers. ------------copy of customers. As value of semaphores can’t access directly.

// shared data
semaphore customers = 0;
semaphore barbers = 0;
semaphore cutting = 0;
semaphore mutex = 1;
int count_cust= 0;
void barber() {
while(true) { //shop is always open
wait(customers); //sleep when there are no waiting customers
wait(mutex); //mutex for accessing customers
count_cust= count_cust-1; //customer left
signal(barbers);
signal(mutex);
cut_hair();
}
}
void customer() {
wait(mutex); //mutex for accessing count_cust
if (count_cust< n) {
count_cust= count_cust+1; //new customer
signal(customers); signal(mutex);
wait(barbers); //wait for available barbers
get_haircut();
}
else { //do nothing (leave) when all chairs are used.
signal(mutex);
}
}
cut_hair(){ waiting(cutting);} get_haircut(){get hair cut for some time; signal(cutting);}

Final Answer

The Sleeping-Barber Problem is a classic concurrency problem in computer science that can be represented and solved using mathematics.

Explanation

The Sleeping-Barber Problem is a classic concurrency problem in computer science, which can be represented using mathematics. This problem involves the synchronization of multiple processes, such as the barber and the customers, and the allocation of resources, such as the barber chair and the waiting room chairs. The problem can be solved using various mathematical models, such as queuing theory and simulation, to analyze the behavior and efficiency of the system.

What is the core problem in the Sleeping-Barber Problem?

The core problem in the Sleeping-Barber Problem is the synchronization and resource allocation in a barbershop setting where customers arrive to get a haircut and the barber needs to manage the waiting customers efficiently.

← Sketch programs in real estate appraisal The exciting world of laser printers explore the possibilities →