银行家算法java代码

博客围绕银行家算法展开,重点给出了其Java代码实现。银行家算法是信息技术领域重要算法,用于避免死锁等问题,通过Java代码可更好地在实际中运用该算法解决相关问题。
import java.util.Scanner;

public class banker {

    private int Process = 0; // 定义最大进程数目
     private int Resource = 0; // 定义最大资源类数
      private int Work[]; // 定义系统可提供给进程继续运行所需的各类资源数目
     private int MAX[][]; // 定义进程最大资源需求
     private int Allocation[][]; // 定义进程当前已用资源数目
     private int need[][]; // 定义进程需要资源数目
     private int Request[][]; // 定义进程请求资源向量
     private boolean finish[];// 定义进程完成标志
     private int Available[];
     private int P[];
     private Scanner in = new Scanner(System.in); // 定义全局输入流
     public void close() {   in.close();   } // 构造函数,初始化各向量
    public banker() throws Exception {
         int i, j;
         System.out.print("请输入当前进程的个数:");
         Process = in.nextInt();
         System.out.print("请输入系统资源种类的类数:");
         Resource = in.nextInt();   // 初始化各个数组
         Available = new int[Resource];
         Work = new int[Resource];
         MAX = new int[Process][Resource];
         Allocation = new int[Process][Resource];
         need = new int[Process][Resource];
         Request = new int[Process][Resource];
         finish = new boolean[Process];
         P = new int[Process];
         System.out.println("请输入每个进程最大资源需求量,按" + Process + "*" + Resource     + "矩阵输入:");
         for (i = 0; i < Process; i++) {     System.out.print("请输入P" + (i + 1) + "进程各类资源最大需求量:");
             for (j = 0; j < Resource; j++)     MAX[i][j] = in.nextInt();   }
         System.out.println("请输入每个进程已分配的各资源数,也按照" + Process + "*" + Resource     + "矩阵输入:");
         for (i = 0; i < Process; i++) {     System.out.print("请输入P" + (i + 1) + "进程各类资源已分配 的资源数:");
            for (j = 0; j < Resource; j++)
            {     Allocation[i][j] = in.nextInt();
            need[i][j] = MAX[i][j] - Allocation[i][j];
            if (need[i][j] < 0) {
                System.out.println("您输入的第" + (i + 1) + " 个进程所拥有的第"         + (j + 1) + "个资源数错误,请重新输入:");
                j--;
                continue;     }    }   }
         System.out.print("请输入系统各类资源可用的数目:");
        for (i = 0; i < Resource; i++) {    Available[i] = in.nextInt();   }  }
        public void Bank() throws Exception {
         int j, i;
         int tempAvailable[] = new int[Resource];
         int tempAllocation[] = new int[Resource];
         int tempNeed[] = new int[Resource];
         System.out.println("----------------------------------------------------------");
         System.out.print("请输入要申请资源的进程号(当前共有" + Process+ "个进程,如为进程P1申请,请输入1,以此类推)");
         i = in.nextInt() - 1;
         System.out.print("请输入P" + (i + 1) + "进程申请的各资源的数量");
         for (j = 0; j < Resource; j++)
         {    Request[i][j] = in.nextInt();
         }
         for (j = 0; j < Resource; j++)
         { if (Request[i][j] > need[i][j])
         {      System.out.println("您输入的申请的资源数超过进程的需求量!请重新输入!");
         continue;
         }
         if (Request[i][j] > Available[j])
         {      System.out.println("您输入的申请数超过系统可用的资源数!请重新输入!");     continue;
         }
         }
         for (j = 0; j < Resource; j++) {    tempAvailable[j] = Available[j];
             tempAllocation[j] = Allocation[i][j];
             tempNeed[j] = need[i][j];
             Available[j] = Available[j] - Request[i][j];
             Allocation[i][j] = Allocation[i][j] + Request[i][j];
             need[i][j] = need[i][j] - Request[i][j];   }
         if (Safe()) {
             System.out.println("分配给P" + i + "进程成功!");
         System.out.print("分配前系统可用资源:");
             for (int k = 0; k < Resource; k++)
                 System.out.print(tempAvailable[k] + " ");
             System.out.print("\n分配前进程P" + i + "各类资源已分配数量:");
             for (int k = 0; k < Resource; k++)
                 System.out.print(tempAllocation[k] + " ");
             System.out.print("\n分配前进程P" + i + "各类资源需求数量:");
             for (int k = 0; k < Resource; k++)
                 System.out.print(tempNeed[k] + " ");
             System.out.print("\n分配后系统可用资源: ");
             for (int k = 0; k < Resource; k++)
                 System.out.print(Available[k] + " ");
             System.out.print("\n分配后进程P" + i + "各类资源已分配数量:");
             for (int k = 0; k < Resource; k++)
                 System.out.print(Allocation[i][k] + " ");
             System.out.print("\n分配后进程P" + i + "各类资源需求数量:");
             for (int k = 0; k < Resource; k++)
                 System.out.print(need[i][k] + " ");    System.out.println(); }
         else {     System.out.println("申请资源失败!");
         for (j = 0; j < Resource; j++)
         {      Available[j] = Available[j] + Request[i][j];
         Allocation[i][j] =  Allocation[i][j]  +  Request[i][j];
         need[i][j] = need[i][j] + Request[i][j];    }   }
         for (i = 0; i < Process; i++)
         {    finish[i] = false;   }   } // 安全性算法
     public boolean Safe()
     {   int i, j, k, t = 0;
     Work = new int[Resource];
     for (i = 0; i < Resource; i++)
         Work[i] = Available[i];
     for (i = 0; i < Process; i++)
     {    finish[i] = false;   }
    for (i = 0; i < Process; i++)
    {    if (finish[i] == true)
    {     continue;    } else
        {      for (j = 0; j < Resource; j++)
        {      if (need[i][j] > Work[j])
        {       break;
        }
        }
        if (j == Resource)
        {      finish[i] = true;
        for (k = 0; k < Resource; k++)
        {       Work[k] += Allocation[i][k];
        }
        P[t++] = i;
        i = -1;
        }
        else {      continue;
        }
        }
        if (t == Process) {
            System.out.print("当前系统是安全的,存在一安全序 列:");
        for (i = 0; i < t; i++)
        {
            System.out.print("P" + P[i]);
            if (i != t - 1) {
                System.out.print("---");      }
        }
        System.out.println();     return true;    }
    }
    System.out.println("当前系统是不安全的,不存在安全序列");
    return false;  }
    public static void main(String[] args) {
         try {     banker b = new banker();
             b.Safe();
             for (int i = 0; i < 200; i++)
                 b.Bank();
             b.close();   }
         catch (Exception e) {

         }
     }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寻梦&之璐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值