-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection_system.c
More file actions
108 lines (97 loc) · 2.87 KB
/
Copy pathdetection_system.c
File metadata and controls
108 lines (97 loc) · 2.87 KB
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
#include<stdio.h>
struct file {
int all[10];
int max[10];
int need[10];
int flag;
};
void main() {
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt = 0, id, newr;
int avail[10], seq[10];
printf("Enter number of processes -- ");
scanf("%d", &n);
printf("Enter number of resources -- ");
scanf("%d", &r);
for(i = 0; i < n; i++) {
printf("Enter details for P%d", i);
printf("\nEnter allocation\t -- \t");
for(j = 0; j < r; j++)
scanf("%d", &f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j = 0; j < r; j++)
scanf("%d", &f[i].max[j]);
f[i].flag = 0;
}
printf("\nEnter Available Resources\t -- \t");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d", &id);
printf("Enter Request for Resources \t -- \t");
for(i = 0; i < r; i++) {
scanf("%d", &newr);
f[id].all[i] += newr;
avail[i] = avail[i] - newr;
}
for(i = 0; i < n; i++) {
for(j = 0; j < r; j++) {
f[i].need[j] = f[i].max[j] - f[i].all[j];
if(f[i].need[j] < 0)
f[i].need[j] = 0;
}
}
cnt = 0;
fl = 0;
while(cnt != n) {
g = 0;
for(j = 0; j < n; j++) {
if(f[j].flag == 0) {
b = 0;
for(p = 0; p < r; p++) {
if(avail[p] >= f[j].need[p])
b++;
}
if(b == r) {
printf("\nP%d is visited", j);
seq[fl++] = j;
f[j].flag = 1;
for(k = 0; k < r; k++)
avail[k] = avail[k] + f[j].all[k];
cnt++;
printf("(");
for(k = 0; k < r; k++)
printf("%3d", avail[k]);
printf(")");
g = 1;
}
}
}
if(g == 0) {
printf("\nREQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\nSYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i = 0; i < fl; i++)
printf("P%d ", seq[i]);
printf(")");
y:
printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i = 0; i < n; i++) {
printf("P%d\t", i);
for(j = 0; j < r; j++)
printf("%6d", f[i].all[j]);
printf("\t");
for(j = 0; j < r; j++)
printf("%6d", f[i].max[j]);
printf("\t");
for(j = 0; j < r; j++)
printf("%6d", f[i].need[j]);
printf("\n");
}
}