Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program to implement the Banker\'s deadlock avoidance algorithm for

ID: 3773721 • Letter: W

Question

Write a Java program to implement the Banker's deadlock avoidance algorithm for a system that has more than one instance per resource type. The algorithm will have as an input a resource-allocation status given as the values of the Allocation, Max, and Available matrices.

When presented with a new request at the command prompt, the program should run the Resource-Request algorithm, which in turn calls the Safety algorithm. The program should state whether it is safe or unsafe to grant the given request.

The input to the program would be three text files that might consist of multiple lines:

• The first text file (allocation.txt) has a line for each process where each line contains the number of instances per resource type currently allocated to that process separated by tabs.

• The second text file (max.txt) has a line for each process where each line contains the maximum number of instances per resource type, separated by tabs, that this process might request.

• The third text file (available.txt) has one line containing the current available (unallocated) number of instances per resource type.

At the command prompt, the user should be able to enter a new request by specifying the process number (starting at process 0) and how many instances per resource type (starting at resource type 0) it is requesting.

The program will be tested using 5 test cases.

Explanation / Answer

JAVA 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(" Safely allocated");

       else

        System.out.println("All proceess cant be allocated safely");

    }

     

    public static void main(String[] args) {

       new Bankers().isSafe();

    }

}

Out put:

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote