Prerequisite – Inter Process Communication
Problem : The analogy is based upon a hypothetical barber shop with one barber. There is a barber shop which has one barber, one barber chair, and n chairs for waiting for customers if there are any to sit on the chair.
- If there is no customer, then the barber sleeps in his own chair.
- When a customer arrives, he has to wake up the barber.
- If there are many customers and the barber is cutting a customer’s hair, then the remaining customers either wait if there are empty chairs in the waiting room or they leave if no chairs are empty.
Solution : The solution to this problem includes three semaphores.First is for the customer which counts the number of customers present in the waiting room (customer in the barber chair is not included because he is not waiting). Second, the barber 0 or 1 is used to tell whether the barber is idle or is working, And the third mutex is used to provide the mutual exclusion which is required for the process to execute. In the solution, the customer has the record of the number of customers waiting in the waiting room if the number of customers is equal to the number of chairs in the waiting room then the upcoming customer leaves the barbershop.
When the barber shows up in the morning, he executes the procedure barber, causing him to block on the semaphore customers because it is initially 0. Then the barber goes to sleep until the first customer comes up.
When a customer arrives, he executes customer procedure the customer acquires the mutex for entering the critical region, if another customer enters thereafter, the second one will not be able to anything until the first one has released the mutex. The customer then checks the chairs in the waiting room if waiting customers are less then the number of chairs then he sits otherwise he leaves and releases the mutex.
If the chair is available then customer sits in the waiting room and increments the variable waiting value and also increases the customer’s semaphore this wakes up the barber if he is sleeping.
At this point, customer and barber are both awake and the barber is ready to give that person a haircut. When the haircut is over, the customer exits the procedure and if there are no customers in waiting room barber sleeps.
Algorithm for Sleeping Barber problem:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Process Synchronization | Set 2
- Semaphores in Process Synchronization
- Monitors in Process Synchronization
- Introduction of Process Synchronization
- Dekker's algorithm in Process Synchronization
- Bakery Algorithm in Process Synchronization
- Peterson's Algorithm in Process Synchronization
- Process states and Transitions in a UNIX Process
- Pass the value from child process to parent process
- Process Table and Process Control Block (PCB)
- Process Scheduler : Job and Process Status
- Mutual Exclusion in Synchronization
- Critical Section in Synchronization
- Synchronization in Distributed Systems
- Priority Inheritance Protocol (PIP) in Synchronization
- Lock Variable Synchronization Mechanism
- Difference between Process Image and Multi Thread Process image
- Classical problems of Synchronization with Semaphore Solution
- Mutex lock for Linux Thread Synchronization
- TCP 3-Way Handshake Process
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Kumar Shubham 10