Skip to content

Commit 97ded95

Browse files
committed
Improved Banker Algorithm
1 parent 2e6bfd7 commit 97ded95

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Deadlock Algorithms/Banker.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool isSystemSafe()
5454
hasChanged = false;
5555
// Loop Through Processes
5656
for (p = 0; p < PN; p++)
57-
// If The Procces Not Executed Before
57+
// If The Proccess Not Executed Before
5858
if (finished[p] == 0)
5959
{
6060
// Check: Is This Process Need Resources More Than The Available?
@@ -90,16 +90,9 @@ bool isSystemSafe()
9090

9191
bool allocateIfSafe(int processNo, int request[RN])
9292
{
93-
// Temporary Copies Of: The Process Original Values From All Tables
94-
int tempNeed[RN], tempAlloc[RN], tempAvail[RN];
93+
// Change Tables Values
9594
for (r = 0; r < RN; r++)
9695
{
97-
// Store The Original Values
98-
tempAlloc[r] = allocation[processNo][r];
99-
tempNeed[r] = need[processNo][r];
100-
tempAvail[r] = available[r];
101-
102-
// Change Tables Values
10396
allocation[processNo][r] += request[r];
10497
need[processNo][r] -= request[r];
10598
available[r] -= request[r];
@@ -108,13 +101,14 @@ bool allocateIfSafe(int processNo, int request[RN])
108101
if (isSystemSafe())
109102
return true;
110103

111-
// Restore The Original Values
104+
// Restore Tables Values
112105
for (r = 0; r < RN; r++)
113106
{
114-
allocation[processNo][r] = tempAlloc[r];
115-
need[processNo][r] = tempNeed[r];
116-
available[r] = tempAvail[r];
107+
allocation[processNo][r] -= request[r];
108+
need[processNo][r] += request[r];
109+
available[r] += request[r];
117110
}
111+
118112
return false;
119113
}
120114

0 commit comments

Comments
 (0)