-
Notifications
You must be signed in to change notification settings - Fork 0
/
Peterson.java
139 lines (109 loc) · 4.79 KB
/
Peterson.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
136
137
138
139
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
class Producer extends Thread{
Producer(String name){
this.setName(name);
}
public void run(){
for(int rounds=0;rounds<20;rounds++){
Peterson.enter_region(0);//add process to critical region
int bufferdatacount=Peterson.bufferdatacount;
int buffersize=Peterson.buffer.length;
if(bufferdatacount!=buffersize){
Random rand = new Random();
Peterson.buffer[bufferdatacount]= rand.nextInt(50) + 1;
Peterson.bufferdatacount+=1;
}
else{
System.out.println("No free space in the buffer");
}
for(int i=0;i<Peterson.buffer.length;i++){
System.out.print(Peterson.buffer[i]+" ");
}
System.out.println();
Random rand = new Random();//get random integer to make thread sleep
int sleep=(rand.nextInt(4) + 1)*1000;
System.out.println(this.getName()+" "+"will sleep for "+sleep/1000+" seconds");
Peterson.leave_region(0);//leave process from the critical region
System.out.println();
try {
Thread.sleep(sleep);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("\n\n"+this.getName()+" "+" is Terminated..!\n");
}
}
class Consumer extends Thread{
Consumer(String name){
this.setName(name);
}
public void run(){
for(int rounds=0;rounds<20;rounds++){
Peterson.enter_region(1);//add process to critical reigion
int bufferdatacount=Peterson.bufferdatacount;
if(bufferdatacount!=0){
System.out.println(Peterson.buffer[bufferdatacount-1]);
Peterson.buffer[bufferdatacount-1]=0;
Peterson.bufferdatacount-=1;
}
else{
System.out.println("No data to fetch");
}
for(int i=0;i<Peterson.buffer.length;i++){
System.out.print(Peterson.buffer[i]+" ");
}
System.out.println();
Random rand = new Random();//get random integer to make thread sleep
int sleep=(rand.nextInt(4) + 1)*1000;
System.out.println(this.getName()+" "+"will sleep for "+sleep/1000+" seconds");
Peterson.leave_region(1);//leave process from the critical region
System.out.println();
try {
Thread.sleep(sleep);
} catch (InterruptedException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("\n\n"+this.getName()+" "+" is Terminated..!\n");
}
}
public class Peterson {
static int pcount=2;//No of process, In this code processes are equal to threads
static int turn;
static boolean interested[]=new boolean[pcount]; //boolean process array. control the critical reagion state by changing values
static int buffer[]=new int[6]; //buffer to illustrate produser, consumer problem
static int bufferdatacount=0; // filled data count in the buffer
static Thread arr[]=new Thread[2];// process array
public static void enter_region(int process){
int other=1-process;
interested[process]=true;//set process to ready state(add to critical region)
turn=process;
System.out.println(arr[process].getName()+" "+" is Waiting ");
//keep the process in waiting mod till below condition get false
while(turn==process && interested[other]==true){}
//start run the process after the waiting mod
System.out.println(arr[process].getName()+" "+" is Executing ");
}
public static void leave_region(int process){
System.out.println(arr[process].getName()+" "+" is leaved from the critical region ");
interested[process]=false;//remove process from the critical region
}
public static void main(String[] args) {
//initialze the boolean array with value false
for(int i=0;i<pcount;i++){
interested[i]=false;
}
//initialize the buffer with 0 values
for(int i=0;i<6;i++){
buffer[i]=0;
}
//make processes which call producer and consumer
arr[0]=new Producer("Producer");
arr[1]=new Consumer("Consumer");
arr[0].start();
arr[1].start();
}
}