Barber Shop
Explore the Barber Shop concurrency problem to understand thread synchronization in C#. Learn how to use semaphores and locks to manage customer waiting, waking the barber, and coordinating haircuts. This lesson helps you implement a concurrency control solution suitable for senior engineering interviews.
We'll cover the following...
Barber Shop
A similar problem appears in Silberschatz and Galvin's OS book, and variations of this problem exist in the wild.
A barbershop consists of a waiting room with 'n' chairs, and a barber chair for giving haircuts. If there are no customers to be served, the barber goes to sleep. 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, the customer wakes up the barber. Write a program to coordinate the interaction between the barber and the customers.
Solution
First of all, we need to understand the different state transitions for this problem before we devise a solution. Let's look at them piecemeal: ...