操作系统算法 之 银行家算法

来源:互联网 发布:矩阵满秩是什么意思 编辑:程序博客网 时间:2024/05/20 01:45

MyBanker.java:银行家算法类

Main.java:程序入口

如有疑问,请请在评论留言。


下面附上代码

MyBanker.java

————————————————————

import java.util.LinkedHashMap;import java.util.Map;import java.util.Scanner;public class MyBanker {private int resourceCount;private int processCount;private int[] available;private int[][] max;private int[][] allocation;private int[][] need;private int[] request;Scanner scanner = new Scanner(System.in);public MyBanker(){System.out.println("请输入资源数量:");this.resourceCount = scanner.nextInt();System.out.println("请输入进程数量:");this.processCount = scanner.nextInt();available = new int[resourceCount];max = new int[processCount][resourceCount];allocation = new int[processCount][resourceCount];need = new int[processCount][resourceCount];}public void initData(){System.out.println("请输入" + resourceCount + "类资源的数量:");        for(int i=0;i<resourceCount;i++){            available[i]=scanner.nextInt();        }        for(int i=0;i<processCount;i++){            System.out.println("请输入进程" + i + "对"+ resourceCount +"类资源的最大需求:");            for(int j=0;j<resourceCount;j++){                max[i][j]=scanner.nextInt();            }        }        for(int i=0;i<processCount;i++){            System.out.println("请输入进程" + i + "已分配的" + resourceCount + "类资源数量:");            for(int j=0;j<resourceCount;j++){                allocation[i][j]=scanner.nextInt();            }        }        for(int i=0;i<processCount;i++){            for(int j=0;j<resourceCount;j++){                need[i][j]=max[i][j]-allocation[i][j];            }        }        for(int i=0;i<resourceCount;i++){            for(int j=0;j<processCount;j++){                available[i]-=allocation[j][i];            }        }        checkSafety();}public void requestResource(){System.out.println("请输入申请资源的线程:");int requestId = scanner.nextInt();if(requestId < 0 || requestId >= processCount){throw new RuntimeException("该线程不存在!");}System.out.println("请输入申请的资源(共" + resourceCount + "类,若某类资源不申请则填0)。");request = new int[resourceCount];        for(int i=0;i<resourceCount;i++){            request[i]=scanner.nextInt();        }        for(int i=0;i<resourceCount;i++){        if(request[i] > need[requestId][i]){        throw new RuntimeException(requestId+"线程申请的资源超出其需要的资源!");        }        }        for(int i=0;i<resourceCount;i++){        if(request[i] > available[i]){        throw new RuntimeException(requestId+"线程申请的资源大于系统资源!");        }        }        for(int i=0;i<resourceCount;i++){            available[i] -= request[i];            allocation[requestId][i] += request[i];            need[requestId][i] -= request[i];        }        if(checkSafety()){        System.out.println(requestId+"线程申请资源成功!");        }else{        for(int i=0;i<resourceCount;i++){        available[i] += request[i];        allocation[requestId][i] -= request[i];        need[requestId][i] += request[i];        }        throw new RuntimeException(requestId+"线程申请资源失败!");        }}private boolean checkSafety(){        int work[]=new int[resourceCount];        boolean finish[]=new boolean[processCount];        int queue[]=new int[processCount];        Map<Integer, Integer> rest = new LinkedHashMap<Integer, Integer>();        for(int i=0;i<resourceCount;i++){        work[i] = available[i];        }        for(int i=0;i<processCount;i++){        finish[i] = false;        rest.put(i, i);        }        int processId = 0;        int count = 0;        int noChange = 0;        while(rest.size() != 0){        if(finish[processId]){        processId = (processId + 1) % processCount;        continue;        }        boolean p = true;        for(int i=0;i<resourceCount;i++){        if(need[processId][i] > work[i]){        p = false;        break;        }        }        if(p){        rest.remove(processId);        for(int i=0;i<resourceCount;i++){        work[i] += allocation[processId][i];        }        queue[count] = processId;        finish[processId] = true;        count++;        noChange = 0;        }else{        noChange++;        if(noChange == rest.size()){        System.out.println("不存在安全序列!");        return false;        }        }        processId = (processId + 1) % processCount;        }        System.out.println("存在安全序列:");        for(int i=0;i<processCount;i++){        if(i != 0){        System.out.print(",");        }        System.out.print(queue[i]);        }        System.out.println();        return true;}public void showData(){System.out.println("need:");for(int i=0;i<processCount;i++){for(int j=0;j<resourceCount;j++){if(j != 0){System.out.print("   ");}System.out.print(need[i][j]);}System.out.println();}System.out.println("available:");for(int i=0;i<resourceCount;i++){if(i != 0){System.out.print("   ");}System.out.print(available[i]);}System.out.println();}}

————————————————————


Main.java

————————————————————

public class Main {public static void main(String[] args) {MyBanker mb = new MyBanker();Scanner s = new Scanner(System.in);mb.initData();boolean p = true;while(p){try {System.out.println("请输入你要进行的操作:(1:申请资源。2:显示资源。0:退出。)");int cmd = s.nextInt();switch(cmd){case 1:{mb.requestResource();};break;case 2:{mb.showData();};break;case 0:{p = false;};break;}} catch (Exception e) {System.out.println(e.getMessage());}}}}
————————————————————

0 0