-
Notifications
You must be signed in to change notification settings - Fork 0
/
ba.c
120 lines (117 loc) · 2.74 KB
/
ba.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
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>
void main(){
int available[100],max[20][100],need[20][100],allocation[20][100],flag,finish[20];
int i,np,nr,j,safe[20],k=0;
printf("Enter number of processes: ");
scanf("%d",&np);
printf("Enter number of resources: ");
scanf("%d",&nr);
printf("Enter availability of instances of each resource: ");
for(i=0;i<nr;i++){
scanf("%d",&available[i]);
}
safe[np];
printf("Enter maximum number of instances of each resource required by each process");
printf("\nEnter max matrix :\n");
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter maximum number of instances of each resource requestes by each process");
printf("\nEnter allocation matrix :\n");
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&allocation[i][j]);
}
}
//compute need matrix
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
//display all matrices-allocation need and max
printf("Max matrix:\n");
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
printf("%d ",max[i][j]);
}
printf("\n");
}
printf("Allocation matrix:\n");
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
printf("%d ",allocation[i][j]);
}
printf("\n");
}
printf("Need matrix:\n");
for ( i = 0; i < np; i++)
{
for(j=0;j<nr;j++)
{
printf("%d ",need[i][j]);
}
printf("\n");
}
for ( i = 0; i < np; i++)
{
finish[i]=0;
}
for (int x = 0; x < np; x++)
{
/* code */
for ( i = 0; i < np; i++)
{
for (j = 0; j < nr; j++)
{
if(finish[i]==0)
{
if (need[i][j]<=available[j])
{
flag=0;
}
else{
flag=1;
break;
}
}
else
{
flag=1;
}
}
if(flag==0)
{
finish[i]=1;
safe[k++]=i;
for(j=0;j<nr;j++){
available[j]+=allocation[i][j];
}
}
}
}
if(k==np){
printf("Safe Sequence is:\n");
for ( i = 0; i < np-1; i++)
{
printf("P%d -> ",safe[i]);
}
printf("P%d\n",safe[i]);
}
else
{
printf("Not in safe state");
}
}