-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbankersproblem.c
94 lines (92 loc) · 1.98 KB
/
bankersproblem.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
// code to simulate bankers problem
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, m, i, j, k;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);
int alloc[n][m], maxmat[n][m], need[n][m], avail[m], finish[n], sequence[n];
printf("Enter allocation matrix: \n");
for(i = 0;i<n;i++)
{
for(j = 0;j<m;j++)
{
scanf("%d", &alloc[i][j]);
}
}
printf("Enter max matrix: \n");
for(i = 0;i<n;i++)
{
for(j = 0;j<m;j++)
{
scanf("%d", &maxmat[i][j]);
}
}
printf("Enter available matrix: \n");
for(i = 0;i<m;i++)
{
scanf("%d", &avail[i]);
}
for(i = 0;i<n;i++)
{
finish[i] = 0;
}
for(i = 0;i<n;i++)
{
for(j = 0;j<m;j++)
{
need[i][j] = maxmat[i][j] - alloc[i][j];
}
}
int count = 0;
for(i = 0;i<n;i++)
{
for(j = 0;j<n;j++)
{
if(finish[j] == 0)
{
int flag = 0;
for(k = 0;k<m;k++)
{
if(need[j][k] > avail[k])
{
flag = 1;
break;
}
}
if(flag == 0)
{
sequence[count++] = j;
finish[j] = 1;
for(k = 0;k<m;k++)
{
avail[k] += alloc[j][k];
}
}
}
}
}
int flag = 0;
for(i = 0;i<n;i++)
{
if(finish[i] == 0)
{
flag = 1;
printf("Unsafe system\n");
break;
}
}
if(flag == 0)
{
printf("Safe system\n");
printf("Safe sequence: ");
for(i = 0;i<n;i++)
{
printf("P%d ", sequence[i]);
}
printf("\n");
}
}