Skip to content

Commit 1befda8

Browse files
Create 8.c
1 parent dca7d71 commit 1befda8

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

8.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int main()
4+
{
5+
int Max[10][10],need[10][10],alloc[10][10],avail[10],completed[10],safeSequence[10];
6+
int p,r,i,j,process,count;
7+
count = 0;
8+
printf("Enter the no of processes : ");
9+
scanf("%d",&p);
10+
for(i=0;i<p;i++)
11+
completed[i] = 0;
12+
printf("\n\nEnter the no of resources : ");
13+
scanf("%d",&r);
14+
printf("\n\nEnter the Max Matrix for each process : ");
15+
for(i=0;i<p;i++)
16+
{
17+
printf("\nFor process %d : ", i + 1);
18+
for(j = 0; j < r; j++)
19+
scanf("%d", &Max[i][j]);
20+
}
21+
printf("\n\nEnter the allocation for each process : ");
22+
for(i = 0; i < p; i++)
23+
{
24+
printf("For process %d : ",i + 1);
25+
for(j = 0; j < r; j++)
26+
scanf("%d", &alloc[i][j]);
27+
}
28+
printf("\n\nEnter the Available Resources : ");
29+
for(i = 0; i < r; i++)
30+
scanf("%d", &avail[i]);
31+
for(i = 0; i < p; i++)
32+
{
33+
for(j = 0; j < r; j++)
34+
need[i][j] = Max[i][j] - alloc[i][j];
35+
}
36+
37+
do
38+
{
39+
printf("\n Max matrix:\tAllocation matrix:\n");
40+
for(i = 0; i < p; i++)
41+
{
42+
for( j = 0; j < r; j++)
43+
printf("%d ", Max[i][j]);
44+
printf("\t\t");
45+
for( j = 0; j < r; j++)
46+
printf("%d ", alloc[i][j]);
47+
printf("\n");
48+
}
49+
process = -1;
50+
for(i = 0; i < p; i++)
51+
{
52+
if(completed[i] == 0)
53+
{
54+
process = i ;
55+
for(j = 0; j < r; j++)
56+
{
57+
if(avail[j] < need[i][j])
58+
{
59+
process = -1;
60+
break;
61+
}
62+
}
63+
}
64+
if(process != -1)
65+
break;
66+
}
67+
if(process != -1)
68+
{
69+
printf("\nProcess %d runs to completion!", process + 1);
70+
safeSequence[count] = process + 1;
71+
count++;
72+
for(j = 0; j < r; j++)
73+
{
74+
avail[j] += alloc[process][j];
75+
alloc[process][j] = 0;
76+
Max[process][j] = 0;
77+
completed[process] = 1;
78+
}
79+
}
80+
}
81+
while(count != p && process != -1);
82+
if(count == p)
83+
{
84+
printf("\nThe system is in a safe state!!\n");
85+
printf("Safe Sequence : < ");
86+
for( i = 0; i < p; i++)
87+
printf("%d ", safeSequence[i]);
88+
printf(">\n");
89+
}
90+
else
91+
printf("\nThe system is in an unsafe state!!");
92+
}

0 commit comments

Comments
 (0)