0% found this document useful (0 votes)
6 views4 pages

Sleeping Barbers Report

Sleeping barbers solution

Uploaded by

Rommel L. Bayoca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Sleeping Barbers Report

Sleeping barbers solution

Uploaded by

Rommel L. Bayoca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2.

Sleeping Barber Problem Implementation and Screenshot 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

The implementation uses several threading primitives to handle synchronization:

Threading.Event: Used for signaling between barber and customers

`barber_sleeping`: Indicates when the barber is asleep


`customer_ready`: Signals when a customer is ready for service
`haircut_done`: Indicates completion of a haircut

Queue: Implements the waiting room with a maximum capacity

Threading.Lock: Ensures thread-safe operations on shared resources

2. Key Components

Barber Shop Class


 Manages the state of the shop and synchronization objects
 Tracks number of served customers
 Implements waiting room logic using a Queue

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

Sample Simulation Output


Performance Analysis
 Successfully handles multiple concurrent customers
 Maintains proper synchronization between threads
 Effectively manages waiting room capacity
 Provides accurate tracking of served customers

Synchronization Challenges Addressed


1. Race Conditions: Prevented using proper locking mechanisms
2. Deadlock Prevention: Careful ordering of resource acquisition
3. Starvation Avoidance: First-come-first-served queue implementation
4. Resource Management: Limited waiting room capacity

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy