Monitor implementation of the Sleeping-Barber Problem:
type sleeping_barber = monitor
var number_of_customer: integer;
var x_barber: condition;
var x_customer: condition;
procedure entry get_customer()
begin
if (number_of_customer = 0)
x_barber.wait; /* the barber has to wait for customers */
number_of_customers := number_of_customers - 1;
x_customer.signal; /* inform the customer that the barber is ready */
end
procedure entry haircut()
begin
number_of_customer := number_of_customer + 1;
x_barber.signal; /* inform the barber that a customer is in */
x_customer.wait; /* wait for the barber */
do_haircut();
end
begin /*initialization*/
number_of_customer = 0;
end
end /* monitor */
sb: monitor sleeping_barber;
Barber code:
while (true)
begin
sb.get_customer();
cut_hair();
end;
customer code:
hair_cut();