Operating System TheSleepingBarberProblem A barbershop consi

Operating System:

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 callInCustomer()

In BarberRoom.java

            synchronized void cutMyHair()

            synchronized void work()

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.

Your code must print similar outputs as the demo program (use printChairStatus() in WaitingRoom.java to print status of the wait set in waiting room).

SUBMISSION

Modified WaitingRoom.java and BarberRoom.java only in a zipped folder. IMPORTANT: Unzipping the zip file must create a folder with the two java files in it.

USING the DEMO jar file

            java –cp barbershop.jar BarberShop 5 (you will have to type this out rather than copying and pasting), where 5 is the total number of seats in the waiting room.

Link for java files:

http://www.megafileupload.com/80ux/bshop.zip

Solution

import java.util.concurrent.*;

public class SleepingBarber extends Thread {

    public static Semaphore customers = new Semaphore(0);

    public static Semaphore barber = new Semaphore(0);

    public static Semaphore accessSeats = new Semaphore(1);

    public static final int CHAIRS = 5;

   public static int numberOfFreeSeats = CHAIRS;

  

/* THE CUSTOMER THREAD */

class Customer extends Thread {

int iD;

boolean notCut=true;

/* Constructor for the Customer */

   

public Customer(int i) {

    iD = i;

}

public void run() {  

    while (notCut) { // as long as the customer is not cut

      try {

      accessSeats.acquire(); //tries to get access to the chairs

      if (numberOfFreeSeats > 0) { //if there are any free seats

        System.out.println(\"Customer \" + this.iD + \" just sat down.\");

        numberOfFreeSeats--; //sitting down on a chair

        customers.release(); //notify the barber that there is a customer

        accessSeats.release(); // don\'t need to lock the chairs anymore

        try {

                barber.acquire(); // now it\'s this customers turn but we have to wait if the barber is busy

        notCut = false; // this customer will now leave after the procedure

        this.get_haircut(); //cutting...

        } catch (InterruptedException ex) {}

      }  

      else { // there are no free seats

        System.out.println(\"There are no free seats. Customer \" + this.iD + \" has left the barbershop.\");

        accessSeats.release(); //release the lock on the seats

        notCut=false; // the customer will leave since there are no spots in the queue left.

      }

     }

      catch (InterruptedException ex) {}

    }

}

/* this method will simulate getting a hair-cut */

public void get_haircut(){

    System.out.println(\"Customer \" + this.iD + \" is getting his hair cut\");

    try {

    sleep(5050);

    } catch (InterruptedException ex) {}

}

}

/* THE BARBER THREAD */

class Barber extends Thread {

public Barber() {}

public void run() {

    while(true) { // runs in an infinite loop

      try {

      customers.acquire(); // tries to acquire a customer - if none is available he goes to sleep

      accessSeats.release(); // at this time he has been awaken -> want to modify the number of available seats

        numberOfFreeSeats++; // one chair gets free

      barber.release(); // the barber is ready to cut

      accessSeats.release(); // we don\'t need the lock on the chairs anymore

      this.cutHair(); //cutting...

    } catch (InterruptedException ex) {}

    }

}

    /* this method will simulate cutting hair */

  

public void cutHair(){

    System.out.println(\"The barber is cutting hair\");

    try {

      sleep(5000);

    } catch (InterruptedException ex){ }

}

}      

/* main method */

public static void main(String args[]) {

   

    SleepingBarber barberShop = new SleepingBarber(); //Creates a new barbershop

    barberShop.start(); // Let the simulation begin

}

public void run(){  

   Barber giovanni = new Barber(); //Giovanni is the best barber ever

   giovanni.start(); //Ready for another day of work

   /* This method will create new customers for a while */

   

   for (int i=1; i<16; i++) {

     Customer aCustomer = new Customer(i);

     aCustomer.start();

     try {

       sleep(2000);

     } catch(InterruptedException ex) {};

   }

}

}

Operating System: TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no cust
Operating System: TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no cust
Operating System: TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no cust
Operating System: TheSleeping-BarberProblem. A barbershop consists of a waiting room with n chairs and a barber room with one barber chair. If there are no cust

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site