-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.java
More file actions
139 lines (124 loc) · 2.76 KB
/
Copy pathProcess.java
File metadata and controls
139 lines (124 loc) · 2.76 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
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
136
137
138
139
package scheduler2PL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Process implements Comparable<Process>
{
int id;
public static Set<Process> liste = new HashSet<>();
private Status status = Status.OPEN;
Anweisung lock;
public Process(int id)
{
this.id = id;
}
@Override
public boolean equals(Object arg)
{
if (arg instanceof Process)
{
return id == ((Process) arg).id;
}
else
{
return ((Integer) id).equals(arg);
}
}
@Override
public int compareTo(Process o)
{
return id - o.id;
}
@Override
public int hashCode()
{
return id;
}
@Override
public String toString()
{
return String.valueOf(id);
}
public static Process getAProcess(int id)
{
liste.add(new Process(id));
Iterator<Process> it = liste.iterator();
while (it.hasNext())
{
Process curent = it.next();
if (curent.equals(id))
{
return curent;
}
}
return null;
}
public static int getLength()
{
return liste.size();
}
public boolean work(Anweisung anweisung, boolean debugPrint)
{
if (status == Status.DONE)
{
return false;
}
if (status == Status.LOCKED && lock != anweisung)
{
return false;
}
switch (anweisung.getAnweisungsTyp())
{
case C:
Variable.removeLocks(this);
done(anweisung, debugPrint);
status = Status.DONE;
return true;
case R:
if (!anweisung.getVariable().lock.ReadLock(this))
{
lock(anweisung);
return false;
}
else
{
done(anweisung, debugPrint);
return true;
}
case W:
if (!anweisung.getVariable().lock.WriteLock(this))
{
lock(anweisung);
return false;
}
else
{
done(anweisung, debugPrint);
return true;
}
case E:
break;
default:
break;
}
return true;
}
private void done(Anweisung anweisung, boolean debugPrint)
{
status = Status.OPEN;
anweisung.setDone();
if (debugPrint)
{
System.out.println(anweisung);
}
}
private void lock(Anweisung anweisung)
{
status = Status.LOCKED;
lock = anweisung;
}
public Status getStatus()
{
return status;
}
}