-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFQ2.java
317 lines (268 loc) · 11.2 KB
/
MFQ2.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import java.util.*;
class Process
{
//Process arrival time , CPU burst time and priority
int atime, cbt, priority;
// Taking input for the variables
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Process Priority: ");
priority=sc.nextInt();
System.out.println("Enter Process Arrival Time: ");
atime=sc.nextInt();
System.out.println("Enter Process CPU Burst Time: ");
cbt=sc.nextInt();
}
public static void main(String[] args)
{
//Declaring the queues and the quantum times
int qt1=2,qt2=4,qt3=6,qt4=8;
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
Queue<Integer> q3 = new LinkedList<>();
Queue<Integer> q4 = new LinkedList<>();
//Making queue for the first Round Robin
Queue<Integer> rrqueue1= new LinkedList<>();
//Entering the processes
System.out.println("Enter the number of process:- ");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Process p[]= new Process[n];
for(int i=0;i<n;i++)
{
p[i]=new Process();
p[i].input();
}
// Making Arraylist for all the process parameters
ArrayList<Integer> arrival=new ArrayList<>();
for(int i=0;i<n;i++)
arrival.add(p[i].atime);
ArrayList<Integer> burst=new ArrayList<>();
for(int i=0;i<n;i++)
burst.add(p[i].cbt);
ArrayList<Integer> prio=new ArrayList<>();
for(int i=0;i<n;i++)
prio.add(p[i].priority);
//Temporary Arraylist
ArrayList<Integer> temp2=new ArrayList<>();
//Declaring thread
Thread thread=new Thread();
// Storing the sum of all the CPU burst time and the total time needed for execution in sum
int sum=0;
for(int i=0;i<burst.size();i++)
{
sum+=burst.get(i);
sum+=arrival.get(i);
}
sum =sum + Collections.min(burst);
try
{
int leq2,leq3,leq4;
boolean istwenty=false;
for(int time=0;time<=sum;)
{
//Storing in Round Robin Queue for all the Process which arrives first
for(int i=0;i<arrival.size();i++)
{
if (arrival.get(i) <= time && arrival.get(i)>=0)
{
temp2.add(arrival.get(i));
}
}
int counter=temp2.size();
for(int i=0;i<counter;i++)
{
int min =Collections.min(temp2);
temp2.remove(temp2.indexOf(min));
rrqueue1.add(burst.get(arrival.indexOf(min)));
arrival.set(arrival.indexOf(min),-1);
}
if(rrqueue1.isEmpty()==false)
{
int len=rrqueue1.size();
for(int i=0;i<len;i++)
((LinkedList<Integer>) q1).add(rrqueue1.remove());
}
//For Queue 1
//Following Round Robin
if(q1.isEmpty()==false)
{
System.out.println("Processing In Queue 1");
System.out.println("Executing Process P" + prio.get(burst.indexOf(q1.peek())));
if(q1.peek()<qt1)
System.out.println("Wait for "+ q1.peek() + " Seconds...");
else
System.out.println("Wait for 2 Seconds...");
int i = 1;
for (i = 1; i <= qt1; i++)
{
//Process Executing
thread.sleep(1000);
//Incrementing the time
time++;
//Checking if the time is a multiple of 20
if(time % 20 == 0 && time !=0)
istwenty=true;
//reducing the burst time by 1
int temp3;
temp3 = ((LinkedList<Integer>) q1).removeFirst() - 1;
burst.set(burst.indexOf(temp3+1),temp3);
((LinkedList<Integer>) q1).addFirst(temp3);
//checking if the time becomes 0
if (temp3 == 0)
{
if(q1.isEmpty()==false)
q1.remove();
break;
}
}
if (i == qt1+1) {
int temp=q1.remove();
q2.add(temp);
}
}
//For Queue 2
//Following Round Robin
else if(q2.isEmpty()==false)
{
System.out.println("Processing In Queue 2");
System.out.println("Executing Process P" + prio.get(burst.indexOf(q2.peek())));
if(q2.peek()<qt2)
System.out.println("Wait for "+ q2.peek() + " Seconds...");
else
System.out.println("Wait for 4 Seconds...");
int i = 1;
for (i = 1; i <= qt2; i++)
{
//Process Executing
thread.sleep(1000);
//Incrementing the time
time++;
//Checking if the time is a multiple of 20
if(time % 20 == 0 && time !=0)
istwenty=true;
//reducing the burst time by 1
int temp3;
temp3 = ((LinkedList<Integer>) q2).removeFirst() - 1;
burst.set(burst.indexOf(temp3+1),temp3 );
((LinkedList<Integer>) q2).addFirst(temp3);
//checking if the time becomes 0
if (temp3==0)
{
if(q2.isEmpty()==false)
q2.remove();
break;
}
}
if (i == qt2+1) {
int temp = q2.remove();
q3.add(temp);
}
leq2=time;
}
//For Queue 3
//Following Round Robin
else if(q3.isEmpty()==false)
{
System.out.println("Processing In Queue 3");
System.out.println("Executing Process P" + prio.get(burst.indexOf(q3.peek())));
if(q3.peek()<qt3)
System.out.println("Wait for "+ q3.peek() + " Seconds...");
else
System.out.println("Wait for 6 Seconds...");
int i = 1;
for (i = 1; i <= qt3; i++)
{
//Process Executing
thread.sleep(1000);
//Incrementing the time
time++;
//Checking if the time is a multiple of 20
if(time % 20 == 0 && time !=0)
istwenty=true;
//reducing the burst time by 1
int temp3;
temp3 = ((LinkedList<Integer>) q3).removeFirst() - 1;
burst.set(burst.indexOf(temp3+1),temp3);
((LinkedList<Integer>) q3).addFirst(temp3);
//checking if the time becomes 0
if (temp3==0)
{
if(q3.isEmpty()==false)
q3.remove();
break;
}
}
if (i == qt3+1) {
int temp = q3.remove();
q4.add(temp);
}
}
//For Queue 4
//Following First Come First Serve
else if(q4.isEmpty()==false)
{
System.out.println("Processing In Queue 4");
System.out.println("Executing Process P" + prio.get(burst.indexOf(q4.peek())));
if(q4.peek()<qt4)
System.out.println("Wait for "+ q4.peek() + " Seconds...");
else
System.out.println("Wait for 8 Seconds...");
int i = 1;
for (i = 1; i <= qt4; i++)
{
//Process Executing
thread.sleep(1000);
//Incrementing the time
time++;
//Checking if the time is a multiple of 20
if(time % 20 == 0 && time !=0)
istwenty=true;
//reducing the burst time by 1
int temp3;
temp3 = ((LinkedList<Integer>) q4).removeFirst() - 1;
burst.set(burst.indexOf(temp3+1),temp3 );
((LinkedList<Integer>) q4).addFirst(temp3);
//checking if the time becomes 0
if (temp3==0)
{
if(q4.isEmpty()==false)
q4.remove();
break;
}
}
}
else
{
//if all the queues are vacant the time increeases by 1
time++;
}
//Solution for starvation
if(time % 20 == 0 && time !=0)
istwenty=true;
if(istwenty)
{
//Transferring all the processes of Queue 2 to Queue 1
while (q2.size() > 0) {
q1.add(q2.remove());
}
//Transferring all the processes of Queue 3 to Queue 1
while (q3.size() > 0) {
q1.add(q3.remove());
}
//Transferring all the processes of Queue 3 to Queue 1
while (q4.size() > 0) {
q1.add(q4.remove());
}
istwenty=false;
}
}
}
//Catching Exception used for any exception for sleep function
catch (Exception e)
{
System.out.println("There was an Exception");
}
}
}