Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs

ID: 3697104 • Letter: T

Question

TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber Write a program to coordinate the barber and the customers using Java synchronization. For your convenience, I am providing 5 java files (i.e. Barber.java, BarberRoom.java, BarberShop.java, Customer.java, and WaitingRoom.java). You will simply have to implement the following 4 methods. In WaitingRoom.java synchronized boolean proceedToBarberRoom(int id) synchronized void callln Customer0 In BarberRoom.java synchronized void cutMyHairO synchronized void workO For details on what each of the methods should do, refer to the header for each of the methods in respective .java files You will have to read all of the java files carefully to be able to get the big picture. ADDITIONAL CODING REQUIREMENTS You will not use any new variables other than the ones already given in the template .java files. When a customer A is awoken up from the wait set of the Waiting Room to get a hair cut, the seat must be given up immediately. This means that if a new customer comes in before customer A actually gets to have the hair cut, the new customer should be able to use the seat given up by customer A A new customer cannot cut in to get the hair cut when there are other customers already waiting in the wait set. 1. 2. 3. 4. Your code must print similar outputs as the demo program (use printChairStatusO in WaitingRoom.java to print status of the wait set in waiting room).

Explanation / Answer

import Utilities.*; import Synchronization.*; class Barber extends MyObject implements Runnable { private Salon salon = null; public Barber(String name, Salon salon) { super(name); this.salon = salon; new Thread(this).start(); } public void run() { while (true) { salon.wantToCut(); } } } class Customer extends MyObject implements Runnable { private int id = 0; private Salon salon = null; private int cutTime = 0; private int growTime = 0; public Customer(String name, int id, int cutTime, int growTime, Salon salon) { super(name + " " + id); this.id = id; this.salon = salon; this.cutTime = cutTime; this.growTime = growTime; new Thread(this).start(); } public void run() { int napping; while (true) { napping = 1 + (int)random(growTime); System.out.println("age=" + age() + ", " + getName() + " growing hair " + napping + "ms"); nap(napping); System.out.println("age=" + age() + ", " + getName() + " needs a haircut"); salon.wantHairCut(id, cutTime); } } } class Salon extends MyObject { private int numChairs = 0; private CountingSemaphore customers = null; private CountingSemaphore barber = null; private BinarySemaphore mutex = null; private CountingSemaphore cutting = null; private int waiting = 0; public Salon(String name, int numChairs) { super(name); this.numChairs = numChairs; customers = new CountingSemaphore(0); barber = new CountingSemaphore(0); mutex = new BinarySemaphore(1); cutting = new CountingSemaphore(0); } public void wantToCut() { System.out.println("age=" + age() + ", Barber free, waiting for a customer"); P(customers); P(mutex); waiting--; V(barber); System.out.println("age=" + age() + ", Barber has customer, waiting=" + waiting); V(mutex); System.out.println("age=" + age() + ", Barber cutting hair"); P(cutting); } public void wantHairCut(int i, int cutTime) { int napping; P(mutex); if (waiting