forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bankers_Algo.java
87 lines (74 loc) · 2.24 KB
/
Bankers_Algo.java
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
package com.company.os;
class Bankers_Algo
{
// Number of processes
static int P = 5;
// Number of resources
static int R = 3;
// Function to find the need of each process
static void calculateNeed(int[][] need, int[][] maxm,
int[][] allot)
{
// Calculating Need of each P
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)
need[i][j] = maxm[i][j] - allot[i][j];
}
// Function to find the system is in safe state or not
static void isSafe(int[] avail, int[][] maxm, int[][] allot)
{
int [][]need = new int[P][R];
calculateNeed(need, maxm, allot);
boolean []finish = new boolean[P];
int []safeSeq = new int[P];
int []work = new int[R];
System.arraycopy(avail, 0, work, 0, R);
int count = 0;
while (count < P)
{
boolean found = false;
for (int p = 0; p < P; p++)
{
if (!finish[p])
{
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;
if (j == R)
{
for (int k = 0 ; k < R ; k++)
work[k] += allot[p][k];
safeSeq[count++] = p;
finish[p] = true;
found = true;
}
}
}
if (!found)
{
System.out.print("System is not in safe state");
return;
}
}
System.out.println("System is in safe state.");
for (int i = 0; i < P ; i++)
System.out.print("P"+safeSeq[i] + " -> ");
}
// Driver code
public static void main(String[] args)
{
int[] avail = {3, 3, 2};
int[][] maxm = {{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};
int[][] allot = {{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
isSafe(avail, maxm, allot);
}
}