资源管理系统的设计和实现,还有银行家算法演示java代码

根据下面的实验报告,输出内容如最后两个图片所示,资源管理系统的设计和实现,还有银行家算法演示java代码

img

img

img

img

资源调度之最大最小分配算法java实现:https://blog.csdn.net/PSY_God/article/details/123765564
计算机操作系统_银行家算法(java实现):https://blog.csdn.net/weixin_53029078/article/details/120815685

银行家算法示例

import java.util.Scanner;

public class BankerDemo {
    private int[][] max; // 最大资源需求矩阵
    private int[][] allocation; // 分配资源矩阵
    private int[][] need; // 资源需求矩阵
    private int[] available; // 可用资源向量
    private int processNum; // 进程数量
    private int resourceNum; // 资源种类数

    public void init() {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入进程的数量: ");
        processNum = in.nextInt();
        System.out.print("请输入资源的种类数: ");
        resourceNum = in.nextInt();
        max = new int[processNum][resourceNum];
        allocation = new int[processNum][resourceNum];
        need = new int[processNum][resourceNum];
        available = new int[resourceNum];
        System.out.println("请输入每个进程的最大资源需求量");
        for (int i = 0; i < processNum; i++) {
            System.out.print("请输入进程 " + i + " 的最大资源需求量:");
            for (int j = 0; j < resourceNum; j++) {
                max[i][j] = in.nextInt();
            }
        }
        System.out.println("请输入每个进程分配的资源量");
        for (int i = 0; i < processNum; i++) {
            System.out.print("请输入进程 " + i + " 分配的资源量:");
            for (int j = 0; j < resourceNum; j++) {
                allocation[i][j] = in.nextInt();
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }
        System.out.println("请输入每个资源的可用数量");
        for (int i = 0; i < resourceNum; i++) {
            System.out.print("请输入第 " + i + " 种资源的可用数量:");
            available[i] = in.nextInt();
        }
    }

    public boolean checkSafety() {
        boolean[] finish = new boolean[processNum];
        int[] work = new int[resourceNum];
        int[] safeSequence = new int[processNum];
        int count = 0;
        for (int i = 0; i < resourceNum; i++) {
            work[i] = available[i];
        }
        while (count < processNum) {
            boolean found = false;
            for (int i = 0; i < processNum; i++) {
                if (!finish[i]) {
                    int j;
                    for (j = 0; j < resourceNum; j++) {
                        if (need[i][j] > work[j])
                            break;
                    }
                    if (j == resourceNum) {
                        for (int k = 0; k < resourceNum; k++) {
                            work[k] += allocation[i][k];
                        }
                        safeSequence[count++] = i;
                        finish[i] = true;
                        found = true;
                    }
                }
            }
            if (!found)
                return false;
        }
        System.out.print("系统是安全的,安全序列为:");
        for (int i = 0; i < processNum; i++) {
            System.out.print(safeSequence[i] + " ");
        }
        System.out.println();
        return true;
    }

    public void requestResource() {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入请求资源的进程编号: ");
        int pid = in.nextInt();
        System.out.println("请输入请求的资源量");
        int[] request = new int[resourceNum];
        for (int i = 0; i < resourceNum; i++) {
            request[i] = in.nextInt();
        }
        for (int i = 0; i < resourceNum; i++) {
            if (request[i] > need[pid][i] || request[i] > available[i]) {
                System.out.println("请求拒绝!");
                return;
            }
        }
        for (int i = 0; i < resourceNum; i++) {
            available[i] -= request[i];
            allocation[pid][i] += request[i];
            need[pid][i] -= request[i];
        }
        if (checkSafety()) {
            System.out.println("请求通过!");
        } else {
            for (int i = 0; i < resourceNum; i++) {
                available[i] += request[i];
                allocation[pid][i] -= request[i];
                need[pid][i] += request[i];
            }
            System.out.println("请求被拒绝!");
        }
    }

    public static void main(String[] args) {
        BankerDemo banker = new BankerDemo();
        banker.init();
        boolean isSafe = banker.checkSafety();
        if (isSafe) {
            System.out.println("系统当前是安全的");
        } else {
            System.out.println("系统当前是不安全的");
        }
        banker.requestResource();
    }
}

第一张图:

    // 读取输入数据
    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入系统可供资源种类的数量:");
    int resourceNum = scanner.nextInt();

