File tree 1 file changed +78
-0
lines changed
1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <iostream>
2
+ using namespace std;
3
+
4
+ int main()
5
+ {
6
+
7
+ int n, m, i, j, k;
8
+ n = 5; // Number of processes
9
+ m = 3; // Number of resources
10
+ int alloc[5][3] = { { 0, 1, 0 },
11
+ { 2, 0, 0 },
12
+ { 3, 0, 2 },
13
+ { 2, 1, 1 },
14
+ { 0, 0, 2 } };
15
+
16
+ int max[5][3] = { { 7, 5, 3 },
17
+ { 3, 2, 2 },
18
+ { 9, 0, 2 },
19
+ { 2, 2, 2 },
20
+ { 4, 3, 3 } };
21
+
22
+ int avail[3] = { 3, 3, 2 };
23
+
24
+ int f[n], ans[n], ind = 0;
25
+ for (k = 0; k < n; k++) {
26
+ f[k] = 0;
27
+ }
28
+ int need[n][m];
29
+ for (i = 0; i < n; i++) {
30
+ for (j = 0; j < m; j++)
31
+ need[i][j] = max[i][j] - alloc[i][j];
32
+ }
33
+ int y = 0;
34
+ for (k = 0; k < 5; k++) {
35
+ for (i = 0; i < n; i++) {
36
+ if (f[i] == 0) {
37
+
38
+ int flag = 0;
39
+ for (j = 0; j < m; j++) {
40
+ if (need[i][j] > avail[j]){
41
+ flag = 1;
42
+ break;
43
+ }
44
+ }
45
+
46
+ if (flag == 0) {
47
+ ans[ind++] = i;
48
+ for (y = 0; y < m; y++)
49
+ avail[y] += alloc[i][y];
50
+ f[i] = 1;
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ int flag = 1;
57
+
58
+ // To check if sequence is safe or not
59
+ for(int i = 0;i<n;i++)
60
+ {
61
+ if(f[i]==0)
62
+ {
63
+ flag = 0;
64
+ cout << "The given sequence is not safe";
65
+ break;
66
+ }
67
+ }
68
+
69
+ if(flag==1)
70
+ {
71
+ cout << "Following is the SAFE Sequence" << endl;
72
+ for (i = 0; i < n - 1; i++)
73
+ cout << " P" << ans[i] << " ->";
74
+ cout << " P" << ans[n - 1] <<endl;
75
+ }
76
+
77
+ return (0);
78
+ }
You can’t perform that action at this time.
0 commit comments