-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankers.c
124 lines (106 loc) · 1.92 KB
/
bankers.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define N 100
typedef struct
{
int max;
int need;
int allocated;
int available;
}table;
int main()
{
system("clear");
int n, m, p, a[N];
table t[N][N];
int total[N], done[N];
printf("Enter number of processes : ");
scanf("%d", &n);
printf("Enter number of types of resources : ");
scanf("%d", &m);
printf("Enter total available resources of all types\n");
for(int i=0; i<m; i++)
scanf("%d", total+i);
printf("\nEnter for each process the number of resources needed at max,\n");
printf("and that currently available for each type\n");
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
scanf("%d", &t[i][j].max);
for(int j=0; j<m; j++)
{
scanf("%d", &t[i][j].allocated);
t[i][j].need = t[i][j].max - t[i][j].allocated;
total[j] -= t[i][j].allocated;
}
done[i]=0;
}
printf("Enter process id and request : ");
scanf("%d", &p);
p--;
int fl=1;
for(int i=0; i<m; i++)
{
scanf("%d", a+i);
if(a[i] > t[p][i].need)
fl=0;
}
if(fl)
{
for(int i=0; i<m; i++)
{
total[i] -= a[i];
t[p][i].allocated += a[i];
t[p][i].need -= a[i];
}
}
else
{
printf("\n\nRequest not granted!!\n\n");
return 0;
}
printf("\n\nOrder of execution : ");
for(int z=0; z<n; z++)
{
int pos=-1;
for(int j=0; j<n; j++)
{
int flag=1;
for(int k=0; k<m; k++)
if(total[k] < t[j][k].need)
{
flag=0;
break;
}
if(flag && done[j] != 1)
{
pos=j;
break;
}
}
printf("%d ", pos+1);
if(pos==-1)
break;
done[pos]=1;
for(int j=0; j<m; j++)
{
t[pos][j].available = total[j];
total[j] += t[pos][j].allocated;
}
}
printf("\n\nProcess\t\tAvailable Resources\n");
for(int i=0; i<n; i++)
{
printf("%d\t\t", i+1);
if(done[i]==0)
{
printf("Never allocated resources\n");
continue;
}
for(int j=0; j<m; j++)
printf("%d ", t[i][j].available);
printf("\n");
}
printf("\n");
}