-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanker.c
75 lines (75 loc) · 2.22 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
#include <stdio.h>
void main()
{
int k = 0, a = 0, b = 0, instance[5], availability[5], allocated[10][5], need[10][5],
MAX[10][5], process, P[10], no_of_resources, cnt = 0, i, j, op[10];
printf("\n Enter the number of resources : ");
scanf("%d", &no_of_resources);
printf("\n enter the max instances of each resources\n");
for (i = 0; i < no_of_resources; i++)
{
availability[i] = 0;
printf("%c= ", (i + 97));
scanf("%d", &instance[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &process);
printf("\n Enter the allocation matrix \n");
for (i = 0; i < no_of_resources; i++)
printf(" %c", (i + 97));
printf("\n");
for (i = 0; i < process; i++)
{
P[i] = i;
printf("P[%d] ", P[i]);
for (j = 0; j < no_of_resources; j++)
{
scanf("%d", &allocated[i][j]);
availability[j] += allocated[i][j];
}
}
printf("\nEnter the MAX matrix \n ");
for (i = 0; i < no_of_resources; i++)
{
printf(" %c", (i + 97));
availability[i] = instance[i] - availability[i];
}
printf("\n");
for (i = 0; i < process; i++)
{
printf("P[%d] ", i);
for (j = 0; j < no_of_resources; j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A:
a = -1;
for (i = 0; i < process; i++)
{
cnt = 0;
b = P[i];
for (j = 0; j < no_of_resources; j++)
{
need[b][j] = MAX[b][j] - allocated[b][j];
if (need[b][j] <= availability[j])
cnt++;
}
if (cnt == no_of_resources)
{
op[k++] = P[i];
for (j = 0; j < no_of_resources; j++)
availability[j] += allocated[b][j];
}
else
P[++a] = P[i];
}
if (a != -1)
{
process = a + 1;
goto A;
}
printf("\t <");
for (i = 0; i < k; i++)
printf(" P[%d] ", op[i]);
printf(">");
}