Sunday, June 16, 2013
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation of predetermined maximum possible amount of all resources, and then makes a check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue. Here we have 3 input matrix where
max[][] - denotes maximum resources that will be required by processes
allocate[][] - denotes currently allocated resources to processes
avail[][] - denotes resources available in the system
We will calculate need matrix and then try to allocate. I f we can allocate safely then give a suitable message.

--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Bankers{
    private int need[][],allocate[][],max[][],avail[][],np,nr;
    
    private void input(){
     Scanner sc=new Scanner(System.in);
     System.out.print("Enter no. of processes and resources : ");
     np=sc.nextInt();  //no. of process
     nr=sc.nextInt();  //no. of resources
     need=new int[np][nr];  //initializing arrays
     max=new int[np][nr];
     allocate=new int[np][nr];
     avail=new int[1][nr];
     
     System.out.println("Enter allocation matrix -->");
     for(int i=0;i<np;i++)
          for(int j=0;j<nr;j++)
         allocate[i][j]=sc.nextInt();  //allocation matrix
      
     System.out.println("Enter max matrix -->");
     for(int i=0;i<np;i++)
          for(int j=0;j<nr;j++)
         max[i][j]=sc.nextInt();  //max matrix
      
        System.out.println("Enter available matrix -->");
        for(int j=0;j<nr;j++)
         avail[0][j]=sc.nextInt();  //available matrix
        
        sc.close();
    }
    
    private int[][] calc_need(){
       for(int i=0;i<np;i++)
         for(int j=0;j<nr;j++)  //calculating need matrix
          need[i][j]=max[i][j]-allocate[i][j];
       
       return need;
    }
 
    private boolean check(int i){
       //checking if all resources for ith process can be allocated
       for(int j=0;j<nr;j++) 
       if(avail[0][j]<need[i][j])
          return false;
   
    return true;
    }

    public void isSafe(){
       input();
       calc_need();
       boolean done[]=new boolean[np];
       int j=0;

       while(j<np){  //until all process allocated
       boolean allocated=false;
       for(int i=0;i<np;i++)
        if(!done[i] && check(i)){  //trying to allocate
            for(int k=0;k<nr;k++)
            avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
         System.out.println("Allocated process : "+i);
         allocated=done[i]=true;
               j++;
             }
          if(!allocated) break;  //if no allocation
       }
       if(j==np)  //if all processes are allocated
        System.out.println("\nSafely allocated");
       else
        System.out.println("All proceess cant be allocated safely");
    }
    
    public static void main(String[] args) {
       new Bankers().isSafe();
    }
}
--------------------------------------------------------------------------------------------------------------------------
Output
--------------------------------------------------------------------------------------------------------------------------
Enter no. of processes and resources : 3 4
Enter allocation matrix -->
1 2 2 1
1 0 3 3
1 2 1 0
Enter max matrix -->
3 3 2 2
1 1 3 4
1 3 5 0
Enter available matrix -->
3 1 1 2
Allocated process : 0
Allocated process : 1
Allocated process : 2
Safely allocated
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------

24 comments:

  1. i need code for deadlock prevention

    ReplyDelete
  2. Is it any different if you have to read the input from a text file instead of the concole??
    Please help.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. u can add this
      public MaTrix(String matran) throws FileNotFoundException, IOException {
      Path path = Paths.get(new File("src/client/").getAbsolutePath(), matrix);
      String temp, token[];
      File file = path.toFile();
      if (Files.exists(path)) {
      BufferedReader in = new BufferedReader(new FileReader(file));
      temp = in.readLine();
      token = temp.split(" ");
      m = Integer.parseInt(token[0]);
      n = Integer.parseInt(token[1]);

      maTran = new double[m][n];
      for (int i = 0; i < m && (temp = in.readLine()) != null; i++) {
      token = temp.split(" ");
      for (int j = 0; j < token.length && j < n; j++) {
      maTrix[i][j] = Double.parseDouble(token[j]);
      }
      }
      } else {
      System.out.println("File " + matrix+ " khong ton tai");
      }
      }

      Delete
    3. how to display sequence of processes being executed?

      Delete
    4. This comment has been removed by the author.

      Delete
    5. I need help too. to read a file. The coding in comment have errors.

      Delete
  3. Umm, Can you please add more sample output? Preferably, one that has a deadlock or unsafe?

    ReplyDelete
  4. how would you implement this algorithm if you have a text file as an input like this


    5
    3
    10 5 7
    0 1 0 7 5 3
    2 0 0 3 2 2
    3 0 2 9 0 2
    2 1 1 2 2 2
    0 0 2 4 3 3


    Which means there are 5 processes, 3 resource types. The first resource type has 10 units, second one has 5 unites, third one has 7 units. The following line means the first process has a allocation of resource (0, 1, 0) and the max need is (7, 5, 3). Then following lines are the similar settings for process 2 unitl to process m.

    ReplyDelete
  5. TEA-80 Saurabh
    Thankyou sir for the readymade code

    ReplyDelete
  6. avail [0] [ k] = lịch phát sóng [0] [k] -need [i] [k] + max [i] [k];
    -->>> I do not understand this it please explain to me

    ReplyDelete
  7. avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
    I do not understand this it please explain to me

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  8. Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.
    Java编程代写

    ReplyDelete
  9. Sir ,If we want to calculate available matrix in program itself then how it can be done .

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  10. Sir ,If we want to calculate available matrix in program itself then how it can be done .

    ReplyDelete
  11. avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
    I do not understand this it please explain to me

    ReplyDelete

Total Pageviews

Followers


Labels

Popular Posts

free counters