forked from mtuts/process-scheduling-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFS.java
46 lines (39 loc) · 1.11 KB
/
FCFS.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
import java.util.Arrays;
import java.util.TreeSet;
public class FCFS extends Algorithm {
public FCFS(Process[] processes) {
super(processes);
}
@Override
protected void sort() {
sorted_processes = new TreeSet<>(new ArriveTimeComparator());
sorted_processes.addAll(Arrays.asList(processes));
}
@Override
protected void apply() {
GrantChart grantChart = new GrantChart();
time = 0;
total_waiting_time = 0;
average_waiting_time = 0;
sort();
for (Process process : sorted_processes) {
grantChart.schedule(process);
grantChart.passTime(process.getBurstTime());
}
grantChart.calculateWaitingTime();
grantChart.print();
average_waiting_time = grantChart.getTotalWaitingTime() / processes.length;
}
@Override
public void printResult() {
if (sorted_processes == null) {
apply();
}
System.out.println();
System.out.println(Process.getHeader());
for (Process process : sorted_processes) {
System.out.println(process);
}
System.out.printf("%s Average waiting time: %.2f\n", getName(), average_waiting_time);
}
}