publicstaticvoidmain(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 = newint[nProcesses][mResources]; maxNeedMatrix = newint[nProcesses][mResources]; availableVector = newint[mResources]; // Read the allocation and max need matricesfor (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 resultsif (safeSequences.isEmpty()) { System.out.println("There is no safe sequence."); } elseif (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. */privatestatic 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 sequencewhile (!processQueue.isEmpty()) { int processId = processQueue.remove(); // If the process has all of its resources, it can finishif (isProcessFinished(processId)) { safeSequences.add(new ArrayList<>(Arrays.asList(processId))); continue; } // Try to allocate all of the resources needed by the processboolean resourcesAllocated = allocateResources(processId); // If the resources were allocated, the process can finishif (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. */privatestaticbooleanisProcessFinished(int processId){ for (int i = 0
We are here to help
We have crazy offers
It’s quick and easy to place an order. We have an efficient customer service that works 24/7 to assist you.It’s quick and easy to place an order. We have an efficient customer service that works 24/7 to assist you.
We are here and ready to help
Ready to join our block community of business leaders for four days of virtual sessions on driving developer happiness and boosting productivity?