Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt file. The file has 7 processes and 5 resources

Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt file. The file has 7 processes and 5 resources however, when the program runs, it doesn't sum the resource columns correctly. The program runs correctly for smaller matricies (4 processes, 3 resources), not sure what the issue is and have been looking over the code for awhile so maybe another set of eyes would help...thanks

BankersAlgorithm.java

/**  * This program implements Bankers algorithm which will determine whether  * the state of the system that is read in from a file is safe or unsafe  * and output the result back to the user.  */ // import the java packages import java.io.*; import java.util.StringTokenizer; import java.util.Scanner; // create a class name bankerAlgorithm public class BankersAlgorithm { //main method public static void main(String[]args)throws IOException,FileNotFoundException, NullPointerException { //Declare local variables int n = 0; // Variable to hold the number of processes int m = 0; // Variable to hold the number of resources int count = 0; // Counter that holds the number of lines in the file int lineCount = 0; // Counter that holds the number of lines in each matrix int [] sumColumn; // Array holding the value of the sum of each column. int [] sumRow; // Array holding the value of the sum of each row. int [] resourceVector; // Array that holds the resource vector int [] availableVector; // Array that holds the available vector int [] work; // Array that holds the currently available vector int [] processSequence; // Array holding the sequence of processes to run to completion int index = 0; // Integer for holding the index value of the process sequence boolean finish[]; // Boolean array that tells if a process has finished int [][] claimMatrix; // Array that holds the claim matrix int [][] allocationMatrix; // Array that holds the allocation matrix int [][] needMatrix; // Array that holds the need matrix ( C-A ); boolean isSafe = false; // Boolean value that tells if the system is in a safe or unsafe state // Variables to read in line from a file and tokenize String line; String fileIn; File file = new File("FILE PATH"); StringTokenizer tokens; @SuppressWarnings("resource") Scanner input = new Scanner(System.in); //System.out.print("Please enter in the name of the file : "); //fileIn = input.nextLine(); WILL ALLOW FOR MANUAL INPUT OF FILE // build input stream FileReader fr = new FileReader(file); //or fileIn // Use Buffered reader to read one line at a time BufferedReader bufRead = new BufferedReader(fr); // Read first line line = bufRead.readLine(); n = Integer.parseInt(line); // Read the second line line = bufRead.readLine(); m = Integer.parseInt(line); //Create 2D array for each of the m process and n resources claimMatrix = new int[n][m]; allocationMatrix = new int[n][m]; needMatrix = new int [n][m]; //Create resourceVector resourceVector = new int[m]; //Create availableVector availableVector = new int[m]; work = new int[m]; finish = new boolean[n]; processSequence = new int[n]; sumColumn = new int[m]; sumRow = new int[n]; line = bufRead.readLine(); count++; // Read the file and get the claimMatrix from the file while(line != null && lineCount < n) { tokens = new StringTokenizer(line); if(tokens.hasMoreTokens()) { for(int j = 0; j < m; j++) { claimMatrix[lineCount][j]=Integer.parseInt(tokens.nextToken()); } } line = bufRead.readLine(); lineCount++; count++; } lineCount = 0; // Read the file and get the Allocation Matrix while(line != null && lineCount < n) { tokens = new StringTokenizer(line); if(tokens.hasMoreTokens()) { for(int j = 0; j < m; j++) { allocationMatrix[lineCount][j] = Integer.parseInt(tokens.nextToken()); } } line = bufRead.readLine(); lineCount++; count++; } // Read the last line and set Resource Vector try{ tokens = new StringTokenizer(line); while(tokens.hasMoreTokens()) { for(int i = 0; i < m; i++) { resourceVector[i] = Integer.parseInt(tokens.nextToken()); } } }catch (NullPointerException e){ e.printStackTrace(); } // Cloe the bufferedreader and file bufRead.close(); fr.close(); // Determine the initial need matrix for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { needMatrix[i][j] = claimMatrix[i][j]- allocationMatrix[i][j]; } } // Determine the initial available vector for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { sumColumn[j] += allocationMatrix[i][j]; sumRow[i] += needMatrix[i][j]; } } for(int j = 0; j < m; j++) { availableVector[j] = resourceVector[j] - sumColumn[j]; } // Initialize Work and Finish for(int j = 0; j < m; j++) { work[j]=availableVector[j]; } for(int i = 0; i < n; i++) { finish[i] = false; } // Safety Algorithm (checks if the system is in a safe or found state) boolean found = false; do { // Process found flag found = false; int i = 0; for(; i < n; i++) { if ((!finish[i])) { boolean good = true; for (int j = 0; j < m; j++) { // If the need is greater than the available, //then the process is not able to run to completion // So it is not good if(needMatrix[i][j] > work[j]) { good = false; break; } } // Try another process if (!good) continue; found = true; break; } } // Process is found that can run to completion, simulate execution if(found) { finish[i] = true; for (int j = 0; j < m; j++) { work[j] += allocationMatrix[i][j]; } processSequence[index++] = i; } }while (found); //check whether all process are finished or not for(int i = 0; i < n; i++) { if(!finish[i]) { isSafe = false; } else { isSafe = true; } } // Display output System.out.println("Number of Processes : " + n); System.out.println("Number of Resources : " + m + " "); //Display the claimMatrix System.out.println("claimMatrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(claimMatrix[i][j] + " "); } System.out.println(); } //Display allocation matrix System.out.println(" Allocation Matrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(allocationMatrix[i][j] + " "); } System.out.println(); } //Display resource matrix System.out.println(" Resource Vector : "); for(int i = 0; i < m; i++) { System.out.print(resourceVector[i] + " "); } System.out.println(); //Display the Need matrix System.out.println(" Need Matrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(needMatrix[i][j] + " "); } System.out.println(); } //Display the Available Vector System.out.println(); System.out.println("Initial Available Vector : "); for(int j = 0; j < m; j++) { System.out.print(availableVector[j] +" "); } System.out.println(" "); //Print the output if(isSafe) { System.out.print("Process Sequence : "); for(int i = 0; i < processSequence.length; i++) { System.out.print((processSequence[i]+1) + " "); } System.out.println(); System.out.println("This system is in a safe state!!!"); } else { System.out.println("This system is not in a safe state!!!"); } } } 

