-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanker_algorithm.c
More file actions
191 lines (173 loc) · 5.38 KB
/
banker_algorithm.c
File metadata and controls
191 lines (173 loc) · 5.38 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
// gcc banker_algorithm.c -o banker_algorithm.o; ./banker_algorithm.o
/*
/ Banker's Algorithm
1. Deadlock avoidance algorithm for multiple isntance resources and processes
2. Consists of three components:
i. Safety algorithm - Checks if system is in safe state or not
ii. Request checking algorithm - Checks if a process may be granted
a request vector without entering unsafe
state.
iii. Deadlock handling algorithm - Breaks a deadlock condition by iteratively
preempting processes and freeing their resources.
3. Takes input from text file.
*/
int safety(int m, int n, int resource_instances[m], int allocated[n][m], int max_needed[n][m], int needed[n][m])
{
printf("------\nSafe state checking:\n\n");
int available[m];
for (int i = 0; i < m; i++)
available[i] = resource_instances[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
available[j] -= allocated[i][j];
int work[m];
for (int i = 0; i < m; i++)
work[i] = available[i];
int finished[n];
for (int i = 0; i < n; i++)
finished[i] = 0;
int flag, finished_sum = 0;
int safe_sequence[n], safe_ind = 0;
while (finished_sum < n)
{
flag = 0;
int temp = -1;
for (int i = 0; i < n; i++)
{
if (finished[i] == 0)
{
int temp2 = 1;
for (int j = 0; j < m; j++)
if (work[j] < needed[i][j])
{
temp2 = 0;
break;
}
if (temp2)
{
temp = i;
break;
}
flag++;
}
}
if (flag == n || temp == -1)
{
printf("Unsafe\n------\n");
return 0;
}
finished[temp] = 1;
finished_sum++;
safe_sequence[safe_ind++] = temp;
for (int i = 0; i < m; i++)
{
work[i] += allocated[temp][i];
allocated[temp][i] = 0;
}
}
printf("Safe sequence: ");
for (int k = 0; k < n; k++)
printf("%d ", safe_sequence[k]);
printf("\n------\n");
return 1;
}
int request_check(int m, int n, int resource_instances[m], int allocated[n][m], int max_needed[n][m], int needed[n][m], int pind, int request[m])
{
printf("Request checking:\n\n");
int available[m];
for (int i = 0; i < m; i++)
available[i] = resource_instances[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
available[j] -= allocated[i][j];
// int flag = 1;
for (int i = 0; i < n; i++)
{
if (available[i] < request[i] || request[i] > needed[pind][i])
{
return 0;
}
}
for (int i = 0; i < n; i++)
{
allocated[pind][i] += request[i];
available[i] -= request[i];
}
return safety(m, n, resource_instances, allocated, max_needed, needed);
}
int deadlock_handle(int m, int n, int resource_instances[m], int allocated[n][m], int max_needed[n][m], int needed[n][m])
{
printf("Deadlock resolution:\n\n");
if (safety(m, n, resource_instances, allocated, max_needed, needed))
return 1;
int preempted = 0;
for (; preempted < n; preempted++)
{
for (int i = 0; i < m; i++)
{
allocated[preempted][i] = 0;
needed[preempted][i] = max_needed[preempted][i];
}
printf("Preempted process %d.\n", preempted);
if (safety(m, n, resource_instances, allocated, max_needed, needed))
{
printf("Deadlock resolved.\n");
return 1;
}
}
printf("Deadlock could not be resolved.\n");
return 0;
}
int main()
{
FILE *f = fopen("banker_algo_in.txt", "r");
int m;
fscanf(f, "%d", &m);
int resource_instances[m];
for (int i = 0; i < m; i++)
fscanf(f, "%d ", &resource_instances[i]);
int n;
fscanf(f, "%d ", &n);
int allocated[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
fscanf(f, "%d ", &allocated[i][j]);
int max_needed[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
fscanf(f, "%d ", &max_needed[i][j]);
int needed[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
needed[i][j] = max_needed[i][j] - allocated[i][j];
int ans = safety(m, n, resource_instances, allocated, max_needed, needed);
int pind;
fscanf(f, "%d", &pind);
int request[m];
for (int i = 0; i < m; i++)
{
fscanf(f, "%d ", &request[i]);
}
if (request_check(m, n, resource_instances, allocated, max_needed, needed, pind, request))
{
printf("Request can be granted.\n---\n");
for (int i = 0; i < m; i++)
{
allocated[pind][i] += request[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
needed[i][j] = max_needed[i][j] - allocated[i][j];
}
else
printf("Request cannot be granted.\n---\n");
if (!ans)
deadlock_handle(m, n, resource_instances, allocated, max_needed, needed);
fclose(f);
return 0;
}