Pages

Ads 468x60px

Sunday, July 22, 2012

Java: Banker Algorithm


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BankerUI extends JFrame
{
int i,j,count=0,get=0,final_pro=0; // all variables that have global scope within the BankerUI class
    int process  = 0;
    int resource = 0;
    int needed[][]    = new int[0][0];
    int allocated[][] = new int[0][0];
int eligible[]    = new int[0];
int total[]       = new int[0];
int free[]        = new int[0];
int sum[]         = new int[0];
    int ans[]         = new int[0];
    JTextField t_res[] =  new JTextField[0];
    JTextField t_ans[] =  new JTextField[0];

  JButton next  = new JButton("NEXT");
   JButton needs = new JButton("NEXT");
   
JTextField process_field  = new JTextField();
JTextField resource_field = new JTextField();
JTextField text_fields[][] = new JTextField[0][0];

BankerUI() // first frame in the constructor
{
    JPanel main_panel   = new JPanel();
    JPanel button_panel = new JPanel();
     JLabel   proces     = new JLabel("Enter Number of Processes");
     JLabel   resourc    = new JLabel("Enter Number of Resources");

     this.setLayout(new GridLayout(2,1));
 
     process_field.setPreferredSize(new Dimension(20,20));
     resource_field.setPreferredSize(new Dimension(20,20));
     main_panel.setLayout(new GridLayout(2,2));
     main_panel.add(proces);
     main_panel.add(process_field);
     main_panel.add(resourc);
     main_panel.add(resource_field);
     button_panel.setLayout(new FlowLayout());
     button_panel.add(next);
 
     this.add(main_panel);
     this.add(button_panel);
 
     ButtonClass button = new ButtonClass();
     next.addActionListener(button);
}

public static void main(String[] args) // main function
{
    BankerUI main_window = new BankerUI(); //call to first frame
   main_window.setTitle("BANKER ALGORITHM");
   main_window.setSize(350,150);
   main_window.setVisible(true);
   main_window.setLocationRelativeTo(null);
   main_window.setResizable(false);
   main_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
}

class ButtonClass implements ActionListener //1st button action listener class
{
  public void actionPerformed(ActionEvent e)       //action on clicking next button first time
  {
  String pr = process_field.getText(); // get number of processes
  String re = resource_field.getText(); // get number of resources
  try
  {
  process   = Integer.parseInt(pr);
  resource  = Integer.parseInt(re);
  }
  catch (Exception ex)
  { // exception on wrong input
  JOptionPane.showMessageDialog(null,"INPUT EXCEPTION OCCURED");
  System.exit(0);
  }
 
    int process   = Integer.parseInt(pr);
  int resource  = Integer.parseInt(re);
  needed        = new int[process][resource];
  allocated     = new int[process][resource];
eligible      = new int[process];
total         = new int[resource];
free          = new int[resource];
sum           = new int[resource];
    ans           = new int[process];

if (e.getSource()==next)
{
JOptionPane.showMessageDialog(null,"ENTER ALLOCATED RESOURCES");
JFrame  allocate_frame = create_frame(process,resource);   // call to 2nd frame
allocate_frame.setTitle("ALLOCATED RESOURCES");
JPanel  get_need = new JPanel(new FlowLayout());

get_need.add(needs);
allocate_frame.add(get_need,BorderLayout.SOUTH);

      ButtonClass need_button = new ButtonClass();          
  needs.addActionListener(need_button);
}

  else if (e.getSource()==needs)
  {
  String get_pr = "";
    for (i=0; i<process; i++)
  {
  for (j=0; j<resource; j++)
  {
  get_pr = text_fields[i][j].getText();
  try
  {
  allocated[i][j] = Integer.parseInt(get_pr); // get allocated resoureces
  }
  catch (Exception ex)
  { // exception on wrong input
  JOptionPane.showMessageDialog(null,"INPUT EXCEPTION OCCURED");
  System.exit(0);
  }
  }
  }
 
  JOptionPane.showMessageDialog(null,"YOU HAVE ENTERED ALLOCATED RESOURCES SUCCESSFULLY");
  JOptionPane.showMessageDialog(null,"ENTER NEEDED RESOURCES");
JFrame  need_frame = create_frame(process,resource);     // call to 3rd frame
need_frame.setTitle("NEEDED RESOURCES");
JPanel  get_result = new JPanel(new FlowLayout());
JButton answer = new JButton("NEXT");
get_result.add(answer);
need_frame.add(get_result,BorderLayout.SOUTH);

AnswerClass answ = new AnswerClass();
answer.addActionListener(answ);
}
  }
}

public class AnswerClass implements ActionListener                 // 2nd button action listener class
{
  public void actionPerformed(ActionEvent e)
  {
  JOptionPane.showMessageDialog(null,"YOU HAVE ENTERED NEEDED RESOURCES SUCCESSFULLY");
  JFrame total_resc = t_resource("TOTAL RESOURCES");
 
  String get_re = "";
    for (i=0; i<process; i++)
  {
  for (j=0; j<resource; j++)
  {
  get_re = text_fields[i][j].getText();
  try
  {
  needed[i][j] = Integer.parseInt(get_re);       // get needed resources
  }
  catch (Exception ex)
  { // exception on wrong input
  JOptionPane.showMessageDialog(null,"INPUT EXCEPTION OCCURED");
  System.exit(0);
  }
  }
  }
  }
 
  public JFrame t_resource(String label) //  function of frame to get total resources
  {
     JFrame main_total = new JFrame();
   main_total.setTitle(label);
   main_total.setSize(500,100);
   main_total.setVisible(true);
   main_total.setLocationRelativeTo(null);
   main_total.setResizable(false);
   main_total.setLayout(new BorderLayout());
   main_total.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
   t_res =  new JTextField[resource];
 
   for (i=0; i<resource; i++)
   {
    t_res[i] = new JTextField();
   }
 
  JPanel t_panel = new JPanel(new GridLayout(1,resource+1));
  JPanel b_panel = new JPanel(new FlowLayout());
  JButton b_resc = new JButton("RESULT");
  b_panel.add(b_resc);

  MainClass main_obj = new MainClass();
  b_resc.addActionListener(main_obj);

   for (i=0; i<resource; i++)
   {
    if (i==0)
    {
    t_panel.add(new JLabel("TOTAL RESOURCES"));
    }
    t_panel.add(t_res[i]);
   }
 main_total.add(t_panel,"North");
 main_total.add(b_panel,"Center");

  return main_total;
  }
 
 
  public class MainClass implements ActionListener    // result button listener class
{
  public void actionPerformed(ActionEvent e)
  {
  String total_recs = "";
  for (i=0; i<resource; i++)
  {
  try
  {
  total_recs = t_res[i].getText(); // get total resources
  total[i]   = Integer.parseInt(total_recs);
  }
  catch (Exception ex)
  { // exception on wrong input
  JOptionPane.showMessageDialog(null,"INPUT EXCEPTION OCCURED");
  System.exit(0);
  }
  }
  for (i=0; i<resource; i++)
    {
    for (j=0; j<process; j++)
    { //calculate totoal allocated resources left by processes
    sum[i] += allocated[j][i];
    }
    }
   
    for (i=0; i<resource; i++)
    {
    free[i] = total[i] - sum[i];        // calculate available free resources
    }
   
    int inc = 0;
    String ren = "";
    for (int k=0; k<process; k++)
    {
    get = main_func(needed,free,eligible);
    if (get==-1)
{
  for (i=0; i<ans.length; i++)
  {
  if(ans[i]!=0)
  {
  ren = ren  + ren.valueOf(ans[i]) + ".";
  }
  }
  JOptionPane.showMessageDialog(null,"PROCESS, " + ren + "Then there is a deadlock");
  System.exit(0);
  }
ans[inc]=get;
inc++;

    for (i=0; i<process; i++)
    {
  for (j=0; j<resource; j++)
    {
    if (i==get-1)
  {
    free[j] = free[j] - needed[i][j];
    free[j] = free[j] + allocated[i][j] + needed[i][j];
    needed[i][j] = total[j]+1;
    eligible[i] = -1;
    }
    }
    }
    }
    String collect = "";
    for (i=0; i<process; i++)
    {
    collect = collect  + collect.valueOf(ans[i]) + ".";
    }
    JOptionPane.showMessageDialog(null,"SAVE SEQUENCE OF PROCESSES " + collect);
    System.exit(0);
}

   public int main_func(int needed[][],int free[],int eligible[])      // main logical function of algo
   {
    final_pro=0;
    int ret = 0;
    int total_count=0;
   
    for (i=0; i<process; i++)
    {  
    int count=0;
    for (j=0; j<resource; j++)
    {
    if (free[j]>=needed[i][j])
    {
    count++;
    }
    }
    if (count==resource)
    {    
    for (j=0 ; j<resource; j++)
    {
    text_fields[i][j].setBackground(Color.BLUE);
    }
    try
    {
    Thread.sleep(1000);
    }
    catch (Exception ex)
    {
   
    }
    eligible[i] = i;
    ret=i;
    break;    
   
    }
    else
    {
    total_count++;
    }
    }
 
    if (total_count==process)
    {
    return -1;
    }
    else
    {
    return ret+1;
    }
    }
}  
}

public JFrame create_frame(int process,int resource)
{ // function to make frame of allocated and needed resources
    JFrame allocated_window  = new JFrame();
    allocated_window .setSize(500,200);
   allocated_window .setVisible(true);
   allocated_window .setLocation(0,100);
   allocated_window .setResizable(false);
   allocated_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
   text_fields = new JTextField[process][resource];
   JPanel name = new JPanel(new GridLayout(process,resource+1));
   JPanel col = new JPanel(new GridLayout(1,resource));
   JPanel allocate = new JPanel(new BorderLayout());

   String s = "";
   String t = "";
   for (i=0; i<resource; i++)
   {
    t = t.valueOf(i+1);
    if (i==0)
    {
    col.add(new JLabel("        " ));
    }
    col.add(new JLabel("Resource " + t));
   }
     
   for (i=0 ; i<process; i++)
   {
    for (j=0 ; j<resource; j++)
    {
    text_fields[i][j] = new JTextField();
    }
   }
   for (i=0; i<process; i++)
   {
s = s.valueOf(i+1);
    name.add(new JLabel("Process " + s));
   
    for (j=0; j<resource; j++)
    {    
    name.add(text_fields[i][j]);
    }
   }
   allocate.add(name,"Center");
   allocated_window.setLayout(new BorderLayout());
   allocated_window .add(col,"North");
   allocated_window .add(allocate,"Center");
 
   return allocated_window;
}
}

No comments:

Post a Comment