给个赞是对我最好的肯定~
1 using System;
2
3 namespace 银行家算法
4 {
5 class MainClass
6 {
7 public class Banker {
8 static int[] available = new int[4]; //资源数
9 static int[,] max = new int[5,4]; //最大资源需求
10 static int[,] allocation = new int[5,4]; //已经分配的资源
11 static int[,] need = new int[5,4]; //还需要的资源
12 static int[] request = new int[4]; //存放请求
13
14 int thread; //线程号
15
16 //初始化各类的值
17 public void getData(){
18 Console.Write("请输入A,B,C,D四类资源的数目:");
19 //输入A,B,C三类资源数量
20 for(int i = 0; i < 4; i++){
21 available [i] = int.Parse (Console.ReadLine ());
22 }
23 //输入进程对三类资源的最大需求
24 for(int i = 0; i < 5; i++){
25 Console.WriteLine("请输入进程" + i + "对A,B,C,D三类资源的最大需求");
26 for(int j = 0; j < 4; j++){
27 max [i,j] = int.Parse (Console.ReadLine ());
28 }
29 }
30 //输入进程分配的三类资源数
31 for(int i = 0; i < 5; i++){
32 Console.WriteLine("请输入进程" + i + "已分配的A,B,C,D三类资源数");
33 for(int j = 0; j < 4; j++){
34 allocation[i,j] = int.Parse (Console.ReadLine ());
35 }
36 }
37 //计算进程还需要的三类资源数
38 for(int i = 0; i < 5; i++){
39 for(int j = 0; j < 4; j++){
40 need[i,j] = max[i,j] - allocation[i,j];
41 }
42 }
43 //重新计算available
44 for(int i = 0; i < 4; i++){
45 for(int j = 0; j < 5; j++){
46 available[i] -= allocation[j,i];
47 }
48 }
49 }
50
51 //用户输入要申请资源的线程和申请的资源,并进行判断
52 public void getThread(){
53 Console.WriteLine("请输入申请资源的线程");
54 int thread = int.Parse(Console.ReadLine()); //线程
55 if(thread < 0 || thread > 4){
56 Console.WriteLine("该线程不存在,请重新输入");
57 getThread();
58 }else{
59 this.thread = thread;
60 Console.WriteLine("请输入申请的资源(0-4)");
61 for(int i = 0; i < 4; i++){
62 request[i] = int.Parse(Console.ReadLine());
63 }
64 if(request[0] > need[thread,0] || request[1] > need[thread,1] || request[2] > need[thread,2] || request[3] > need[thread,3]){
65 Console.WriteLine(thread+"线程申请的资源超出其需要的资源,请重新输入");
66 getThread();
67 }else{
68 if(request[0] > available[0] || request[1] > available[1] || request[2] > available[2] || request[3] > available[3]){
69 Console.WriteLine(thread + "线程申请的资源大于系统资源,请重新输入");
70 getThread();
71 }
72 }
73 changeData(thread);
74 if(check(thread)){
75 getThread();
76 }else{
77 recoverData(thread);
78 getThread();
79 }
80
81 }
82 }
83
84 //thread线程请求响应后,试探性分配资源
85 public void changeData(int thread){
86 for(int i = 0; i < 4; i++){
87 //重新调整系统资源数
88 available[i] -= request[i];
89 //计算各个线程拥有资源
90 allocation[thread,i] += request[i];
91 //重新计算需求
92 need[thread,i] -= request[i];
93 }
94 }
95
96 //安全性检查为通过,分配失败时调用,恢复系统原状
97 public void recoverData(int thread){
98 for(int i = 0; i < 4; i++){
99 //重新调整系统资源数
100 available[i] += request[i];
101 //计算各个线程拥有资源
102 allocation[thread,i] -= request[i];
103 //重新计算需求
104 need[thread,i] += request[i];
105 }
106 }
107
108 //对线程thread安全性检查
109 public bool check(int thread){
110 bool[] finish = new bool[5];
111 int[] work = new int[4];
112 int[] queue = new int[5]; //由于存放安全队列
113 int k = 0;//安全队列下标
114 int j; //要判断的线程
115 int i;
116 //是否分配的标志
117 for( i = 0; i < 5; i++)
118 finish[i] = false;
119 j = thread;
120 for(i = 0; i < 4; i++){
121 work[i] = available[i];
122 }
123 while(j < 5){
124 for( i = 0; i < 4; i++){
125 if(finish[j]){
126 j++;
127 break;
128 }else if(need[j,i] > work[i]){
129 //(need[j,i]+"*"+i+work[i]);
130 j++;
131 break;
132 }else if(i == 2){
133 for(int m = 0; m < 4; m++){
134 work[m] += allocation[j,m];
135 }
136 finish[j] = true;
137 queue[k] = j;
138 k++;
139 j = 0; //从最小线程再开始判断
140 }
141 }
142 }
143 //判断是否都属于安全状态
144 for(int p = 0; p < 5; p++){
145 if(finish[p] == false){
146 Console.WriteLine("系统不安全,资源申请失败");
147 return false;
148 }
149 }
150 Console.WriteLine("资源申请成功,安全队列为:");
151 for(int q = 0; q < 5; q++){
152 Console.WriteLine (queue [q]);
153 }
154 return true;
155 }
156
157 //输出need和available
158 public void showData(){
159 Console.WriteLine("need");
160 for(int i = 0; i < 5; i++){
161 for(int j = 0; j < 4; j++){
162 Console.WriteLine (need [i, j] + " ");
163 }
164 }
165 Console.WriteLine("available");
166 for(int j = 0; j < 4; j++){
167 Console.WriteLine(available[j] + " ");
168 }
169 }
170
171 static void Main(string[] args) {
172 Banker bk = new Banker();
173 bk.getData();
174 bk.getThread();
175 }
176
177 }
178
179 }
180 }