-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitForGraphClass.java
135 lines (100 loc) · 4.05 KB
/
WaitForGraphClass.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
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
131
132
133
134
135
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class WaitForGraphClass {
public String WaitForGraph(String time) throws IOException
{
char locktype = ' ', lockItem =' ';
int count = 0, i = 0 ,t=0,k = 0,m=0,g=0,d=0,a=-1,w=-1,TransNum = 0;
Item I = new Item();
String wait="";
Transaction T = new Transaction();
Transaction [] translist = new Transaction [100];
Item[] itemlist = new Item[100];
Item[] waitlist = new Item[100];
int[][] cycle=new int[2][100];
int[]det= new int[100];
BufferedReader br;
br = new BufferedReader(new FileReader(new File("/Users/robaahmad/Desktop/Transactions.txt")));
String line;
while ((line=br.readLine()) != null &&( count+1)<=Integer.parseInt(time)) {
count++;
if ("begin".equals(line.substring(0, 5))) {
T.setTransNum(Integer.parseInt(""+line.charAt(6)));
T.setTransTime(count);
translist[t]=new Transaction(T.getTransNum(),T.getTransTime());
t++;
}
else if (("lock".equals(line.substring(0, 4)))){
locktype = line.charAt(5);
lockItem = line.charAt(7);
TransNum = Integer.parseInt(""+line.charAt(10));
for(int l=0;l<t ;l++){
if (translist[l].getTransNum() == TransNum) {
T = new Transaction(TransNum, translist[l].getTransTime());
itemlist[k] = new Item(locktype,lockItem,T);
k++;
}}
while (i < k ){
if (itemlist[i].gettype() == locktype && itemlist[i].getItem() == lockItem&&TransNum!=itemlist[i].getT().getTransNum())
{
if ((locktype == 'X') || ((locktype == 'S' && itemlist[i].gettype() == 'X') && (itemlist[i].getItem() == lockItem))) {
waitlist[m++] = new Item(locktype, lockItem, T);
cycle[0][g]=itemlist[i].getT().getTransNum();
cycle[1][g++]=TransNum;
}
}
++i;
}
i=0;
}
else if (("commit".equals(line.substring(0, 6)))||"abort".equals(line.substring(0, 5))) {
for (int j = 0; j <m; j++) {
if (waitlist[j].getT().getTransNum() == (line.charAt(7))) {
waitlist[j] = null;
}
}
}
if(count==Integer.parseInt(time))
{
for(int l=0;l<g;l++)
{
for(int y=0;y<g;y++)
{
for(int u=0;u<g;u++){
if( cycle[0][y]== cycle[1][u]&&cycle[0][y]!=w)
{
w=cycle[0][y];
a=y;
break;
}
}
if(a!=-1)
break;
}
if(a!=-1)
{
for(int s=0;s<g;s++){
if(cycle[1][a]==cycle[0][s]){
if(cycle[1][a]==w)
{
return "There is a deadlock at this time";
}
if(a!=g-1)
{
a=s;
s=-1;
}
}
}
a=-1;
}
}
return "There is no a deadlock at this time";
}
}
return "Enter a valid time";
}
}