bankers.txt

7 5 1 1 2 3 5 3 2 4 4 6 2 2 2 2 2 0 0 0 0 5 1 3 6 7 6 2 2 2 0 0 1 0 0 0 1 0 1 2 3 4 2 0 3 4 6 1 2 0 0 0 0 0 0 0 4 1 3 4 7 5 2 1 2 0 0 0 0 0 0 1

OUTPUT

"C:\Program Files\Java\jdk-9.0.4\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\lib\idea_rt.jar=64246:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\andre\OneDrive\Documents\CMSC412\out\production\CMSC412 BankersAlgorithm

java.lang.NullPointerException

at java.base/java.util.StringTokenizer.(StringTokenizer.java:199)

at java.base/java.util.StringTokenizer.(StringTokenizer.java:236)

at BankersAlgorithm.main(BankersAlgorithm.java:202)

Number of Processes : 7

Number of Resources : 5

claimMatrix :

1 1 2 3 5

3 2 4 4 6

2 2 2 2 2

0 0 0 0 5

1 3 6 7 6

2 2 2 0 0

1 0 0 0 1

Allocation Matrix :

0 1 2 3 4

2 0 3 4 6

1 2 0 0 0

0 0 0 0 4

1 3 4 7 5

2 1 2 0 0

0 0 0 0 1

Resource Vector :

0 0 0 0 0

Need Matrix :

1 0 0 0 1

1 2 1 0 0

1 0 2 2 2

0 0 0 0 1

0 0 2 0 1

0 1 0 0 0

1 0 0 0 0

Initial Available Vector :

-6 -7 -11 -14 -20

This system is not in a safe state!!!

Process finished with exit code 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions