How to Solve the Barber’s Dilemma Using Semaphores in Python?

Estimated read time 3 min read

The Barber’s Dilemma is a classic concurrency problem that involves coordinating access to a shared resource (the barber’s chair) among multiple threads (the customers). Here is an example of how to solve this problem using semaphores in Python:

import threading
import time

# create a semaphore for the barber's chair and a list to store the waiting customers
chair = threading.Semaphore(1)
customers = []

# define a function to represent a customer thread
def customer_thread(id):
    global customers
    print("Customer %d arrived" % id)
    # check if there is an empty chair in the barber shop
    if chair.acquire(blocking=False):
        print("Customer %d got a haircut" % id)
        time.sleep(1) # simulate the haircut
        chair.release() # release the chair
    else:
        print("Customer %d left because the shop was full" % id)
    # add the customer to the waiting list
    customers.append(id)

# define a function to represent the barber thread
def barber_thread():
    global customers
    while True:
        # check if there are any waiting customers
        if len(customers) > 0:
            id = customers.pop(0) # remove the first customer from the waiting list
            print("Barber gave a haircut to customer %d" % id)
            time.sleep(2) # simulate the haircut
        else:
            print("Barber is sleeping")
            time.sleep(3) # simulate the barber sleeping

# create the barber thread and start it
barber = threading.Thread(target=barber_thread)
barber.start()

# create the customer threads and start them
for i in range(1, 11):
    customer = threading.Thread(target=customer_thread, args=(i,))
    customer.start()
    time.sleep(0.5) # simulate the arrival of the customer

In this solution, we first create a semaphore chair to represent the barber’s chair, which can be held by at most one thread at a time. We also create a list customers to store the waiting customers.

We then define a function customer_thread to represent a customer thread. The function takes an integer id representing the customer’s ID as input. The function first prints out a message indicating that the customer has arrived.

The function then tries to acquire the semaphore chair using the acquire method with the blocking=False argument, which allows the function to continue executing even if the semaphore is not available. If the semaphore is acquired successfully, the function prints out a message indicating that the customer has received a haircut, simulates the haircut with a sleep statement, and releases the semaphore using the release method.

If the semaphore is not available, the function prints out a message indicating that the customer has left the barber shop. Finally, the function adds the customer’s ID to the customers list.

We also define a function barber_thread to represent the barber thread. The function continuously checks if there are any waiting customers in the customers list. If there are, the function removes the first customer from the list, prints out a message indicating that the barber has given the customer a haircut, and simulates the haircut with a sleep statement. If there are no waiting customers, the function prints out a message indicating that the barber is sleeping and simulates the barber sleeping with a sleep statement.

In the main part of the script, we create the barber thread and start it. We then create 10 customer threads and start them with a delay of 0.5 seconds between each thread to simulate the arrival of the customers.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply