-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankers.c
110 lines (87 loc) · 1.71 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
#include<stdio.h> //verified
#include<stdlib.h>
int main()
{
int p,r;
printf("Enter the no of processes");
scanf("%d",&p);
printf("Enter the no of resources");
scanf("%d",&r);
int alloc[p][r],max[p][r],need[p][r];
int finish[p],available[r],work[r],safeseq[p];
printf("Enter the allocated matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
finish[i]=0;
}
printf("Enter the max matrix");
for(int i=0;i<p;i++)
{
for(int j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the available resources");
for(int i=0;i<r;i++)
{
scanf("%d",&available[i]);
work[i]=available[i];
}
printf("The need matrix is ");
for(int i=0;i<p;i++)
{
for(int j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d ",need[i][j]);
}
printf("\n");
}
int over=0;
int flag=0;
int k=0;
while(over<p)
{
for(int i=0;i<p;i++)
{
flag=0;
if(finish[i]==0)
{
for(int j=0;j<r;j++)
{
if(need[i][j]<=work[j])
{
flag++;
}
}
}
if(flag==r)
{
for(int j=0;j<r;j++)
{
work[j]=work[j]+alloc[i][j];
}
safeseq[over]=i+1;
finish[i]=1;
over++;
//printf("%d",safeseq[i]);
}
}
}
if(over==p)
{
printf("The safe sequence is \n");
for(int i=0;i<p;i++)
{
printf("%d ",safeseq[i]);
}
}
else{
printf("The system is not safe");
}
}