-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathBankers_algorithm.c
118 lines (118 loc) · 2.87 KB
/
Bankers_algorithm.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
#include <stdio.h>
#include <stdlib.h>
void final_output(int k[][10], int n, int p)
{
int i, j;
for (i = 0; i < n; i++)
{
printf("\n");
for (j = 0; j < p; j++)
{
printf("%d\t", k[i][j]);
}
}
}
//Banker's Algorithm
void Banker(int A[][10], int N[][10],
int M[10][10], int W[1][10], int *n, int *m)
{
int i, j;
printf("\n Enter total number of processes : ");
scanf("%d", n);
printf("\n Enter total number of resources : ");
scanf("%d", m);
for (i = 0; i < *n; i++)
{
printf("\n Process %d\n", i + 1);
for (j = 0; j < *m; j++)
{
printf(" Allocation for resource %d : ", j + 1);
scanf("%d", &A[i][j]);
printf(" Maximum for resource %d : ", j + 1);
scanf("%d", &M[i][j]);
}
}
printf("\n Available resources : \n");
for (i = 0; i < *m; i++)
{
printf(" Resource %d : ", i + 1);
scanf("%d", &W[0][i]);
}
for (i = 0; i < *n; i++)
for (j = 0; j < *m; j++)
N[i][j] = M[i][j] - A[i][j];
printf("\nAllocation Matrix");
final_output(A, *n, *m);
printf("\nMaximum Requirement Matrix");
final_output(M, *n, *m);
printf("\nNeed Matrix");
final_output(N, *n, *m);
}
//Safety algorithm
int safety(int A[][10], int N[][10],
int B[1][10], int n, int m, int a[])
{
int i, j, k, x = 0, f1 = 0, f2 = 0;
int F[10], W[1][10];
for (i = 0; i < n; i++)
F[i] = 0;
for (i = 0; i < m; i++)
W[0][i] = B[0][i];
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (F[i] == 0)
{
f2 = 0;
for (j = 0; j < m; j++)
{
if (N[i][j] > W[0][j])
f2 = 1;
}
if (f2 == 0 && F[i] == 0)
{
for (j = 0; j < m; j++)
W[0][j] += A[i][j];
F[i] = 1;
f1++;
a[x++] = i;
}
}
}
if (f1 == n)
return 1;
}
return 0;
}
int banker(int A[][10], int N[][10], int W[1][10], int n, int m)
{
int j, i, a[10];
j = safety(A, N, W, n, m, a);
if (j != 0)
{
printf("\n\n");
printf("\n A safety sequence has been detected.\n");
for (i = 0; i < n; i++)
{
printf(" P%d ", a[i]);
printf("\n");
}
return 1;
}
else
{
printf("\n Deadlock has occured.\n");
return 0;
}
}
int main()
{
int All[10][10], Max[10][10], Need[10][10], W[1][10];
int n, m, pid, c, r;
printf("\nDEADLOCK AVOIDANCE USING BANKER'S ALGORITHM\n");
printf("-----------------------------------------------\n");
Banker(All, Need, Max, W, &n, &m);
r = banker(All, Need, W, n, m);
return 0;
}