int freeSeats = n; sem waitingRoom = 1; // mutex for the waiting room counter and the priority customer counter sem customerReady = 0; // the queue for the waiting customers sem barberReady = 0; // the availability of the barber int waitingPriorityCustomers = 0; // number of waiting priority customers process RegularCustomer[1:m] { while (true) { P(waitingRoom); // acquire lock for waiting room // check if there are free seats, other leave if (freeSeats > 0) { // wait for the barber to be ready freeSeats := freeSeats - 1; // take a seat V(customerReady); // signal to the barber that a customer is waiting V(waitingRoom); // release the look, before we are waiting for the barber to be ready P(barberReady); // wait for the barber to pick up the customer while (waitingPriorityCustomers > 0) { // the barber has the look for the waiting room, therefore it safe to check this variable // release the lock on the room V(barberReady); V(waitingRoom); // we need to free to the lock that we got from the barber, before we wait again P(barberReady); } // customer gets a hair cut } V(waitingRoom); // customer leaves the waiting room and releases the look OR we need to free the lock that we got from the barber } } process PriorityCustomer[1:t] { while (true) { P(waitingRoom); // acquire lock for waiting room // check if there are free seats, otherwise leave if (freeSeats > 0) { // wait for the barber to be ready freeSeats := freeSeats - 1; waitingPriorityCustomers := waitingPriorityCustomers + 1 V(customerReady); // signal to the barber that a customer is waiting V(waitingRoom); // release the look, before we are waiting for the barber to be ready P(barberReady); // wait for the barber to pick up the customer waitingPriorityCustomers := waitingPriorityCustomers - 1 // barber has the look for the waiting room, therefore it safe to manipulate this variable // customer gets a hair cut } V(waitingRoom); // customer leaves the waiting room and releases the look OR we need to free the lock that we got from the barber } } process Barber { while (true) { // sleeps if there a no customers // if customer available then cut hairs P(customerReady); P(waitingRoom); // lock the waiting room freeSeats := freeSeats + 1; V(barberReady); // We don't free the lock but pass it back to the customer // cut the hairs of the customer } }