Sleeping Barbers Report
Sleeping Barbers Report
Problem Overview
The Sleeping Barber Problem is a classic synchronization problem that demonstrates concurrent
programming challenges. The scenario involves a barbershop with one barber, a waiting room with
limited chairs, and customers who arrive randomly. The barber serves customers or sleeps when
none are present, while customers either take a seat in the waiting room or leave if it's full.
Implementation Approach
1. Synchronization Mechanisms
2. Key Components
Barber Thread
Sleeps when no customers are present
Wakes up when customers arrive
Processes customers from the waiting room
Signals completion of haircuts
Customer Threads
Attempt to enter the waiting room
Wake up the barber if sleeping
Wait for haircut completion
Leave after service or if waiting room is full
Customer Generator
Creates customer threads at random intervals
Simulates varying customer arrival patterns
Closes shop after generating all customers
3. Synchronization Flow
1. Customer arrives:
Checks waiting room capacity
Either enters waiting room or leaves if full
Wakes barber if sleeping
1. Barber cycle:
Sleeps if no customers
Wakes when customer arrives
Processes customers from waiting room
Signals completion of each haircut
Implementation Results
Conclusion
The implementation successfully demonstrates the Sleeping Barber Problem while addressing key
synchronization challenges. The use of Python's threading primitives provides a clear and efficient
solution to this classic concurrency problem.