-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-17
More file actions
130 lines (113 loc) · 3.37 KB
/
problem-17
File metadata and controls
130 lines (113 loc) · 3.37 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int available[MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
bool isSafeState(int processes, int resources) {
bool finish[MAX_PROCESSES] = {0};
int work[MAX_RESOURCES];
for (int i = 0; i < resources; i++) {
work[i] = available[i];
}
while (true) {
bool found = false;
for (int p = 0; p < processes; p++) {
if (!finish[p]) {
bool canAllocate = true;
for (int r = 0; r < resources; r++) {
if (need[p][r] > work[r]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int r = 0; r < resources; r++) {
work[r] += allocation[p][r];
}
finish[p] = true;
found = true;
}
}
}
if (!found) {
break;
}
}
for (int p = 0; p < processes; p++) {
if (!finish[p]) {
return false;
}
}
return true;
}
void requestResources(int p, int resources) {
printf("Enter the request for process %d:\n", p);
int request[MAX_RESOURCES];
for (int i = 0; i < resources; i++) {
scanf("%d", &request[i]);
}
bool validRequest = true;
for (int i = 0; i < resources; i++) {
if (request[i] > need[p][i]) {
validRequest = false;
break;
}
}
if (validRequest) {
for (int i = 0; i < resources; i++) {
if (request[i] > available[i]) {
validRequest = false;
break;
}
}
}
if (validRequest) {
for (int i = 0; i < resources; i++) {
available[i] -= request[i];
allocation[p][i] += request[i];
need[p][i] -= request[i];
}
if (isSafeState(MAX_PROCESSES, resources)) {
printf("Request can be granted.\n");
} else {
for (int i = 0; i < resources; i++) {
available[i] += request[i];
allocation[p][i] -= request[i];
need[p][i] += request[i];
}
printf("Request cannot be granted as it leads to unsafe state.\n");
}
} else {
printf("Request cannot be granted.\n");
}
}
int main() {
int processes = MAX_PROCESSES;
int resources = MAX_RESOURCES;
printf("Enter the available resources:\n");
for (int i = 0; i < resources; i++) {
scanf("%d", &available[i]);
}
printf("Enter the maximum resource matrix:\n");
for (int p = 0; p < processes; p++) {
for (int r = 0; r < resources; r++) {
scanf("%d", &max[p][r]);
need[p][r] = max[p][r];
}
}
printf("Enter the allocation matrix:\n");
for (int p = 0; p < processes; p++) {
for (int r = 0; r < resources; r++) {
scanf("%d", &allocation[p][r]);
need[p][r] -= allocation[p][r];
}
}
int processNumber;
printf("Enter the process number to make a request: ");
scanf("%d", &processNumber);
requestResources(processNumber, resources);
return 0;
}