Banker’s algorithm

Sample Solution

   
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class BankersAlgorithm {

    private static int[][] allocationMatrix;
    private static int[][] maxNeedMatrix;
    private static int[][] availableVector;
    private static int nProcesses;
    private static int mResources;

Full Answer Section

    public static void main(String[] args) throws IOException { // Read the input file String inputFile = "input.txt"; BufferedReader reader = new BufferedReader(new FileReader(inputFile)); // Read the number of processes and resources nProcesses = Integer.parseInt(reader.readLine()); mResources = Integer.parseInt(reader.readLine()); // Initialize the matrices allocationMatrix = new int[nProcesses][mResources]; maxNeedMatrix = new int[nProcesses][mResources]; availableVector = new int[mResources]; // Read the allocation and max need matrices for (int i = 0; i < nProcesses; i++) { String[] line = reader.readLine().split(" "); for (int j = 0; j < mResources; j++) { allocationMatrix[i][j] = Integer.parseInt(line[j]); maxNeedMatrix[i][j] = Integer.parseInt(line[j + mResources]); } } // Read the available vector String[] line = reader.readLine().split(" "); for (int i = 0; i < mResources; i++) { availableVector[i] = Integer.parseInt(line[i]); } // Find the safe sequences List<List<Integer>> safeSequences = findSafeSequences(); // Display the results if (safeSequences.isEmpty()) { System.out.println("There is no safe sequence."); } else if (safeSequences.size() == 1) { System.out.println("There is exactly one safe sequence:"); System.out.println(safeSequences.get(0)); } else { System.out.println("There are two safe sequences:"); System.out.println(safeSequences.get(0)); System.out.println(safeSequences.get(1)); } reader.close(); } /** * Finds all possible safe sequences. * * @return A list of safe sequences, or an empty list if there are no safe sequences. */ private static List<List<Integer>> findSafeSequences() { List<List<Integer>> safeSequences = new ArrayList<>(); // Create a queue of processes Queue<Integer> processQueue = new LinkedList<>(); for (int i = 0; i < nProcesses; i++) { processQueue.add(i); } // While the process queue is not empty, try to find a safe sequence while (!processQueue.isEmpty()) { int processId = processQueue.remove(); // If the process has all of its resources, it can finish if (isProcessFinished(processId)) { safeSequences.add(new ArrayList<>(Arrays.asList(processId))); continue; } // Try to allocate all of the resources needed by the process boolean resourcesAllocated = allocateResources(processId); // If the resources were allocated, the process can finish if (resourcesAllocated) { safeSequences.add(new ArrayList<>(Arrays.asList(processId))); continue; } // If the resources could not be allocated, put the process back on the queue processQueue.add(processId); } return safeSequences; } /** * Checks if the given process has all of its resources. * * @param processId The ID of the process to check. * @return True if the process has all of its resources, false otherwise. */ private static boolean isProcessFinished(int processId) { for (int i = 0

IS IT YOUR FIRST TIME HERE? WELCOME

USE COUPON "11OFF" AND GET 11% OFF YOUR ORDERS