/
Untitled1.cpp
149 lines (130 loc) · 4.09 KB
/
Untitled1.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
int allocatted[5][5], max_needs[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maximum_res[5], running[5], safe = 0;
int count = 0, i, j, exec, resource, process, k = 1;
int main()
{
printf("\nEnter number of process: ");
scanf("%d", &process);
for (i = 0; i < process; i++)
{
running[i] = 1;
count++;
}
printf("\nEnter number of resource: ");
scanf("%d", &resource);
printf("\nEnter Claim Vector:");
for (i = 0; i < resource; i++)
{
scanf("%d", &maximum_res[i]);
}
printf("\nEnter allocatted Resource Table:\n");
for (i = 0; i < process; i++)
{
for(j = 0; j < resource; j++)
{
scanf("%d", &allocatted[i][j]);
}
}
printf("\nEnter Maximum Claim Table:\n");
for (i = 0; i < process; i++)
{
for(j = 0; j < resource; j++)
{
scanf("%d", &max_needs[i][j]);
}
}
printf("\nThe Claim Vector is: ");
for (i = 0; i < resource; i++)
{
printf("\t%d", maximum_res[i]);
}
printf("\nThe allocatted Resource Table:\n");
for (i = 0; i < process; i++)
{
for (j = 0; j < resource; j++)
{
printf("\t%d", allocatted[i][j]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table:\n");
for (i = 0; i < process; i++)
{
for (j = 0; j < resource; j++)
{
printf("\t%d", max_needs[i][j]);
}
printf("\n");
}
for (i = 0; i < process; i++)
{
for (j = 0; j < resource; j++)
{
allocation[j] += allocatted[i][j];
}
}
printf("\nallocatted resource:");
for (i = 0; i < resource; i++)
{
printf("\t%d", allocation[i]);
}
for (i = 0; i < resource; i++)
{
available[i] = maximum_res[i] - allocation[i];
}
printf("\nAvailable resource:");
for (i = 0; i < resource; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (count != 0)
{
safe = 0;
for (i = 0; i < process; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resource; j++)
{
if (max_needs[i][j] - allocatted[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
count--;
safe = 1;
for (j = 0; j < resource; j++)
{
available[j] += allocatted[i][j];
}
break;
}
}
}
if (!safe)
{
printf("\nThe process are in unsafe state.\n");
break;
}
else
{
printf("\nThe process is in safe state");
printf("\nAvailable vector:");
for (i = 0; i < resource; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
}
}
return 0;
}