The document provides code for three programs - Semaphore, SleepingBarber, and SleepingBarberD - that simulate the sleeping barber problem using threads and semaphores. SleepingBarberD is identical to SleepingBarber except that it adds delays for customers to simulate potential wait times. The code defines classes for semaphores, barbers, and customers and includes run methods that define the thread behavior for each.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
257 views7 pages
Sleeping Barber
The document provides code for three programs - Semaphore, SleepingBarber, and SleepingBarberD - that simulate the sleeping barber problem using threads and semaphores. SleepingBarberD is identical to SleepingBarber except that it adds delays for customers to simulate potential wait times. The code defines classes for semaphores, barbers, and customers and includes run methods that define the thread behavior for each.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
3.
Sleeping Barber - code
Here are the programs for Semaphore, SleepingBarber and SleepingBarberD described before. SleepingBarberD differs only by the addition of delay to the customers.
Semaphore.java----------------------------------- // This count implements a classic counting Semaphore // It has two synchronized operations which it can perform // up - increments the count of the semaphore and wakes up // waiting threads // down - decrements the count of the semaphore or if zero // suspends the thread
class Semaphore extends Object { private int count;
public Semaphore(int startingCount){ count = startingCount; }
public void down(){ synchronized (this) { while (count = 0) { // We must wait try { wait(); } catch (InterruptedException ex) { // I was interupted, continue onwards } }
// We can decrement the count count--; }
}
public void up(){ synchronized (this) { count++; //notify a waiting thread to wakeup if (count == 1 ) { notify(); } } } } End of Semaphore.java
// Shared objects // Number of customers waiting for service public static Semaphore customers = new Semaphore(0);
// Number of barbers waiting for customers public static Semaphore barbers = new Semaphore(0);
// For mutual exclusion public static Semaphore mutex = new Semaphore(1);
// Customers are waiting (not being cut) public static int waiting = 0;
// Chairs for waiting customers public static final int CHAIRS = 5;
class Barber extends Thread { private int myNumber; // Id for the Barber
public Barber(int i) { // Constructor for the Barber myNumber = i; }
public void run(){ // What a barber does while(true) { customers.down(); // Go to sleep if no customers mutex.down(); // Acquire access to waiting waiting = waiting - 1; // Decrement count of waiting // customers barbers.up(); // One barber is now ready to cut // hair mutex.up(); // Release waiting cut_hair(); // Noncritical region } }
public void cut_hair(){ System.out.println("Barber " + myNumber + " is cutting hair"); try { sleep(7500); } catch (InterruptedException ex){ } } } //end of Barber Class
private class Customer extends Thread { private int myNumber; // Id for the Customer
public Customer(int i) { // Constructor for the Customer myNumber = i; }
public void run(){ // What a customer does mutex.down(); // Acquire access to waiting if(waiting CHAIRS){ waiting = waiting + 1; // Increment count of waiting // customers customers.up(); // Wake up barber if needed mutex.up(); // Release waiting barbers.down(); // Go to sleep if number of free // barbers is 0 get_haircut(); // Noncritical region } else { mutex.up(); // Shop is full do not wait } }
public void get_haircut(){ System.out.println("Customer " + myNumber + " is getting his hair cut"); try { sleep(10000); } catch (InterruptedException ex){ } } } //end of Customer Class
public static void main(String args[]) {
SleepingBarber holder = new SleepingBarber();
holder.start();
}
// This thread spins off a number of customers public void run(){
final int BARBERS = 3;
Barber aBarber; Customer aCustomer;
for(int i=0; iBARBERS; i++) { // Create the barbers aBarber = new Barber(i);
// Start the threads running aBarber.start(); }
int customerNumber = 0; while(true){ aCustomer = new Customer(customerNumber++);
// Start the customer running aCustomer.start();
// Wait a bit and make another customer try { sleep(1000); } catch(InterruptedException ex) {}; } } //end of run method for SleepingBarber
} End of SleepingBarber.java---------------------------------------
// Shared objects // Number of customers waiting for service public static Semaphore customers = new Semaphore(0);
// Number of barbers waiting for customers public static Semaphore barbers = new Semaphore(0);
// For mutual exclusion public static Semaphore mutex = new Semaphore(1);
// Customers are waiting (not being cut) public static int waiting = 0;
// Chairs for waiting customers public static final int CHAIRS = 5;
class Barber extends Thread { private int myNumber; // Id for the Barber
public Barber(int i) { // Constructor for the Barber myNumber = i; }
public void run(){ // What a barber does while(true) { customers.down(); // Go to sleep if no customers mutex.down(); // Acquire access to waiting waiting = waiting - 1; // Decrement count of waiting // customers barbers.up(); // One barber is now ready to cut // hair mutex.up(); // Release waiting cut_hair(); // Noncritical region } }
public void cut_hair(){ System.out.println("Barber " + myNumber + " is cutting hair"); try { sleep(7500); } catch (InterruptedException ex){ } } } //end of Barber Class
private class Customer extends Thread { private int myNumber; // Id for the Customer
public Customer(int i) { // Constructor for the Customer myNumber = i; }
public void run(){ // What a customer does mutex.down(); // Acquire access to waiting if(waiting CHAIRS){ waiting = waiting + 1; // Increment count of waiting // customers customers.up(); // Wake up barber if needed mutex.up(); // Release waiting
barbers.down(); // Go to sleep if number of free // barbers is 0 get_haircut(); // Noncritical region } else { mutex.up(); // Shop is full do not wait } }
public void get_haircut(){ System.out.println("Customer " + myNumber + " is getting his hair cut"); try { sleep(10000); } catch (InterruptedException ex){ } } } //end of Customer Class
public static void main(String args[]) {
SleepingBarberD holder = new SleepingBarberD();
holder.start();
}
// This thread spins off a number of customers public void run(){
final int BARBERS = 3;
Barber aBarber; Customer aCustomer;
for(int i=0; i // Create the barbers aBarber = new Barber(i);
// Start the threads running aBarber.start(); }
int customerNumber = 0; while(true){ aCustomer = new Customer(customerNumber++);
// Start the customer running aCustomer.start();
// Wait a bit and make another customer try { sleep(1000); } catch(InterruptedException ex) {}; } } //end of run method for SleepingBarber