-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanker.cpp
More file actions
106 lines (86 loc) · 3.09 KB
/
banker.cpp
File metadata and controls
106 lines (86 loc) · 3.09 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
#include <iostream>
#include <fstream>
using namespace std;
const int P = 5; // Number of processes
const int R = 3; // Number of resources
// Function to find the need matrix
void calculate(int need[P][R], int max[P][R], int allot[P][R]) {
// Calculate the need matrix based on max and allotted resources
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
need[i][j] = max[i][j] - allot[i][j];
}
// Function to check if the system is in a safe state
bool isSafe(int processes[], int avail[], int max[][R], int allot[][R]) {
int need[P][R];
calculate(need, max, allot); // Calculate the need matrix
bool finish[P] = { 0 }; // Mark all processes as unfinished
int safeSeq[P]; // Safe sequence array
int work[R];
// Initialize the work array with available resources
for (int i = 0; i < R; i++)
work[i] = avail[i];
int count = 0;
// Continue until all processes are finished
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p]) { // If process p is not finished
int j;
// Check if process p's needs can be met with current work resources
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;
// If all needs of process p can be met
if (j == R) {
// Allocate resources to process p and add to work
for (int k = 0; k < R; k++)
work[k] += allot[p][k];
// Add process p to safe sequence and mark it as finished
safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}
// If no unfinished process could be found whose needs can be met
if (!found) {
cout << "System is not in safe state" << endl;
return false;
}
}
// Print the safe sequence
cout << "System is in safe state.\nSafe sequence is: ";
for (int i = 0; i < P; i++)
cout << safeSeq[i] << " ";
return true;
}
int main() {
ifstream inputFile("input.txt");
if (!inputFile) {
cout << "Error opening file." << endl;
return 1;
}
int processes[P]; // Array to hold process identifiers
int avail[R]; // Array to hold available resources
int max[P][R]; // Max matrix
int allot[P][R]; // Allocation matrix
// Read process identifiers from file
for (int i = 0; i < P; i++)
inputFile >> processes[i];
// Read available resources from file
for (int i = 0; i < R; i++)
inputFile >> avail[i];
// Read max matrix from file
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
inputFile >> max[i][j];
// Read allocation matrix from file
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
inputFile >> allot[i][j];
inputFile.close();
// Check if the system is in a safe state
isSafe(processes, avail, max, allot);
return 0;
}