Skip to content

Implementation of the Sleeping Barber problem using Python3

Notifications You must be signed in to change notification settings

VincentAchukwu/SleepingBarber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Sleeping Barber Project

I approached this by imagining getting a haircut, being in the barber shop and asking myself what would happen, what should happen, and why it should happen. Breaking the problem bit by bit. First I began by creating a list of customers by appending them to a list. I knew I'd have to use the time.sleep() method in order to have the customers come at random intervals, as well as the barber cutting the customers hair at random intervals. I did this in a while loop and popped the customers off the list as they entered the barber shop, to prevent an infinite loop and not have the same customers enter the barber shop again. I expanded my program to use Object Oriented Programming and did some refactoring so the methods could be called. I did some research on threading.Event() function and implemented it to the barbers class, so each barber cuts/sleeps depending on what event occurs. I tested this code first by having one barber, 15 chairs and 20 customers. I wanted to see what would happen if all the chairs were used up and did this by increasing how often customers would enter (i.e every 2 to 3 second intervals instead of every 5 to 10 seconds). And that worked, and the customer left if there were no seats. My next task was to implement 3 barbers. I was stuck at first but I realised I needed a list of barbers too, just like the list of customers, and pass in that list to the barberShop class. So each barber is aware of how many customer are in the waiting room which is associated with the barberShop class.

All barbers go to sleep once the barber shop opens (while event hasn't occured). If an event occurs (customer enters), the customer wakes up all barbers, and the first barber to wake up from all the barbers who were sleeping (e.g barber 1, barber 2, barber 5 all wake up at the same time, but barber 1 is technically the "first" to wake up), barber 1 ends up cutting the customers' hair, and the other barbers go back to sleep. If a barber finishes cutting a customers' hair and another customer enters and wakes up the barbers, the barbers who are asleep wake up, but the barber who just finished cutting will cut the new customers' hair since that barber is already awake and just finished cutting a customer, and the other barbers resume in sleeping. Of course however, if new customers enter while other barber(s) are busy, another barber will step in in the same way and cut the customers' hair.

A small issue the program has is that once the barbers are finished with all customers, they wait for a customer and the program doesn't end. In other words, the threads are waiting for an event after all customers have been served. You can simply keyboard interrupt to exit execution. I couldn't quite figure out how to "kill" the threads and allow the program to end.

About

Implementation of the Sleeping Barber problem using Python3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages