How sleeping barber problem is solved ??describe with algorithm and proper implementation?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

How sleeping barber problem is solved ??describe with algorithm and proper implementation??

sololearn

1st Aug 2017, 10:49 AM
GAWEN STEASY
GAWEN STEASY - avatar
2 Answers
+ 10
what is sleeping barber problem?
1st Aug 2017, 11:07 AM
jay
jay - avatar
+ 5
There is one barber in the barber shop, one barber chair and n chairs for waiting customers. If there are no customers, the barber sits down in the barber chair and takes a nap. An arriving customer must wake the barber. Subsequent arriving customers take a waiting chair if any are empty or leave if all chairs are full. This problem addresses race conditions. 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. When the barber arrives for work, the barber procedure is executed blocking the barber on the customer semaphore until a customer arrives. When a customer arrives, the customer procedure is executed which begins by acquiring mutex to enter a critical region. Subsequent arriving customers have to wait until the first customer has released mutex. After acquiring mutex, a customer checks to see if the number of waiting customers is less than the number of chairs. If not, mutex is released and the customer leaves without a haircut. If there is an available chair, the waiting counter is incremented, the barber is awaken, the customer releases mutex, the barber grabs mutex, and begins the haircut. Once the customer's hair is cut, the customer leaves. The barber then checks to see if there is another customer. If not, the barber takes a nap. @Jay I want to know how its algorithm is implemented
1st Aug 2017, 11:12 AM
GAWEN STEASY
GAWEN STEASY - avatar