Operating System代写:CS4118 Sleeping Barber Problem

代写多线程程序,解决Sleeping Barber问题。

Requirement

Write a multithreaded program practice that solves the sleeping barber problem as described below.

The barber shop has one barber, one barber chair, and n chairs for waiting customers, if any, to sit on. If there are no customers present, the barber sits down in the barber chair and falls asleep. When a customer arrives and the barber is sleeping, s/he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer’s hair, they either sit down (if there are empty chairs) or wait for the empty chairs (if all chairs are full). The problem is to program the barber and the customers without getting into race conditions.

Your program must prompt user for the number of customers and the number of chairs in the waiting room. You can assume that the maximum number of customers is 25. A sample output is as below (the user input is in red color)

Sample output:

Maximum number of customers can only be 25. Enter number of customers and chairs.

6 3

A solution to the sleeping barber problem using semaphores.

The barber is sleeping
Customer 3 leaving for barber shop.
Customer 1 leaving for barber shop.
Customer 2 leaving for barber shop. 
Customer 0 leaving for barber shop. 
Customer 4 leaving for barber shop. 
Customer 5 leaving for barber shop. 
Customer 1 arrived at barber shop. 
Customer 1 entering waiting room.
Customer 1 waking the barber.
Customer 4 arrived at barber shop.
Customer 4 entering waiting room. 
Customer 2 arrived at barber shop.
Customer 2 entering waiting room. 
Customer 5 arrived at barber shop.
Customer 5 entering waiting room.
The barber is cutting hair
Customer 3 arrived at barber shop.
Customer 0 arrived at barber shop. 
The barber has finished cutting hair. 
The barber is sleeping
Customer 1 leaving barber shop. 
Customer 4 waking the barber.
Customer 3 entering waiting room.
The barber is cutting hair
The barber has finished cutting hair. 
The barber is sleeping
Customer 4 leaving barber shop. 
Customer 2 waking the barber.
Customer 0 entering waiting room. 
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 2 leaving barber shop.
Customer 5 waking the barber.
The barber is cutting hair
The barber has finished cutting hair. 
The barber is sleeping
Customer 5 leaving barber shop. 
Customer 3 waking the barber.
The barber is cutting hair
The barber has finished cutting hair.
The barber is sleeping
Customer 3 leaving barber shop. 
Customer 0 waking the barber.
The barber is cutting hair
The barber has finished cutting hair. 
The barber is sleeping
Customer 0 leaving barber shop.
The barber is going home for the day.