银行家算法实现

操作系统编程小作业

银行家算法详细讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int process_MAXN = 5; // the process number
const int resource_MAXN = 3; // the resource number


struct bankAlgorithm
{
private:
int Available[resource_MAXN] = {2,3,0};
int tmpAvailable[process_MAXN][resource_MAXN]; //
int Max[process_MAXN][resource_MAXN] = { {7, 5, 3},{3, 2, 1},{9, 0, 2},{2, 2, 2},{4, 3, 3} };
int Allocation[process_MAXN][resource_MAXN] = {{0, 1, 0},{2, 0, 0},{3, 0, 2},{2,1,1},{0, 0, 2}};
int Need[process_MAXN][resource_MAXN] = {{7, 4, 3},{1, 2, 2},{6, 0, 0},{0, 1, 1},{4, 3, 1}};
int processId = 1;
int request[3] = {1,0,2};
int visit[process_MAXN][3] = {{0}}; // the priority of visit
public:
void mainCtrl();
void sayHelloworld();
void inputData();
void outputData();
bool sendRequest();
void tryToAskResource();
void showInformation();
void showError();
};

void bankAlgorithm::mainCtrl()
{
inputData();
outputData();
sendRequest();
}
void bankAlgorithm::sayHelloworld()
{
printf("\thello the cruel world\n");
}
void bankAlgorithm::inputData()
{
printf("input the process id and the three request resource\n");
scanf("%d%d%d%d",&processId,&request[0],&request[1],&request[2]);

}
void bankAlgorithm::outputData()
{
printf("ProcessId:%d\nrequest:",processId);
for(int i = 0; i < resource_MAXN; i++) {
printf("%d ",request[i]);
}
printf("\n");
}


bool bankAlgorithm::sendRequest() //no bug
{
for(int i = 0; i < resource_MAXN; i++) {
if(request[i] > Need[processId][i]) {
printf("The requested resource has exceeded the required resource\n");
return false;
}
if(request[i] > Available[i]) {
printf("The requested resource has exceeded the Available resource\n");
return false;
}
}

tryToAskResource();
}
void bankAlgorithm::tryToAskResource()
{
for(int i = 0; i < resource_MAXN; i++) {
Available[i] -= request[i];
Need[processId][i] -= request[i];
Allocation[processId][i] += request[i];
}

int cnt = 0; //the visitted priority
for(int i = 0; i < process_MAXN; i++) {
visit[i][0] = 0;
}
while(true) {
bool judgeToRun = true;
for(int i = 0; i < process_MAXN; i++) {
if(!visit[i][0]) {
bool flag = true;
for(int j = 0; j < resource_MAXN; j++) {
if(Available[j] < Need[i][j]) {
flag = false;
break;
}
}
if(flag) {
for(int j = 0; j < resource_MAXN; j++) {
tmpAvailable[i][j] = Available[j];
Available[j] += Allocation[i][j];
}
judgeToRun = false;
visit[i][0] = 1;
visit[i][1] = cnt++;
visit[i][2] = i;
// cout<<"i = "<<i<<endl;
}
}
}
if(judgeToRun) {break;}
}
// sayHelloworld();
// cout<<cnt<<' '<<process_MAXN<<endl;
if(cnt != process_MAXN) {
showError();
return;
}
showInformation();
}

void bankAlgorithm::showInformation()
{
printf("Resource\tMax\tAvailable Allocation Need Work\n");
int tmp[3];
int len = sizeof(visit[0]);
for(int i = 0; i < process_MAXN; i++) {
for(int j = i + 1; j < process_MAXN; j++) {
if(visit[i][1] > visit[j][1]) {

memcpy(tmp,visit[i],len);
memcpy(visit[i],visit[j],len);
memcpy(visit[j],tmp,len);
}
}
}
for(int i = 0; i < process_MAXN; i++) {
int process_id = visit[i][2];
printf("\t%d ",process_id);
printf("\t");
for(int j = 0; j < resource_MAXN; j++) {
printf("%d ",Max[process_id][j]);
}
printf("\t ");
for(int j = 0; j < resource_MAXN; j++) {
printf("%d ",tmpAvailable[process_id][j]);
}
printf("\t");
for(int j = 0; j < resource_MAXN; j++) {
printf("%d ",Allocation[process_id][j]);
}
printf("\t ");
for(int j = 0; j < resource_MAXN; j++) {
printf("%d ",Need[process_id][j]);
}
printf("\t");
for(int j = 0; j < resource_MAXN; j++) {
printf("%d ",tmpAvailable[process_id][j] + Allocation[process_id][j]);
}
printf("\n");
}
}
void bankAlgorithm::showError()
{
printf("Sorry,you can't allocate resources to %d.\nBecause the Available resource can't satisfy any of the Process\n",processId);
}
int main()
{
struct bankAlgorithm shan;
shan.mainCtrl();
return 0;
}

-------------本文结束感谢您的阅读-------------
0%