-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMahfuzBankerAlgorithm.c
94 lines (85 loc) · 2.26 KB
/
MahfuzBankerAlgorithm.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
// CPU-Scheduling-Algorithm-In-C
// Bankers Algorithm
#include<stdio.h>
void main()
{
int process, resource, i, j, instance, k=0, count=0, temp=0;
printf("\n Enter No. of Process : ");
scanf("%d", &process);
printf(" Enter No. of Resources : ");
scanf("%d",&resource);
int available[resource], max[process][resource], allocated[process][resource], need[process][resource], completed[process];
for(i=0; i<process; i++)
completed[i] = 0;
printf("\n Enter No. of AVAILABLE Instances for each resource\n\n");
for(i=0; i<resource;i++)
{
printf(" Resources[%d] : ", i);
scanf("%d", &instance);
available[i] = instance;
}
printf("\n------------------------------------------------------------------------------\n");
printf("\n Enter MAXIMUM instance for a Process & its corresponding resource :\n");
for(i=0; i<process; i++)
{
printf("\n For Process[%d] \n", i);
for(j=0; j<resource; j++)
{
printf(" Resource[%d] : ", j);
scanf("%d", &instance);
max[i][j] = instance;
}
}
printf("\n------------------------------------------------------------------------------\n");
printf("\n Enter instance ALLOCATED for a Process & its corresponding resource :\n");
for(i=0; i<process; i++)
{
printf("\n For Process[%d] \n", i);
for(j=0; j<resource; j++)
{
printf(" Resource[%d] : ", i);
scanf("%d", &instance);
allocated[i][j] = instance;
need[i][j] = max[i][j] - allocated[i][j];
}
}
printf("\n------------------------------------------------------------------------------\n");
printf("\nSafe Sequence is : ");
while(count != process)
{
count = temp;
for(i=0; i<process; i++)
{
for(j=0; j<resource; j++)
{
if(need[i][j] <= available[j])
{
k++;
}
}
if(k == resource && completed[i] == 0 )
{
printf(" > P[%d] ", i);
completed[i] = 1;
for(j=0; j<resource; j++)
{
available[j] = available[j] + allocated[i][j];
}
count++;
}
k=0;
}
if(count == temp)
{
break;
}
}
for(i=0; i<process; i++)
{
if(completed[i] != 1)
{
printf("\n\n P[%d] not able to allocate", i);
}
}
printf("\n");
}