-
Notifications
You must be signed in to change notification settings - Fork 3
/
DeadLockDetection.java
69 lines (67 loc) · 2.09 KB
/
DeadLockDetection.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
import java.util.*;
public class DeadLockDetection{
static int[] processID = {0,1,2,3,4};
static int[][] alloc = {{0,1,1},{2,0,0},{0,1,1},{0,0,0},{0,0,1}};
static int[][] requ = {{2,1,0},{2,2,1},{0,2,2},{1,2,1},{1,0,0}};
static int[] available = {1,1,0};
// do not modify further
static boolean[] finish;
static boolean deadLock = false;
public static void main(String[] args){
initFinish();
while(!deadLock){
deadLock = true;
if(nextExist()){
deadLock = false;
}
}
boolean isDeadLock = false;
for(boolean each : finish){
if(!each){
isDeadLock = true;
}
}
if(isDeadLock){
System.out.print("the system is deadlocked, process involved in deadlock : ");
for(int i = 0; i < finish.length; i++){
if(!finish[i]){
System.out.print("P"+processID[i]+", ");
}
}
}
else{
System.out.print("No DeadLock");
}
System.out.println("\n Final finish array : "+Arrays.toString(finish));
}
public static boolean nextExist(){
for(int i = 0; i < requ.length; i++){
boolean isSmall = true;
for(int j = 0 ; j < available.length; j++){
if(requ[i][j] > available[j]){
isSmall = false;
}
}
if(isSmall && !(finish[i])){
finish[i] = true;
for(int j = 0; j < available.length; j++){
available[j] += alloc[i][j];
}
return true;
}
}
return false;
}
public static void initFinish(){
finish = new boolean[alloc.length];
for(int i = 0; i < alloc.length; i++){
boolean assign = true;
for(int j = 0; j < alloc[0].length; j++){
if(alloc[i][j] != 0){
assign = false;
}
}
finish[i] = assign;
}
}
}