-
Notifications
You must be signed in to change notification settings - Fork 0
/
banker.c
133 lines (106 loc) · 3.15 KB
/
banker.c
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
#include <stdio.h>
#include <stdbool.h>
int main() {
int curr[10][10];
int max_claim[10][10];
int need[10][10];
int avl[5];
int alloc[5] = {0, 0, 0, 0, 0};
int max_res[5];
int running[5];
int safestate[5];
int ctr=0;
int i, j, exec, r, p;
int count = 0;
bool safe = false;
printf("\nEnter the number of resources: ");
scanf("%d", &r);
printf("\nEnter the number of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++) {
running[i] = 1;
count++;
}
printf("\nEnter Available Vector: ");
for (i = 0; i < r; i++)
scanf("%d",&avl[i]);
printf("\nEnter Allocated Resource Table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
scanf("%d", &curr[i][j]);
}
printf("\nEnter Maximum Claim table: ");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
scanf("%d", &max_claim[i][j]);
}
for (int i = 0 ; i < p ; i++){
for (int j = 0 ; j < r ; j++){
// Need of instance = maxm instance -
// allocated instance
need[i][j] = max_claim[i][j] - curr[i][j];}}
printf("\nThe Allocated Resource Table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
printf("\t%d", curr[i][j]);
printf("\n");
}
printf("\nThe Maximum Claim Table:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
printf("\t%d", max_claim[i][j]);
printf("\n");
}
printf ("Need matrix is \n");
for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
printf("\t%d", need[i][j]);
printf("\n");
}
for (i = 0; i < p; i++)
for (j = 0; j < r; j++)
alloc[j] += curr[i][j];
printf("\nAllocated resources: ");
for (i = 0; i < r; i++)
printf("%d ", alloc[i]);
printf("\nAvailable resources: ");
for (i = 0; i < r; i++)
printf("%d ", avl[i]);
printf("\n");
while (count != 0) {
safe = false;
for (i = 0; i < p; i++) {
//ctr++;
if (running[i]) {
ctr++;
exec = 1;
for (j = 0; j < r; j++) {
if (max_claim[i][j] - curr[i][j] > avl[j]) {
exec = 0;
break;
}
}
if (exec) {
printf("\nProcess%d is executing.\n", i + 1);
running[i] = 0;
count--;
safe = true;
//ctr++;
for (j = 0; j < r; j++)
avl[j] += curr[i][j];
}
}
}
if (!safe) {
printf("\nThe processes are in unsafe state.");
break;
}
}
if (safe)
printf("\nThe process is in safe state.");
printf("\nAvailable vector: ");
for (i = 0; i < r; i++)
printf("%d ", avl[i]);
printf("\n\n total no. of iteration in unsafe else take it as in simple case %d",ctr);
return 0;
}