Java实现银行家算法

简介: Java实现银行家算法

关于银行家算法,是操作系统进程分配中很重要的一个算法,理论解释请看这篇文章

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});
    }
}
复制代码


运行结果:


网络异常,图片无法展示
|


相关文章
|
1天前
|
缓存 算法 Java
如何优化Java中的递归算法?
如何优化Java中的递归算法?
|
1天前
|
存储 算法 搜索推荐
Java数据结构与算法优化
Java数据结构与算法优化
|
2天前
|
算法 Java 数据处理
Java中MD5加密算法的实现
Java中MD5加密算法的实现
|
4天前
|
存储 算法 安全
Java中的DES和3DES加密算法详解
Java中的DES和3DES加密算法详解
|
4天前
|
Java BI C#
技术笔记:SM4加密算法实现Java和C#相互加密解密
技术笔记:SM4加密算法实现Java和C#相互加密解密
|
4天前
|
机器学习/深度学习 并行计算 搜索推荐
程序技术好文:桶排序算法及其Java实现
程序技术好文:桶排序算法及其Java实现
|
5天前
|
搜索推荐 算法 小程序
基于Java协同过滤算法的电影推荐系统设计和实现(源码+LW+调试文档+讲解等)
基于Java协同过滤算法的电影推荐系统设计和实现(源码+LW+调试文档+讲解等)
|
5天前
|
搜索推荐 算法 小程序
基于Java协同过滤算法的图书推荐系统设计和实现(源码+LW+调试文档+讲解等)
基于Java协同过滤算法的图书推荐系统设计和实现(源码+LW+调试文档+讲解等)
|
5天前
|
搜索推荐 算法 Java
Java数据结构与算法:排序算法之选择排序
Java数据结构与算法:排序算法之选择排序
|
5天前
|
搜索推荐 算法 Java
Java数据结构与算法:排序算法之冒泡排序
Java数据结构与算法:排序算法之冒泡排序