-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha7q2.c
88 lines (78 loc) · 1.12 KB
/
a7q2.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
#include<stdio.h>
void main()
{
int max[3][3],allocation[3][3],need[3][3],work[3],available[3],finish[3]={0,0,0};
int i,j,c,k,check;
printf("Enter the maximum requirment of the processes\n");
for(i=0;i<3;i++)
{
printf("PROCESS %d\n",i);
for(j=0;j<3;j++)
{
printf("Resource %d\n",j);
scanf("%d",&max[i][j]);
}
}
printf("Enter the allocation of the processes");
for(i=0;i<3;i++)
{
printf("PROCESS %d\n",i);
for(j=0;j<3;j++)
{
printf("Resource %d\n",j);
scanf("%d",&allocation[i][j]);
}
}
printf("Enter the available resources\n");
for(i=0;i<3;i++)
{
printf("Resource %d\n",i);
scanf("%d",&available[i]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
for(i=0;i<3;i++)
{
work[i]=available[i];
}
//SAfety Algo.
for(i=0;i<3;i++)
{
if (finish[i]==0)
{
c=0;
for(j=0;j<3;j++)
{
if(need[i][j]<=work[j])
{
c++;
}
}
if(c==3)
{
for(k=0;k<3;k++)
{
work[k]=work[k]+allocation[i][k];
}
finish[i]=1;
}
}
}
check=0;
printf("The finish matrix\n");
for(i=0;i<3;i++)
{
printf("%d\n",finish[i]);
if(finish[i]==1)
check++;
}
if(check==3)
printf("The state is safe \n");
else
printf("The state is unsafe\n");
}