|
| 1 | +package com.thealgorithms.Operating_System_Concepts_Implementation; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class BankersAlgorithm { |
| 7 | + private int processes; // Number of processes |
| 8 | + private int resources; // Number of resources |
| 9 | + private int[] available; // Available instances of each resource |
| 10 | + private int[][] max; // Maximum R of each resource |
| 11 | + private int[][] allot; // Allocation R of each resource |
| 12 | + private int[][] need; // Remaining R needed by each process |
| 13 | + |
| 14 | + public BankersAlgorithm(int processes, int resources) { |
| 15 | + this.processes = processes; |
| 16 | + this.resources = resources; |
| 17 | + available = new int[resources]; |
| 18 | + max = new int[processes][resources]; |
| 19 | + allot = new int[processes][resources]; |
| 20 | + need = new int[processes][resources]; |
| 21 | + } |
| 22 | + |
| 23 | + // Function to calculate the need matrix |
| 24 | + private void calculateNeed() { |
| 25 | + for (int i = 0; i < processes; i++) { |
| 26 | + for (int j = 0; j < resources; j++) { |
| 27 | + need[i][j] = max[i][j] - allot[i][j]; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Function to find if the system is in a safe state |
| 33 | + private boolean isSafe() { |
| 34 | + boolean[] finish = new boolean[processes]; |
| 35 | + int[] safeSequence = new int[processes]; |
| 36 | + int count = 0; |
| 37 | + |
| 38 | + int[] work = Arrays.copyOf(available, resources); |
| 39 | + |
| 40 | + while (count < processes) { |
| 41 | + boolean found = false; |
| 42 | + |
| 43 | + for (int p = 0; p < processes; p++) { |
| 44 | + if (!finish[p]) { |
| 45 | + int j; |
| 46 | + for (j = 0; j < resources; j++) { |
| 47 | + if (need[p][j] > work[j]) { |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (j == resources) { |
| 53 | + for (int k = 0; k < resources; k++) { |
| 54 | + work[k] += allot[p][k]; |
| 55 | + } |
| 56 | + safeSequence[count++] = p; |
| 57 | + finish[p] = true; |
| 58 | + found = true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!found) { |
| 64 | + System.out.println("System is not in a safe state."); |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + System.out.println("System is in a safe state."); |
| 70 | + System.out.print("Safe sequence is: "); |
| 71 | + for (int i = 0; i < processes; i++) { |
| 72 | + System.out.print(safeSequence[i] + " "); |
| 73 | + } |
| 74 | + System.out.println(); |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + // Function to request resources |
| 79 | + public void requestResources(int processNum, int[] request) { |
| 80 | + // Check if request is less than need |
| 81 | + for (int i = 0; i < resources; i++) { |
| 82 | + if (request[i] > need[processNum][i]) { |
| 83 | + System.out.println("Error: Process has exceeded its maximum claim."); |
| 84 | + return; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Check if request is less than available |
| 89 | + for (int i = 0; i < resources; i++) { |
| 90 | + if (request[i] > available[i]) { |
| 91 | + System.out.println("Process is waiting."); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Pretend to allocate resources |
| 97 | + for (int i = 0; i < resources; i++) { |
| 98 | + available[i] -= request[i]; |
| 99 | + allot[processNum][i] += request[i]; |
| 100 | + need[processNum][i] -= request[i]; |
| 101 | + } |
| 102 | + |
| 103 | + // Check system state |
| 104 | + if (isSafe()) { |
| 105 | + System.out.println("Resources allocated successfully."); |
| 106 | + } else { |
| 107 | + // Rollback |
| 108 | + for (int i = 0; i < resources; i++) { |
| 109 | + available[i] += request[i]; |
| 110 | + allot[processNum][i] -= request[i]; |
| 111 | + need[processNum][i] += request[i]; |
| 112 | + } |
| 113 | + System.out.println("Resources allocation leads to unsafe state, request denied."); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static void main(String[] args) { |
| 118 | + Scanner scanner = new Scanner(System.in); |
| 119 | + |
| 120 | + System.out.print("Enter number of processes: "); |
| 121 | + int processes = scanner.nextInt(); |
| 122 | + System.out.print("Enter number of resources: "); |
| 123 | + int resources = scanner.nextInt(); |
| 124 | + |
| 125 | + BankersAlgorithm bankersAlgorithm = new BankersAlgorithm(processes, resources); |
| 126 | + |
| 127 | + System.out.println("Enter maximum resource matrix:"); |
| 128 | + for (int i = 0; i < processes; i++) { |
| 129 | + for (int j = 0; j < resources; j++) { |
| 130 | + bankersAlgorithm.max[i][j] = scanner.nextInt(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + System.out.println("Enter allocation matrix:"); |
| 135 | + for (int i = 0; i < processes; i++) { |
| 136 | + for (int j = 0; j < resources; j++) { |
| 137 | + bankersAlgorithm.allot[i][j] = scanner.nextInt(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + System.out.println("Enter available resources:"); |
| 142 | + for (int i = 0; i < resources; i++) { |
| 143 | + bankersAlgorithm.available[i] = scanner.nextInt(); |
| 144 | + } |
| 145 | + |
| 146 | + bankersAlgorithm.calculateNeed(); |
| 147 | + |
| 148 | + System.out.print("Request resources for process (0 to " + (processes - 1) + "): "); |
| 149 | + int processNum = scanner.nextInt(); |
| 150 | + System.out.println("Enter request:"); |
| 151 | + int[] request = new int[resources]; |
| 152 | + for (int i = 0; i < resources; i++) { |
| 153 | + request[i] = scanner.nextInt(); |
| 154 | + } |
| 155 | + |
| 156 | + bankersAlgorithm.requestResources(processNum, request); |
| 157 | + |
| 158 | + scanner.close(); |
| 159 | + } |
| 160 | +} |
| 161 | + |
0 commit comments