Java实现银行家算法

99 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

关于银行家算法,是操作系统进程分配中很重要的一个算法,理论解释请看这篇文章
blog.csdn.net/Mr_YanMingX…
在这里用Java做一下简单代码的实现:

/**
 * Java实现银行家算法
 *
 * @author 17122
 */
public class Test01 {

    /**
     * 数据结构
     */
    public int[][] allocation;
    public int[][] need;
    public int[] available;

    /**
     * 初始化方法
     */
    public void init() {
        allocation = new int[][]{
                {0, 0, 3, 2},
                {1, 0, 0, 0},
                {1, 3, 5, 4},
                {0, 3, 3, 2},
                {0, 0, 1, 4}
        };
        need = new int[][]{
                {0, 0, 1, 2},
                {1, 7, 5, 0},
                {2, 3, 5, 6},
                {0, 6, 5, 2},
                {0, 6, 5, 6}
        };
        available = new int[]{1, 6, 2, 2};
        System.out.println("Allocation矩阵:   Need矩阵:        Available:");
        for (int i = 0; i < allocation.length; i++) {
            for (int j = 0; j < allocation[i].length; j++) {
                System.out.print(allocation[i][j] + "  ");
            }
            System.out.print("      ");
            for (int j = 0; j < need[i].length; j++) {
                System.out.print(need[i][j] + "  ");
            }
            if (i == 0) {
                System.out.print("     ");
                for (int j = 0; j < available.length; j++) {
                    System.out.print(available[j] + "  ");
                }
            }
            System.out.println();
        }

    }

    /**
     * 请求资源方法
     *
     * @param index 请求的进程
     * @param req   请求数量
     */
    public void request(int index, int[] req) {
        //先进性数据初始化
        init();
        if (req[0] <= available[0] && req[1] <= available[1] && req[2] <= available[2] && req[3] <= available[3]) {
            //分配
            int[] process_has = allocation[index];
            int[] process_need = need[index];
            if (process_need[0] >= req[0] && process_need[1] >= req[1] && process_need[2] >= req[2] && process_need[3] >= req[3]) {
                for (int i = 0; i < process_has.length; i++) {
                    process_has[i] += req[i];
                    process_need[i] -= req[i];
                }
                for (int i = 0; i < available.length; i++) {
                    available[i] -= req[i];
                }
            }
            //检查
            if (safe()) {
                System.out.println("分配成功!");
            } else {
                System.out.println("分配后不安全,取消分配!");
            }
        } else {
            System.out.println("可用资源不足,无法分配!");
        }
    }


    /**
     * 检查方法
     *
     * @return
     */
    public boolean safe() {
        Boolean[] finish = {false, false, false, false, false};
        // 完成进程数
        int count = 0;
        // 循环圈数
        int circle = 0;
        // 安全序列
        int[] S = new int[5];
        // 设置工作向量
        int work[] = available;
        boolean flag = true;
        boolean isNeed = true;
        while (count < 5) {
            if (flag) {
                System.out.println("Process  " + "   Work  " + "       Allocation " + "        Need  " + "       Available ");
                flag = false;
            }
            for (int i = 0; i < 5; i++) {
                isNeed = true;
                for (int j = 0; j < 4; j++) {
                    if (!(finish[i] == false && need[i][j] <= work[j])) {
                        isNeed = false;
                    }
                }
                // 判断条件
                if (isNeed) {
                    System.out.print("P" + i + "      ");
                    for (int k = 0; k < 4; k++) {
                        System.out.print(work[k] + "  ");
                    }
                    System.out.print("    ");
                    for (int j = 0; j < 4; j++) {
                        work[j] += allocation[i][j];
                    }
                    // 当前进程能满足时
                    finish[i] = true;
                    // 当前序列
                    S[count] = i;
                    // 满足进程数+1
                    count++;
                    for (int j = 0; j < 4; j++) {
                        System.out.print(allocation[i][j] + "  ");
                    }
                    System.out.print("    ");
                    for (int j = 0; j < 4; j++) {
                        System.out.print(need[i][j] + "  ");
                    }
                    System.out.print("    ");
                    for (int j = 0; j < 4; j++) {
                        System.out.print(work[j] + "  ");
                    }
                    System.out.println();
                }

            }
            // 循环圈数加1
            circle++;
            // 判断是否满足所有进程需要
            if (count == 5) {
                System.out.print("安全序列为:");
                // 输出安全序列
                for (int i = 0; i < 5; i++) {
                    System.out.print("P" + S[i] + " ");
                }
                return true;
            }
            // 判断完成进程数是否小于循环圈数
            if (count < circle) {
                return false;
            }
        }
        return false;

    }


    public static void main(String[] args) {
        Test01 test01 = new Test01();
        test01.request(0, new int[]{0, 0, 1, 2});
    }

}

运行结果:
在这里插入图片描述