-
Notifications
You must be signed in to change notification settings - Fork 368
/
peterson_algo.java
66 lines (52 loc) · 1.81 KB
/
peterson_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
class peterson_algo {
// Shared variables
volatile boolean[] flag = new boolean[2];
volatile int turn;
// Process 0
class Process0 extends Thread {
public void run() {
while (true) {
flag[0] = true;
turn = 1;
// Wait until Process 1 is not in its critical section or it's not its turn
while (flag[1] && turn == 1) {
// Busy wait
}
// Critical section
System.out.println("Process 0 is in its critical section.");
// Exit critical section
flag[0] = false;
// Remainder section
System.out.println("Process 0 is in its remainder section.");
}
}
}
// Process 1
class Process1 extends Thread {
public void run() {
while (true) {
flag[1] = true;
turn = 0;
// Wait until Process 0 is not in its critical section or it's not its turn
while (flag[0] && turn == 0) {
// Busy wait
}
// Critical section
System.out.println("Process 1 is in its critical section.");
// Exit critical section
flag[1] = false;
// Remainder section
System.out.println("Process 1 is in its remainder section.");
}
}
}
public static void main(String[] args) {
PetersonSolution solution = new PetersonSolution();
// Create two threads for the two processes
Process0 process0 = solution.new Process0();
Process1 process1 = solution.new Process1();
// Start the threads
process0.start();
process1.start();
}
}