diff --git a/src/main/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithm.java b/src/main/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithm.java new file mode 100644 index 000000000000..3fa7d0078636 --- /dev/null +++ b/src/main/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithm.java @@ -0,0 +1,161 @@ +package com.thealgorithms.operatingsystemconcepts; + + +import java.util.Arrays; +import java.util.Scanner; + +public class BankersAlgorithm { + private int processes; // Number of processes + private int resources; // Number of resources + private int[] available; // Available instances of each resource + private int[][] max; // Maximum R of each resource + private int[][] allot; // Allocation R of each resource + private int[][] need; // Remaining R needed by each process + + public BankersAlgorithm(int processes, int resources) { + this.processes = processes; + this.resources = resources; + available = new int[resources]; + max = new int[processes][resources]; + allot = new int[processes][resources]; + need = new int[processes][resources]; + } + + // Function to calculate the need matrix + private void calculateNeed() { + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + need[i][j] = max[i][j] - allot[i][j]; + } + } + } + + // Function to find if the system is in a safe state + private boolean isSafe() { + boolean[] finish = new boolean[processes]; + int[] safeSequence = new int[processes]; + int count = 0; + + int[] work = Arrays.copyOf(available, resources); + + while (count < processes) { + boolean found = false; + + for (int p = 0; p < processes; p++) { + if (!finish[p]) { + int j; + for (j = 0; j < resources; j++) { + if (need[p][j] > work[j]) { + break; + } + } + + if (j == resources) { + for (int k = 0; k < resources; k++) { + work[k] += allot[p][k]; + } + safeSequence[count++] = p; + finish[p] = true; + found = true; + } + } + } + + if (!found) { + System.out.println("System is not in a safe state."); + return false; + } + } + + System.out.println("System is in a safe state."); + System.out.print("Safe sequence is: "); + for (int i = 0; i < processes; i++) { + System.out.print(safeSequence[i] + " "); + } + System.out.println(); + return true; + } + + // Function to request resources + public void requestResources(int processNum, int[] request) { + // Check if request is less than need + for (int i = 0; i < resources; i++) { + if (request[i] > need[processNum][i]) { + System.out.println("Error: Process has exceeded its maximum claim."); + return; + } + } + + // Check if request is less than available + for (int i = 0; i < resources; i++) { + if (request[i] > available[i]) { + System.out.println("Process is waiting."); + return; + } + } + + // Pretend to allocate resources + for (int i = 0; i < resources; i++) { + available[i] -= request[i]; + allot[processNum][i] += request[i]; + need[processNum][i] -= request[i]; + } + + // Check system state + if (isSafe()) { + System.out.println("Resources allocated successfully."); + } else { + // Rollback + for (int i = 0; i < resources; i++) { + available[i] += request[i]; + allot[processNum][i] -= request[i]; + need[processNum][i] += request[i]; + } + System.out.println("Resources allocation leads to unsafe state, request denied."); + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter number of processes: "); + int processes = scanner.nextInt(); + System.out.print("Enter number of resources: "); + int resources = scanner.nextInt(); + + BankersAlgorithm bankersAlgorithm = new BankersAlgorithm(processes, resources); + + System.out.println("Enter maximum resource matrix:"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + bankersAlgorithm.max[i][j] = scanner.nextInt(); + } + } + + System.out.println("Enter allocation matrix:"); + for (int i = 0; i < processes; i++) { + for (int j = 0; j < resources; j++) { + bankersAlgorithm.allot[i][j] = scanner.nextInt(); + } + } + + System.out.println("Enter available resources:"); + for (int i = 0; i < resources; i++) { + bankersAlgorithm.available[i] = scanner.nextInt(); + } + + bankersAlgorithm.calculateNeed(); + + System.out.print("Request resources for process (0 to " + (processes - 1) + "): "); + int processNum = scanner.nextInt(); + System.out.println("Enter request:"); + int[] request = new int[resources]; + for (int i = 0; i < resources; i++) { + request[i] = scanner.nextInt(); + } + + bankersAlgorithm.requestResources(processNum, request); + + scanner.close(); + } +} diff --git a/src/test/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithmTest.java b/src/test/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithmTest.java new file mode 100644 index 000000000000..d237dbdfa381 --- /dev/null +++ b/src/test/java/com/thealgorithms/operatingsystemconcepts/BankersAlgorithmTest.java @@ -0,0 +1,90 @@ +package com.thealgorithms.operatingsystemconcepts; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.FileOutputStream; +import java.io.FileDescriptor; + +public class BankersAlgorithmTest { + + private BankersAlgorithm bankersAlgorithm; + + @BeforeEach + void setUp() { + // Example setup with 5 processes and 3 resources + bankersAlgorithm = new BankersAlgorithm(5, 3); + + // Setting up maximum resources + bankersAlgorithm.max = new int[][] { + {7, 5, 3}, + {3, 2, 2}, + {9, 0, 2}, + {2, 2, 2}, + {4, 3, 3} + }; + + // Setting up allocated resources + bankersAlgorithm.allot = new int[][] { + {0, 1, 0}, + {2, 0, 0}, + {3, 0, 2}, + {2, 1, 1}, + {0, 0, 2} + }; + + // Setting up available resources + bankersAlgorithm.available = new int[]{3, 3, 2}; + + // Calculating need matrix + bankersAlgorithm.calculateNeed(); + } + + @Test + void testIsSafeState() { + assertTrue(bankersAlgorithm.isSafe()); + } + + @Test + void testRequestResourcesValid() { + int[] request = {1, 0, 2}; + assertDoesNotThrow(() -> bankersAlgorithm.requestResources(0, request)); + } + + @Test + void testRequestResourcesExceedsNeed() { + int[] request = {8, 0, 0}; + assertEquals("Error: Process has exceeded its maximum claim.", + getOutput(() -> bankersAlgorithm.requestResources(0, request))); + } + + @Test + void testRequestResourcesExceedsAvailable() { + int[] request = {1, 0, 3}; + assertEquals("Process is waiting.", + getOutput(() -> bankersAlgorithm.requestResources(0, request))); + } + + @Test + void testRequestResourcesUnsafeState() { + // Requesting resources that lead to an unsafe state + int[] request = {3, 3, 0}; + assertEquals("Resources allocation leads to unsafe state, request denied.", + getOutput(() -> bankersAlgorithm.requestResources(1, request))); + } + + // Helper method to capture console output + private String getOutput(Runnable runnable) { + // Code to capture System.out output + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + runnable.run(); + + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + return outContent.toString().trim(); + } +}