/** * 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 java.io.*; import java.util.StringTokenizer; import java.util.Scanner; public class Banker { public static void main(String[]args)throws IOException { 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 ( CA ); 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; StringTokenizer tokens; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter in the name of the file : "); fileIn = keyboard.nextLine(); // build input stream FileReader fr = new FileReader(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); count++; // Read second line line = bufRead.readLine(); m = Integer.parseInt(line); count++; // Create each Matrix(n x m) and Vector( m ) claimMatrix = new int[n][m]; allocationMatrix = new int[n][m]; needMatrix = new int [n][m]; resourceVector = new int[m]; 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 through file and set Claim Matrix 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 through file and set 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 tokens = new StringTokenizer(line); while(tokens.hasMoreTokens()) { for(int i = 0; i < m; i++) { resourceVector[i] = Integer.parseInt(tokens.nextToken()); } } // Close the bufferreader 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 unsafe state) boolean found = false; do { found = false; // Process found flag 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; } } if (!good) continue; // Try another process 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 if all processes have finished 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 + "\n"); System.out.println("Claim Matrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(claimMatrix[i][j] + " "); } System.out.println(); } System.out.println("\nAllocation Matrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(allocationMatrix[i][j] + " "); } System.out.println(); } System.out.println("\nResource Vector : "); for(int i = 0; i < m; i++) { System.out.print(resourceVector[i] + " " ); } System.out.println(); System.out.println("\nNeed Matrix : "); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(needMatrix[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("Initial Available Vector : "); for(int j = 0; j < m; j++) { System.out.print(availableVector[j] + " "); } System.out.println("\n"); 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!!!"); } } }
I also have attached two sample input files for a safe and unsafe state
line 1 : number of processes
line 2: number of resources
lines 3 6 : claim matrix
lines 710 : allocation matrix
line 11  resource vector
Safe Sample Input

4
3
3 2 2
6 1 3
3 1 4
4 2 2
1 0 0
6 1 3
2 1 1
0 0 2
9 3 6

Any feedback will be greatly appreciated.
Attached File(s)

Safe.txt (67bytes)
Number of downloads: 583 
Unsafe.txt (57bytes)
Number of downloads: 405