    String[] resourceNames = new String[resourceNum];
    int[] availableResources = new int[resourceNum];

    for (int i = 0; i < resourceNum; i++) {
        System.out.print("资源" + (i + 1) + "的名称:");
        resourceNames[i] = scanner.next();
        System.out.print("资源" + (i + 1) + "的数量:");
        availableResources[i] = scanner.nextInt();
    }

    System.out.print("请输入作业的数量:");
    int jobNum = scanner.nextInt();

    int[][] maxNeedMatrix = new int[jobNum][resourceNum];
    int[][] allocationMatrix = new int[jobNum][resourceNum];
    int[][] needMatrix = new int[jobNum][resourceNum];

    System.out.print("请输入各进程的最大需求量(" + jobNum + "*" + resourceNum + "矩阵)[Max]:");
    for (int i = 0; i < jobNum; i++) {
        for (int j = 0; j < resourceNum; j++) {
            maxNeedMatrix[i][j] = scanner.nextInt();
        }
    }

    System.out.print("请输入各进程已经申请的资源量(" + jobNum + "*" + resourceNum + "矩阵)[Allocation]:");
    for (int i = 0; i < jobNum; i++) {
        for (int j = 0; j < resourceNum; j++) {
            allocationMatrix[i][j] = scanner.nextInt();
            needMatrix[i][j] = maxNeedMatrix[i][j] - allocationMatrix[i][j];
        }
    }

    // 进行安全性检查
    boolean[] isFinished = new boolean[jobNum];
    int[] safeSequence = new int[jobNum];
    int finishedCount = 0;

    while (finishedCount < jobNum) {
        boolean canFinish = false;

        for (int i = 0; i < jobNum; i++) {
            if (!isFinished[i] && checkNeed(i, needMatrix, availableResources)) {
                safeSequence[finishedCount] = i;
                finishedCount++;
                isFinished[i] = true;
                canFinish = true;

                for (int j = 0; j < resourceNum; j++) {
                    availableResources[j] += allocationMatrix[i][j];
                }

                break;
            }
        }

        if (!canFinish) {
            System.out.println("系统处于不安全状态,无法分配资源。");
            break;
        }
    }

    if (finishedCount == jobNum) {
        System.out.println("系统是安全的!");
        System.out.print("分配的序列:");
        for (int i = 0; i < jobNum; i++) {
            System.out.print(safeSequence[i] + "->");
        }
        System.out.println();
    }

    // 实现增加资源、删除资源、修改可用资源数量功能
    while (true) {
        System.out.println("**************银行家算法操作**************");
        System.out.println("1:增加资源");
        System.out.println("2:删除资源");
        System.out.println("3:修改可用资源数量");
        System.out.print("请选择操作编号:");
        int option = scanner.nextInt();

        switch (option) {
            case 1:
                System.out.print("请输入新增资源的名称:");
                String newResourceName = scanner.next();
                int[] addedResources = new int[resourceNum];
                System.out.println("请依次输入各进程对新增资源的最大需求量:");
                for (int i = 0; i < jobNum; i++) {
                    addedResources[i] = scanner.nextInt();
                }

                // 在资源数组中增加新资源
                resourceNames = Arrays.copyOf(resourceNames, resourceNum + 1);
                resourceNames[resourceNum] = newResourceName;

                // 在各矩阵中增加新资源
                maxNeedMatrix = addNewResourceToArray(maxNeedMatrix, addedResources);
                allocationMatrix = addNewResourceToArray(allocationMatrix, new int[jobNum]);
                needMatrix = addNewResourceToArray(needMatrix, addedResources);

                resourceNum++;
                break;
            case 2:
                System.out.print("请输入需要删除的资源编号(1-" + resourceNum + "):");
                int deleteResourceIndex = scanner.nextInt() - 1;

                // 在各矩阵中删除资源
                maxNeedMatrix = deleteResourceFromArray(maxNeedMatrix, deleteResourceIndex);
                allocationMatrix = deleteResourceFromArray(allocationMatrix, deleteResourceIndex);
                needMatrix = deleteResourceFromArray(needMatrix, deleteResourceIndex);

                // 在资源数组中删除资源
                resourceNames = deleteResourceFromArray(resourceNames, deleteResourceIndex);
                availableResources = deleteResourceFromArray(availableResources, deleteResourceIndex);

                resourceNum--;
                break;
            case 3:
                System.out.println("当前可用资源数量:");
                for (int i = 0; i < resourceNum; i++) {
                    System.out.print(resourceNames[i] + availableResources[i] + " ");
                }
                System.out.println();

                System.out.println("请输入修改后的可用资源数量:");
                for (int i = 0; i < resourceNum; i++) {
                    availableResources[i] = scanner.nextInt();
                }
                break;
            default:
                System.out.println("无效的操作编号,请重新选择!");
                break;
        }
    }
}

