DEV Community

Cover image for Mastering Concurrency with the Sleeping Barber Problem
Brandon Kindred
Brandon Kindred

Posted on

Mastering Concurrency with the Sleeping Barber Problem

Mastering Concurrency with the Sleeping Barber Problem

Hey there, fellow coder! Today, we're diving into one of the most fascinating concepts in computer science: the Sleeping Barber Problem. Whether you’re just getting into concurrency or you’re curious about how resource management works in real-world systems, this problem is a great way to get your feet wet.

In this post, we’ll break down what the Sleeping Barber Problem is, the key concepts it demonstrates, and how you can use Python to solve it. Plus, we’ll explore how you can apply these concepts to real-world programming problems.

What Is the Sleeping Barber Problem and Why Does It Matter?

The Sleeping Barber Problem is a classic scenario in computer science that models resource management and synchronization in a multi-threaded system. Imagine a barber shop with one barber, a single barber chair, and a waiting room with a limited number of chairs. Here’s how it works:

  • When no customers are present, the barber falls asleep in the chair, waiting for someone to arrive.

  • When a customer comes in and finds the barber asleep, they wake the barber up and sit down to get a haircut.

  • If the barber is already busy cutting hair, the customer either waits in the waiting room if there are empty chairs, or leaves if all the chairs are taken.

  • When the barber finishes cutting one customer’s hair, they check if there are other customers waiting. If there are, the barber invites the next customer to sit down for a haircut. If no one is waiting, the barber goes back to sleep.

Now, you might be wondering, “Why does this problem matter in programming?” Well, it turns out this simple scenario mimics real-world challenges that occur in concurrent programming — where multiple tasks (in this case, customers) compete for a limited resource (the barber). Understanding the Sleeping Barber Problem helps us grasp core concepts that we encounter when writing multi-threaded or multi-process applications.

Concurrency

The first key idea demonstrated by the Sleeping Barber Problem is concurrency — the ability of a system to handle multiple tasks at once. In the context of our barber shop, concurrency means handling multiple customers arriving at random times, possibly when the barber is busy with someone else. The barber can’t serve two customers simultaneously, so they must deal with the arriving customers in some orderly fashion.

In real-world applications, concurrency is critical. Think about a web server handling multiple user requests. The server is like the barber: it can process only one request at a time, but many requests may come in at once. Concurrency is what allows the server to juggle all those requests efficiently. Without managing concurrency properly, requests (like customers) might be dropped or left waiting indefinitely, leading to poor system performance or failure.

Synchronization

While concurrency deals with running multiple tasks, synchronization ensures that these tasks work together without interfering with each other. In the barber shop, synchronization guarantees that no two customers can sit in the barber’s chair at the same time or occupy the same waiting room chair. We use synchronization mechanisms (like locks and condition variables) to ensure that access to shared resources (the barber’s chair and waiting room chairs) is controlled.

In programming, synchronization is essential to avoid problems like race conditions, where two threads try to modify the same data at the same time. For example, if two customers both try to sit in the barber chair simultaneously, chaos would ensue. Similarly, in a multi-threaded application, if two threads try to update a shared variable without proper synchronization, they could overwrite each other’s changes, leading to bugs and unpredictable behavior.

Resource Allocation

The next concept is resource allocation where we figure out how to distribute limited resources among multiple tasks. In the barber shop, the barber chair and the waiting room chairs are limited resources. If all the waiting room chairs are occupied, new customers will have to leave. This models what happens in many real-world systems: you have a finite amount of memory, CPU power, or network bandwidth, and you have to figure out how to allocate those resources fairly and efficiently.

In real-world programming, resource allocation comes up in scenarios like thread pools. A thread pool is a group of worker threads that can execute tasks. When the pool is full (like the waiting room chairs), new tasks either have to wait or be rejected. Efficient resource allocation ensures that your system doesn’t become overloaded, and tasks are handled in an orderly way.

Blocking and Waiting

Another key part of the Sleeping Barber Problem is the idea of blocking and waiting. When the barber has no customers, they “block” or “wait” by falling asleep until a customer wakes them up. Similarly, if a customer arrives when the barber is busy, they “wait” in the waiting room for their turn. In programming, blocking refers to a task that can’t proceed until some condition is met — such as waiting for a file to become available or waiting for data to be written to a database.

