Saturday, May 30, 2015

MULTIPLE SLEEPING BARBERS - MULTIPROCESSOR OPERATING SYSTEMS

AIM:

To write a java program for the multiple sleeping barbers in multi-processor operating system.

ALGORITHM:


Step 1:              Start the program

Step 2:              while the barber in process is true runs an infinite loop

Step 3:              Barber tries to acquire a customer.

Step 4:              If none is available the barber goes to sleep.

Step 5:              At this time, he has been awakened and wants to modify the number of available seats.

Step 6:              Barber waits for a customer to arrive

Step 7:              If customer arrived run an infinite loop.

Step 8:              Checks if the NumberOfFreeSeats is greater than zero, sit down on the chair. Otherwise goto step 13.

Step 9:              V(Customers) notify the barber, who's waiting that there is a customer

Step 10:          V(accessSeats) don't need to lock the chairs anymore

Step 11:          P(Barber) now it's this customers turn, but wait if the barber is busy

Step 12:          Here the customer is having his hair cut

Step 13:          V(accessSeats) The lock on the seats is removed. Customer leaves without a haircut.

Step 14:          End of program.



PROGRAM:

import java.io.*;
import java.lang.*;
class cust {
    public int disp(int cn)
    {
        return(cn);
    }

}
class em1 extends Thread{
    main2 m=new main2();
    cust c=new cust();
public synchronized void run()
{
    try
    {
    while(m.cnum<=m.n)
    {
        int t=c.disp(m.cnum++);
    System.out.println("Barber2 serves Customer "+t);
        Thread.sleep(2000);
    }
    System.out.println("Barber2 sleeps ");
  
   }
    catch(Exception e){}
}
   
}
public class main2 {
static int cnum=1,n,ch,n1;
 
    public static void  main(String[] args) {
        try
        {
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
         em1 e=new em1();
         cust c=new cust();             
         int j;

             System.out.println("Enter no of Chairs including two barber Chairs: ");
         ch=Integer.parseInt(br.readLine());

             System.out.println("Enter no of Customers : ");
         n=Integer.parseInt(br.readLine());

         e.start();


             if(ch<n)
         {
             n1=n-ch;
             System.out.println(n1+" Customers Leaved from the Shop");         
             n=n-n1;
             while(cnum<=n)
         {
         int t=c.disp(cnum++);
         System.out.println("Barber1 serves " +" Customer " + t);
         Thread.sleep(1000);
             }
             }
            else
             {
         while(cnum<=n)
         {
         int t=c.disp(cnum++);
         System.out.println("Barber1 serves " +" Customer " + t);
         Thread.sleep(1000);
         }
             }
             System.out.println("Barber1 sleeps ");
        
         }
        catch(Exception e){}
    }

}

OUTPUT:


U:\OS_Lab\EX123>javac main2.java

U:\OS_Lab\EX123>java main2
Enter no of Chairs including two barber Chairs:5
Enter no of Customers :4

Barber1 serves  Customer 1
Barber2 serves Customer 2
Barber1 serves  Customer 3
Barber2 sleeps
Barber1 serves  Customer 4
Barber1 sleeps



U:\OS_Lab\EX123>java main2
Enter no of Chairs including two barber Chairs:4
Enter no of Customers :4

Barber1 serves  Customer 1
Barber2 serves Customer 2
Barber1 serves  Customer 3
Barber2 sleeps
Barber1 serves  Customer 4
Barber1 sleeps



U:\OS_Lab\EX123>java main2
Enter no of Chairs including two barber Chairs:3
Enter no of Customers :4

1 Customers Leaved from the Shop
Barber2 serves Customer 1
Barber1 serves  Customer 2
Barber1 serves  Customer 3
Barber2 sleeps
Barber1 sleeps

No comments:

Post a Comment