Barbershop – First Attempt
(* A
barbershop requires synchronization similar to the synchronization necessary in
a computer. In this example there are 50 customers, 3
barbers and one cashier. Only
20 are
allowed in the barbershop at one time. There
is a sofa for convenience that
seats only 4
customers. The problem with this
implementation is when a haircut is
finished, the wrong
customer could be signaled to leave which would lead to two
customers sitting in
the barber chair – perhaps one sitting on the other’s lap. *)
program
barbershop_1;
var max_capacity, sofa, barber_chair, cust_ready, finished, leave_b_chair, payment, receipt,
receipt, coord : semaphore;
procedure customer; procedure barber; procedure cashier;
begin repeat repeat
wait(max_capacity);
enter shop; wait(cust_ready); wait(payment);
wait(sofa); wait(coord); wait(coord);
sit on
sofa; cut hair; accept pay;
wait(barber_chair); signal(coord); signal(coord);
get up from sofa; signal(finished); signal(receipt);
signal(sofa); wait(leave_b_chair);
sit in barber chair; signal(barber_chair); until forever;
signal(cust_ready);
wait(finished); until forever;
leave
barber chair;
signal(leave_b_chair);
pay;
signal(payment);
wait(receipt);
exit shop;
signal(max_capacity);
end;
begin (* main program executable code *)
max_capacity := 20; sofa := 4; barber_chair := 3; cust_ready := 0; finished := 0;
leave_b_chair := 0; payment := 0 receipt := 0; coord := 3; (* semaphore initializations *)
cobegin
customer; customer; customer … (* 50 times – or 50 customers *)
barber; barber; barber (* 3 barbers *)
cashier; cashier (* 2 cashiers *)
coend;
end.