This solution uses three semaphores, one for customers (counts waiting customers), one for the barber (idle - 0 or busy - 1), and a mutual exclusion semaphore, mutex. So, the barber waiting for a new customer who wakes up the barber and the customer thinks that the barber is busy so, he also waits for the barber.īoth are waiting forever which leads to a race condition. But the barber finds no one in the waiting room (customer yet not arrive at the waiting room), he sits down to the chair and sleeps. While he is on the way to the waiting room, the barber just finished his job and sees the waiting room. For example, a customer arrives and notices that the barber is busy cutting the hair of another person. The problem has occurred from the actions of both the barber and customer. This sleeping barber problem may lead to a race condition. But if the waiting room is not empty, the customer must take a seat in the waiting room and wait his turn. So he wakes up the barber and takes his turn in the barber chair. If a customer comes in and the barber is asleep, he knows it’s his turn to get his hair cut. When the waiting room is empty, the barber goes to sleep in the barber chair. The waiting room has n chairs and the workroom only has the barber chair. Dijkstra introduced the Sleeping Barber Problem (Dijkstra, 1965): A barbershop is divided into two rooms.
0 Comments
Leave a Reply. |