The problem of starvation can be solved with a first-in first-out (FIFO) queue. The following pseudocode guarantees synchronization between barber and customer and is deadlock free, but may lead to starvation of a customer. For example, one might store the number of people in the waiting room. A number of semaphores is also required to indicate the state of the system. This would take care of both of the problems mentioned above. The barber must acquire the room status mutex before checking for customers and release it when they begin either to sleep or cut hair a customer must acquire it before entering the shop and release it once they are sitting in a waiting room or barber chair, and also when they leave the shop because no seats were available. There are several possible solutions, but all solutions require a mutex, which ensures that only one of the participants can change state at once. Second, another problem may occur when two customers arrive at the same time when there is only one empty seat in the waiting room and both try to sit in the single chair only the first person to get to the chair will be able to sit.Ī multiple sleeping barbers problem has the additional complexity of coordinating several barbers among the waiting customers. Specifically, a customer may arrive to find the barber cutting hair so they return to the waiting room to take a seat but while walking back to the waiting room the barber finishes the haircut and goes to the waiting room, which he finds empty (because the customer walks slowly or went to the restroom) and thus goes to sleep in the barber chair. First, there is a risk that a race condition, where the barber sleeps while a customer waits for the barber to get them for a haircut, arises because all of the actions-checking the waiting room, entering the shop, taking a waiting room chair-take a certain amount of time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |