-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.java
359 lines (180 loc) · 6.07 KB
/
Algorithms.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package MiniProject;
import java.util.*;
import java.util.List;
import java.util.Queue;
class Process {
int BurstTime;// burst time of the process
String name;// process name
int ArrivalTime;// arrival time
int ct;// completion time
int wt;// waiting time
int priority;
Process(int bt, String name, int pri, int at) {
//this.at=at;let's try to work with the clock
BurstTime = bt;
this.name = name;
priority = pri;
ArrivalTime = at;
//this.ct=ct;
//this.wt=wt;
}
}
public class Algorithms {
void bubbleSort(List<Process> processes, int num) {
for (int i = 0; i < num - 1; i++) {
for (int j = 0; j < num - i - 1; j++) {
if (processes.get(j).priority > processes.get(j + 1).priority) {
// Swap the processes based on priority
Process temp = processes.get(j);
processes.set(j, processes.get(j + 1));
processes.set(j + 1, temp);
}
}
}
}
void nonPreemptivePriorityScheduling(ArrayList<Process> arr) {
// Create arrays to store burst time and priority for each process
int num = arr.size();
int[] burstTime = new int[num];
int[] priority = new int[num];
// Populate burstTime and priority arrays from the Process objects
for (int i = 0; i < num; i++) {
burstTime[i] = arr.get(i).BurstTime;
priority[i] = arr.get(i).priority;
}
// Use bubble sort to sort the priority values and keep corresponding burst times in sync
for (int i = 0; i < num - 1; i++) {
for (int j = 0; j < num - i - 1; j++) {
if (priority[j] > priority[j + 1]) {
// Swap priority values
int tempPriority = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = tempPriority;
// Swap burst times accordingly
int tempBurstTime = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = tempBurstTime;
// Swap corresponding Process objects in the 'arr' list
Process tempProcess = arr.get(j);
arr.set(j, arr.get(j + 1));
arr.set(j + 1, tempProcess);
}
}
}
// Initialize variables for waiting times and total waiting time
int waitTime = 0;
int[] waitTimes = new int[num];
System.out.println("Process\tBurst Time\tWaiting Time");
float totalWaitTime = 0;
for (int i = 0; i < num; i++) {
System.out.println(arr.get(i).name + "\t\t" + burstTime[i] + "\t\t" + waitTime);
waitTimes[i] = waitTime;
totalWaitTime += waitTime;
waitTime += burstTime[i];
}
}
public void FCFS(Queue<Process> readyQueue) {
int currentTime = 0;
int totalWait = 0;
int num = 0;
while (!readyQueue.isEmpty()) {
Process currentProcess = readyQueue.remove();
System.out.print(currentProcess.name + " ");
num++;
// Calculate waiting time for the current process
int waitingTime = Math.max(0, (currentTime - currentProcess.ArrivalTime)) ;
totalWait += waitingTime;
for (int t = 0; t < currentProcess.BurstTime; t++) {
System.out.print("-");
currentTime++;
}
System.out.println();
}
}
void SJF(ArrayList<Process> input, int num) {
int clock = 0;// set clock to 0
boolean CPU_Run = false;
int exec = 0;// the current executing process burst time
int prDone = 0;// num of proc. done
int min = Integer.MAX_VALUE; // to fine min burst time
int minIdx = 0;// indx of min burst time
// start clock
ArrayList<Process> ready = new ArrayList<>();
while (true) {
if (prDone == num) {
break;
}
// add processes which arrive at current clock time in the ready queue
for (int i = 0; i < num; i++) {
if (clock == input.get(i).ArrivalTime) {
ready.add(input.get(i));
}
}
if (!CPU_Run) {
// find minimum burst time of all processes in the queue
for (int i = 0; i < ready.size(); i++) {
if (ready.get(i).ArrivalTime < min) {
min = ready.get(i).BurstTime;
minIdx = i;
}
}
// remove process
if (!ready.isEmpty()) {
exec = ready.get(minIdx).BurstTime;
System.out.println("Process " + ready.get(minIdx).name);
ready.remove(minIdx);
CPU_Run = true;
min = Integer.MAX_VALUE;
}
} else {
if (exec == 0) {
System.out.println();
prDone++;
CPU_Run = false;
} else {
exec--;
System.out.print("-");
}
}
clock++;
}
}
void RoundRobin(Queue<Process> q)
{
Queue<Process> rq=new LinkedList<>();//executing queue
int ft=4;//fixed time or time quantum
if(!q.isEmpty()) {
rq.add(q.remove());
}
int clk=0;
Process running;
while(!rq.isEmpty())
{
running=rq.peek();
System.out.print("\nState of process "+running.name+":");
for(int i=0;i<ft;i++)
{
System.out.print("* ");
clk=clk+10;
running.BurstTime--;
if(running.BurstTime==0)
{
running.ct=clk-running.ArrivalTime;
//running.ct=MiniProject.trial.Main
running.wt=running.ct-running.BurstTime;
System.out.print("\nProcess "+running.name+":Execution complete "+"\n**********************************************************");
rq.remove();
break;
}
}
if(!q.isEmpty()&&q.peek().ArrivalTime<clk)
{
rq.add(q.remove());
}
if(running.BurstTime==0)
continue;
rq.remove();
rq.add(running);
}
}
}