-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank-req.c
More file actions
110 lines (93 loc) · 2.59 KB
/
Copy pathBank-req.c
File metadata and controls
110 lines (93 loc) · 2.59 KB
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
#include <stdio.h>
int main() {
int nop, nor, i, j, k;
int alloc[100][100] = {0}, max[100][100] = {0}, need[100][100] = {0};
int resources[50] = {0}, avlbl[50];
int finished[50] = {0};
printf("Enter the number of process : ");
scanf("%d", &nop);
printf("Enter the number of resources : ");
scanf("%d", &nor);
printf("Enter the allocation matrix\n");
for(i = 1; i <= nop; i++) {
for(j = 1; j <= nor; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the maximum allocation matrix\n");
for(i = 1; i <= nop; i++) {
for(j = 1; j <= nor; j++) {
scanf("%d", &max[i][j]);
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("Enter the total resource instance : ");
for(i = 1; i <= nor; i++) {
scanf("%d", &resources[i]);
}
for(i = 1; i <= nor; i++) {
int rsum = 0;
for(j = 1; j <= nop; j++) {
rsum += alloc[j][i];
}
avlbl[i] = resources[i] - rsum;
}
int count = 0;
printf("Safe sequence : ");
while(1) {
int progress = 0;
for(i = 1; i <= nop; i++) {
if(finished[i] == 0) {
for(j = 1; j <= nor; j++) {
if(avlbl[j] < need[i][j]) {
break;
}
}
if(j > nor) {
for(k = 1; k <= nor; k++) {
avlbl[k] += alloc[i][k];
}
count++;
finished[i] = 1;
progress = 1;
printf("P%d ", i);
}
}
}
if(!progress || count >= nop) {
break;
}
}
if(count == nop) {
printf("No deadlock\n");
} else {
printf("Deadlock\n");
}
int pid;
int request[50];
printf("Enter the process number : ");
scanf("%d", &pid);
printf("Enter the request : ");
for(i = 1; i <= nor; i++) {
scanf("%d", &request[i]);
}
for(i = 1; i <= nor; i++) {
if(request[i] > need[pid][i]) {
printf("Request denied\n");
return 0;
}
}
for(i = 1; i <= nor; i++) {
if(request[i] > avlbl[i]) {
printf("Need to wait\n");
return 0;
}
}
for(i = 1; i <= nor; i++) {
avlbl[i] -= request[i];
alloc[pid][i] += request[i];
need[pid][i] -= request[i];
}
printf("Request granted\n");
return 0;
}