下面是资源请求的代码。我的安全算法运行良好,但是当我要求额外的资源时,它给出了错误情况(请求>需要)。但是当我实际这样做时,我找不到任何错误。
这是我的代码
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
request();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\t");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}
void cal() {
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\n The system is in safe state");
} else {
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
void request() {
int c, pid, request[100][100], B[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n ******Error encountered******\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
getch();
} else {
exit(0);
}
}
上述代码的输出:
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
Process Allocation Max Available
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
P1->P3->P4->P2->P0->
The system is in safe state
Do you want make an additional request for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
******Error encountered******
需要的输出:
*********DEADLOCK AVOIDANCE USINGBANKER'S ALGORITHM***********
Enter total number of processes : 5
Enter total number of resources : 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0
2
2 1 1
0 0 2
Available resources :
Resource 1 : 3
Resource 2 : 3
Resource 3 : 2
********Maximum Requirement Matrix*********
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
********Allocation Matrix**********
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A safety sequence has been detected.
P1 P3 P4 P0 P2
Do you want make an additionalrequest for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
A safety sequence has been detected.
P1 P3 P4 P0 P2
我在哪里可以进行更改以获得上述输出。