-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankers.c
90 lines (89 loc) · 2.07 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
#include<stdio.h>
int main()
{
int no_p, no_r, alloc[50][50], max[50][50], need[50][50], avail[50], work[50], finish[50], count=0, safe[50];
printf("Enter the number of processes: ");
scanf("%d",&no_p);
printf("Enter the number of resources: ");
scanf("%d",&no_r);
printf("Enter the allocation matrix: ");
for(int i=0;i<no_p;i++)
{
for(int j=0;j<no_r;j++)
{
printf("ALLOCATION OF P%d R%d: ",i,j);
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the max matrix: ");
for(int i=0;i<no_p;i++)
{
for(int j=0;j<no_r;j++)
{
printf("MAX OF P%d R%d: ",i,j);
scanf("%d",&max[i][j]);
}
}
printf("Enter the available matrix: ");
for(int i=0;i<no_r;i++)
{
printf("AVAILABLE OF R%d: ",i);
scanf("%d",&avail[i]);
}
for(int i=0;i<no_p;i++)
{
for(int j=0;j<no_r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
for(int i=0;i<no_r;i++)
{
work[i]=avail[i];
}
for(int i=0;i<no_p;i++)
{
finish[i]=0;
}
for(int i=0;i<no_p;i++)
{
for(int j=0;j<no_p;j++)
{
if(finish[j]==0)
{
int flag=0;
for(int k=0;k<no_r;k++)
{
if(need[j][k]>work[k])
{
flag=1;
break;
}
}
if(flag==0)
{
for(int k=0;k<no_r;k++)
{
work[k]+=alloc[j][k];
}
finish[j]=1;
safe[count++]=j;
}
}
}
}
if(count==no_p)
{
printf("The system is in safe state\n");
printf("The safe sequence is: ");
for(int i=0;i<no_p;i++)
{
printf("P%d ",safe[i]);
}
}
else
{
printf("The system is in unsafe state\n");
}
return 0;
}