// 检查进程 i 的需求是否小于等于系统可用资源
private static boolean checkNeed(int i, int[][] needMatrix, int[] availableResources) {
    for (int j = 0; j < needMatrix[0].length; j++) {
        if (needMatrix[i][j] > availableResources[j]) {
            return false;
        }
    }
    return true;
}

// 在矩阵中增加一列新资源
private static int[][] addNewResourceToArray(int[][] sourceArray, int[] newColumn) {
    int[][] newMatrix = new int[sourceArray.length][sourceArray[0].length + 1];
    for (int i = 0; i < sourceArray.length; i++) {
        newMatrix[i] = Arrays.copyOf(sourceArray[i], sourceArray[0].length + 1);
        newMatrix[i][sourceArray[0].length] = newColumn[i];
    }
    return newMatrix;
}

// 在数组中增加一个新元素
private static String[] addNewResourceToArray(String[] sourceArray, String newElement) {
    String[] newArray = Arrays.copyOf(sourceArray, sourceArray.length + 1);
    newArray[sourceArray.length] = newElement;
    return newArray;
}

// 在数组中删除一个元素
private static int[] deleteResourceFromArray(int[] sourceArray, int index) {
    int[] newArray = new int[sourceArray.length - 1];
    System.arraycopy(sourceArray, 0, newArray, 0, index);
    System.arraycopy(sourceArray, index + 1, newArray, index, sourceArray.length - index - 1);
    return newArray;
}

// 在矩阵中删除一列资源
private static int[][] deleteResourceFromArray(int[][] sourceArray, int index) {
    int[][] newMatrix = new int[sourceArray.length][sourceArray[0].length - 1];
    for (int i = 0; i < newMatrix.length; i++) {
        System.arraycopy(sourceArray[i], 0, newMatrix[i], 0, index);
        System.arraycopy(sourceArray[i], index + 1, newMatrix[i], index, sourceArray[0].length - index - 1);
    }
    return newMatrix;
}

// 在数组中删除一个元素
private static String[] deleteResourceFromArray(String[] sourceArray, int index) {
    String[] newArray = new String[sourceArray.length - 1];
    System.arraycopy(sourceArray, 0, newArray, 0, index);
    System.arraycopy(sourceArray, index + 1, newArray, index, sourceArray.length - index - 1);
    return newArray;
}

银行家java代码:

import java.util.ArrayList;
import java.util.List;

public class BankerAlgorithm {
    private int[][] allocation;           // 分配矩阵
    private int[][] max;                  // 最大需求矩阵
    private int[][] need;                 // 需求矩阵
    private int[] available;              // 可用资源数组

    private List<Integer> safeSequence;   // 安全序列

    public BankerAlgorithm(int[][] allocation, int[][] max, int[] available) {
        this.allocation = allocation;
        this.max = max;
        this.available = available;
        this.safeSequence = new ArrayList<>();

        int m = allocation.length;
        int n = allocation[0].length;

        // 计算需求矩阵
        this.need = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j =0; j < n; j++) {
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }
    }

    /**
     * 判断状态是否安全
     */
    public boolean isSafe() {
        boolean[] finished = new boolean[allocation.length];
        int[] work = available.clone();
        boolean isSafe = true;

        while (true) {
            boolean canFinish = false;
            for (int i = 0; i < allocation.length; i++) {
                if (!finished[i] && canBeSatisfied(i, work)) {
                    canFinish = true;
                    finished[i] = true;
                    for (int j = 0; j < work.length; j++) {
                        work[j] += allocation[i][j];
                    }
                    safe

https://blog.csdn.net/weixin_51229662/article/details/118144343
https://blog.csdn.net/qq_55915890/article/details/124008942