Sleepy barber problem

A barber works in his salon. The salon has a waiting room with N seats where customers wait, and a work area with a special chair where the barber receives customers (one by one) and works on their hair.

In the morning, the barber opens the door of the salon and performs the following cyclic activity until the end of his working hours. He sits down on a chair and sleeps until he is either awakened by the arrival of a customer or his alarm clock rings when his working hours are over. When a customer arrives, he takes him and works on his hair. Then, as more customers arrive, he takes them in turn. When the waiting room is empty, he sits back in the chair and sleeps. When there are customers in the waiting room, the barber is not allowed to sleep.

When the salon is open, the new customer enters the waiting room. If there are no empty seats, i.e. N customers are already waiting in the waiting room, the customer leaves without a haircut. If there are empty chairs, he sits down and signals his arrival to the barber. If the barber invites him, he comes and sits on the barber's chair and waits until his haircut is ready. Then he leaves the salon. If the salon is not open, the customer will not enter the salon and leave.

Assignment

Simulate the presented system with a barber process and several customer processes, dynamically generated by the initial main process. The initial process also signals the end of working time, after which no new customers will be accepted.

Proposal of a possible solution (the solution doesn't have to look like this)

process Barber()
    open salon - put sign OPEN
    loop
        if working_hours == FALSE
            put sign CLOSED

        if customers_waiting > 0
            call next customer to move to barber's chair
            wait for him to sit
            start on his hair //simulate with sleep
            signal to the customer that his hair is done and that he can leave
            wait for him to leave

        if no customers waiting and working_hours == TRUE
            sit in barber's chair
            sleep until a new customer arrives

        if no customers waiting and working_hours == FALSE
            close salon
            leave

process Customer(id)
    if sign OPEN is set
        enter the waiting room
        if customers_waiting < N
            take a seat on empty chair (customers_waiting++)
            signal to barber
            wait to be called
            sit on barber's char (also customers_waiting--)
            signal readiness for haircut
            wait until haircut is done
            get out of barber's char
            signal to barber that you left
            leave salon
        else
            exit waiting room
            leave
    else
        leave

proces main()
    working_hours = TRUE
    create process Barber()
    while simulated time < end of working hours
        sleep some (random) time //e.g. 1-3 seconds
        create process Customer(i++)

    //signal Barber that working time is over
    working_hours = FALSE

    //create a few more Customers just to demonstrate
    //that they will not enter salon

    wait for Barber's process to exit

Use of variables shared between processes (i.e. sign state, working_hours, customers_waiting) must be protected by semaphores from simultaneous use.

Example for program output, waiting room with three chairs

Barber: Opening salon
Barber: Put sign OPEN
Barber: Sleeping
	Customer(0): Arrived
	Customer(0): Entering waiting room (1)
Barber: Awaken
Barber: Calling customer
	Customer(0): Moved to barber's chair
Barber: Starting a haircut
	Customer(1): Arrived
	Customer(1): Entering waiting room (1)
	Customer(2): Arrived
	Customer(2): Entering waiting room (2)
	Customer(3): Arrived
	Customer(3): Entering waiting room (3)
	Customer(4): Arrived
	Customer(4): Waiting room full, leaving
Barber: Haircut completed
	Customer(0): Got haircut, leaving
Barber: Calling customer
	Customer(1): Moved to barber's chair
Barber: Starting a haircut
...
main: announcing end of working hours
//assuming Barber is working on Customer(25) and one more waits
Barber: Haircut completed
	Customer(25): Got haircut, leaving
Barber: Put sign CLOSED
Barber: Calling customer
	Customer(26): Moved to barber's chair
Barber: Starting a haircut
	Customer(27): Arrived
	Customer(27): Leaving, salon is closed
Barber: Haircut completed
	Customer(26): Got haircut, leaving
Barber: Closing salon
------
Note: for simplicity of problem description all participants are named as males. However, you can use any gender you like.