Sleeping Barber problem | Operating System - Computer Science Engineering (CSE) PDF Download

Sleeping Barber problem in Process Synchronization

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.

Sleeping Barber problem | Operating System - Computer Science Engineering (CSE)

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.

Sleeping Barber problem | Operating System - Computer Science Engineering (CSE)

The document Sleeping Barber problem | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Sleeping Barber problem - Operating System - Computer Science Engineering (CSE)

1. What is the Sleeping Barber problem in process synchronization?
Ans. The Sleeping Barber problem is a classic synchronization problem in computer science. It is used to illustrate the challenges of managing concurrent access to shared resources. In this problem, there is a barber who serves customers one at a time. The barber has a certain number of chairs for waiting customers, and if all the chairs are occupied, additional customers must wait outside the shop. The problem is to design a synchronization scheme that ensures proper coordination between the barber and the customers.
2. How does the Sleeping Barber problem relate to process synchronization?
Ans. The Sleeping Barber problem is a common example used to demonstrate the need for process synchronization mechanisms. In this problem, the barber and the customers represent concurrent processes that need to access shared resources (i.e., the chairs). Without proper synchronization, issues like race conditions and deadlock can occur. Therefore, solving the Sleeping Barber problem requires implementing synchronization primitives, such as semaphores or mutexes, to ensure that the barber and customers access the shared resources in a coordinated manner.
3. What are the challenges in solving the Sleeping Barber problem?
Ans. Solving the Sleeping Barber problem involves addressing several challenges related to process synchronization. These challenges include: 1. Ensuring mutual exclusion: Only one customer or the barber can access the shared resources (i.e., chairs) at a time to avoid conflicts. 2. Managing concurrency: The solution should allow multiple customers to wait for service simultaneously without causing issues like deadlock or starvation. 3. Coordinating access to resources: The solution needs to ensure that customers are serviced in the order they arrive and that the barber does not attempt to serve a customer if there are none waiting. 4. Handling resource availability: The solution should handle scenarios where the barber is sleeping and no customers are present or where all chairs are occupied. 5. Avoiding busy waiting: The solution should prevent busy waiting, where processes continuously check for resource availability, as it wastes system resources. Instead, a signaling mechanism should be used to notify processes when a resource becomes available.
4. What are some common synchronization mechanisms used to solve the Sleeping Barber problem?
Ans. Several synchronization mechanisms can be used to solve the Sleeping Barber problem. Some commonly used ones include: 1. Semaphores: Binary semaphores can be used to control access to shared resources. A semaphore can represent the number of available chairs, and the barber and customers can acquire and release the semaphore accordingly. 2. Mutexes: Mutexes (mutual exclusion locks) can be used to enforce mutual exclusion. A mutex can be used to protect access to the shared resources, ensuring that only one process can access them at a time. 3. Condition variables: Condition variables can be used to coordinate the barber and customers. They allow processes to wait until a certain condition is met (e.g., when a chair becomes available or a customer arrives). 4. Monitors: Monitors are high-level synchronization constructs that encapsulate data and operations related to synchronization. They provide a structured way to solve synchronization problems like the Sleeping Barber problem.
5. What are the potential issues that can arise in the solution to the Sleeping Barber problem?
Ans. While solving the Sleeping Barber problem, several issues can arise if the synchronization mechanism is not implemented correctly. Some potential issues include: 1. Race conditions: If proper synchronization mechanisms are not used, race conditions can occur, leading to inconsistent or incorrect behavior. For example, multiple customers may attempt to occupy the same chair simultaneously. 2. Deadlock: Deadlock can occur if the synchronization scheme does not handle situations where the barber is waiting for a customer, and the customer is waiting for the barber to become available. This can result in a situation where no progress can be made. 3. Starvation: Starvation can occur if a customer keeps waiting for a long time without being serviced. This can happen if the synchronization mechanism does not ensure fair access to the barber. 4. Busy waiting: Busy waiting can waste system resources if the solution relies on continuously checking for resource availability instead of using signaling mechanisms. It can also lead to high CPU utilization. 5. Inefficient resource usage: If the synchronization mechanism is not designed efficiently, it can result in inefficient usage of resources like CPU time, memory, or network bandwidth. This can impact the overall system performance.
10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

study material

,

mock tests for examination

,

Important questions

,

past year papers

,

practice quizzes

,

MCQs

,

pdf

,

Summary

,

Sleeping Barber problem | Operating System - Computer Science Engineering (CSE)

,

video lectures

,

Objective type Questions

,

Semester Notes

,

Sample Paper

,

Exam

,

Viva Questions

,

ppt

,

Free

,

Sleeping Barber problem | Operating System - Computer Science Engineering (CSE)

,

Sleeping Barber problem | Operating System - Computer Science Engineering (CSE)

;