View Single Post
Sleeping barber
Super Moderator
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default Sleeping barber - 05-13-2014, 09:36 AM

Originally Posted by chris42k View Post
Hey so im working on a final project where I simulating a barbershop. With four chairs, I assign the first four patrons to each seat. This is where i get stuck: I must have a random patron move from the seat at a random duration so the next available client fills the seat. I have 6 patrons for now (4 in the chair, 2 waiting) . Help please.
This is the classic sleeping barber producer/consumer problem. Google and Wikipedia have lots of information on it. I am not sure how to set this up in Alice other than you need to do two things "at the same time." Which to me screems Events! In the absolute simplest case you would write two sets of methods, one for the patrons showing up and one for the barber cutting the hair. Each set of methods could be started by a while the world is running event and random wait times used to control when the next patron goes to the shop.
At this point I think your description is wrong. You would start out with the barber asleep and the waiting chairs empty.
The patrons methods would randomly have a patron show up, if there is an empty chair the patron would sit down and wait. At the same time the barber thread would randomly wake the barber if the chairs are empty he or she would go back to sleep otherwise the customer in the first chair would go to the barber's chair and the other patrons would move down one seat toward the first chair. When the barber is done with a haircut she or he would check to see if there was a customer waiting and the newly shorn patron would go back to the patron queue. As long as the first chair was not empty, the next patron would move to the barber chair and the others would shift toward the first chair. When all the chairs are empty the barber would go back to sleep.
The fact that you have six patrons and four chairs suggests to me that the goal is to never use the last patron. In the worst case you have one customer in the barber chair and four in the waiting chairs (5 total) if the last patron attempts to go to the shop before the barber can finish his current patron and start on the next then the last patron would find the shop full and go somewhere else. This would usually end the simulation.

There use to be a great java applet demonstrating all this. The patrons were represented as cousin It from the Addams family and when the barber was finished and turned to get the next customer, the chair was empty

Mark Henwood

Last edited by chickentree; 05-13-2014 at 09:40 AM.
Reply With Quote