Sleeping Barber Project

The Project

Write a program that simulates the Sleeping Barber problem. The problem was described in class and is discussed in detail in section 2.4.3 of the text Modern Operating Systems second edition, by A.S. Tanenbaum.

How do I get started?

Carefully study the description of the problem and the solution outlined in section 2.4.3 of Tanenbaum. An outline of a semaphore based solution appears in Figure 2-36 in Tanenbaum. You may use either C, C++ or Java for the project. In addition, you may use either semaphores or monitors for synchronization. If you develop a semaphore based solution in Java, you will have to simulate semaphores. Java code for this is available on the OS web page.

Summary of the Problem

One Barber provides service to customers - one customer at a time. The Barber completes the service before starting the next customer. The Barber starts but goes to sleep if there are no customers waiting. The first arriving customer wakes up the barber and receives service. After being serviced, a customer exits. The Barber goes back to sleep if there are no additional customers waiting to be serviced. If a customer arrives and the Barber is busy with another customer, the arriving customer must wait. No more than a fixed number of customers (say n) can be waiting. If n customers already are waiting, the next arriving customer exits.

What does my program do?

Your program will use threads to simulate the barber and customers. There will be one Barber thread and m Customers threads (m should be larger than n). The activities of the Barber and Customer threads must be synchronized to simulate the conditions of the problem. In addition, the threads must avoid race conditions. The semaphore solution in Figure 2-36 handles this. In addition, your program must provide output that convinces someone that it solves the Sleeping Barber Problem (see below).

Input to your program is the number of customers, m, and number of chairs, n. The variables m and n can be set at runtime or compile time. Your program should print out what each thread is doing. For example, a partial print out might look like the following:

Barber: I'm going to sleep.

Customer 0: I'm waiting for a hair cut.

Barber: I am ready to cut hair.

Customer 0: I'm having my hair cut.

Customer 0: I'm finished. Bye!


Customer 3: Too Crowded. I'm leaving. Bye!


Barber: I'm going to sleep.

What do I hand in?

(1) A hard copy of the code along with the output.

(2) A report that describes your program in detail and any of its special capabilities. Your report must discuss the output messages produced by your program.

(3) e-mail copy of the code.

When is the project due?

The due date for the project is Tuesday, December 3. No projects will be accepted after that date.

Choice of Language

As mentioned previously, you can do the project in either C, C++ or Java.