Blocking and waiting are crucial for managing system resources efficiently. For example, in a database, if a process tries to access a record that another process is modifying, it may have to wait (block) until the record is available. Similarly, a customer waiting for the barber to finish a haircut is analogous to one process waiting for another to release a resource.

The Sleeping Barber Problem in Python

Now that we’ve talked about the concepts, let’s see them in action with a Python implementation. This example uses threading to simulate the barber and customers.

import threading
import time
import random
class BarberShop:
    def __init__(self, num_chairs, total_customers):
        self.num_chairs = num_chairs
        self.total_customers = total_customers
        self.customers_served = 0
        self.waiting_customers = 0
        self.shop_open = True
        self.barber_ready = threading.Condition()
        self.customer_ready = threading.Condition()
    def open_shop(self):
        print("The barber shop is now open.")
        barber_thread = threading.Thread(target=self.barber)
        barber_thread.start()
    def barber(self):
        while True:
            with self.customer_ready:
                while self.waiting_customers == 0:
                    if not self.shop_open:
                        print("The barber shop is closing. No more customers.")
                        return  # Exit when shop is closed and no customers remain
                    print("The barber is sleeping...")
                    self.customer_ready.wait()  # Wait for a customer to arrive
                # A customer is ready for a haircut
                self.waiting_customers -= 1
                print("The barber starts cutting hair.")
            # Simulate hair cutting
            time.sleep(random.randint(1, 3))
            with self.barber_ready:
                self.barber_ready.notify()  # Notify a customer that the barber is done
                print("The barber finished cutting hair.")

            with self.customer_ready:
                self.customers_served += 1
                if self.customers_served == self.total_customers:
                    self.shop_open = False  # Close the shop when all customers are served
    def customer(self, customer_id):
        with self.customer_ready:
            if self.waiting_customers < self.num_chairs:
                self.waiting_customers += 1
                print(f"Customer {customer_id} is waiting. Total waiting customers: {self.waiting_customers}")
                self.customer_ready.notify()  # Notify the barber that a customer is ready
            else:
                print(f"Customer {customer_id} left as there are no free chairs.")
                return  # Customer leaves if no chair is available
        # Wait for the barber to be ready
        with self.barber_ready:
            self.barber_ready.wait()  # Wait until the barber is ready
            print(f"Customer {customer_id} is getting a haircut.")
def main():
    num_chairs = 3  # Number of waiting chairs in the barber shop
    total_customers = 10  # Total number of customers to be served
    barber_shop = BarberShop(num_chairs, total_customers)
    barber_shop.open_shop()
    # Simulate customers arriving at random intervals
    for customer_id in range(1, total_customers + 1):
        customer_thread = threading.Thread(target=barber_shop.customer, args=(customer_id,))
        customer_thread.start()
        time.sleep(random.randint(1, 2))  # Customers arrive every 1-2 seconds
if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

What’s Going On in the Code?

Let’s break down the code for the Sleeping Barber Problem step by step so you can fully understand how it works and how each part fits together. This will help you grasp how concurrency and synchronization are managed in the barber shop simulation.

1. BarberShop Class Setup:

The first thing we do is create the BarberShop class. This class handles everything that happens inside the shop — how many customers are waiting, whether the shop is open, and how the barber and customers interact.

— num_chairs represents the number of waiting room chairs available for customers.
— total_customers tracks the total number of customers the shop will serve.
— waiting_customers keeps count of how many customers are currently sitting in the waiting room.
— shop_open is a flag that controls whether the barber is still accepting customers.
— barber_ready and customer_ready are condition variables that help synchronize the barber and customer threads, ensuring they communicate properly.

2. Opening the Barber Shop:

The open_shop method is where the barber starts working. This method spawns a new thread for the barber, allowing them to run concurrently with customers arriving. When you call this method, the barber is essentially “clocking in” for work.

3. Barber’s Work (barber method):

This method runs continuously in a loop, representing the barber’s working day.

— First, the barber checks whether there are any customers waiting by acquiring the customer_ready lock. If there are no customers (i.e., waiting_customers == 0), the barber goes to sleep by calling customer_ready.wait(). This releases the lock and waits for a notification that a customer has arrived.
— When a customer shows up, the barber wakes up, reduces the count of waiting customers (self.waiting_customers -= 1), and starts cutting hair (simulated by time.sleep()).
— After finishing the haircut, the barber notifies the next customer (if any) that they can come up for their turn by calling barber_ready.notify().
— The barber repeats this loop until all customers have been served.

4. Customer Arriving (customer method):

This method simulates each customer arriving at the shop.

— When a customer arrives, they check if there is a free chair in the waiting room by acquiring the customer_ready lock. If there’s room, they sit down, increment the count of waiting customers (self.waiting_customers += 1), and notify the barber that they are ready for a haircut.
— If the waiting room is full, the customer leaves, and no further action is taken (return exits the function).
— After notifying the barber, the customer waits until the barber is ready to give them a haircut (barber_ready.wait()), ensuring that only one customer sits in the barber chair at a time.

5. Main Function (main):

This is where the simulation kicks off. The barber shop is opened by calling barber_shop.open_shop(), which starts the barber’s thread. Then, customer threads are created in a loop, simulating customers arriving one by one at random intervals.

— time.sleep(random.randint(1, 2)) adds a random delay between customer arrivals to make the simulation more realistic. Customers don’t arrive all at once but spread out over time.

6. Condition Variables and Locking:

Two key condition variables, barber_ready and customer_ready, help synchronize communication between the barber and customers:

— customer_ready: Used by customers to notify the barber that they are waiting. The barber sleeps when no customers are ready and wakes up when notified by a customer.
— barber_ready: Used by the barber to notify the customer that their haircut is finished and the next customer can come up. Customers wait until the barber is ready for them.

7. Customer and Barber Synchronization

The condition variables ensure that customers don’t try to sit in the barber’s chair while it’s occupied and that the barber doesn’t try to cut hair when there are no customers. It’s a give-and-take process: the barber works when customers are waiting, and customers wait their turn when the barber is busy.

Real-World Applications of the Sleeping Barber Problem

At first glance, the Sleeping Barber Problem may seem like an abstract puzzle or a fun thought experiment. But in reality, it has practical applications that are very relevant to the world of computing and technology. Let’s break down a few examples where this problem comes into play in real life:

Web Servers: Imagine a busy website where thousands of users are trying to access it at the same time. Web servers need to handle requests from all these users with limited resources, such as available threads or database connections. Just like the barber can only serve a few customers at a time, the server has to manage incoming requests carefully to avoid overload and ensure that everyone gets served efficiently.

Operating Systems: In an operating system, many processes (or programs) are running at the same time, all needing access to limited resources like CPU cores or memory. The system has to decide which processes get to use these resources, similar to how the barber decides which customer gets a haircut next. The operating system needs to keep everything running smoothly while making sure no process waits too long.

Databases: Databases are often accessed by multiple users or applications at once, especially in multi-user environments. The system has to ensure that access to data is synchronized, so everyone gets consistent information without causing conflicts or errors (like race conditions). Think of this like the barber making sure no two customers are trying to sit in the same chair at once!

In short, the Sleeping Barber Problem highlights important strategies for managing limited resources in a way that keeps things running smoothly, whether it’s on a server, in an operating system, or within a database.

Wrapping It Up

And there you have it! The Sleeping Barber Problem is a great way to get your feet wet with concepts like concurrency, synchronization, and resource management. We saw how the barber and customers represent tasks competing for limited resources, and we learned how to keep things running smoothly without chaos. These concepts are super important when you’re building apps or systems that need to handle a bunch of things at once — like servers or databases.

But we’re not done yet! Up next, we’re diving into the Dining Philosophers Problem, which is another fun challenge that builds on what we’ve learned here. In that one, we’ll tackle how multiple philosophers (or processes) can share limited forks (resources) without getting stuck or starving. It’s going to be even more interesting, so stay tuned!

If you’d rather not copy and paste code from this post, I’ve got you covered! You can check out the full code for the Sleeping Barber Problem on GitHub. Feel free to clone it, experiment with it, and make it your own.

Top comments (0)