-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobin.java
87 lines (76 loc) · 2.25 KB
/
RoundRobin.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.HashMap;
import java.util.LinkedList;
public class RoundRobin implements CPUAlgos {
private List<Process> list;
private List<String> gantt;
private Map<Integer, ArrayList<Process>> map;
private Queue<Process> queue;
private int tq, min;
private double avgTat = 0;
private double avgWt = 0;
public RoundRobin(List<Process> list) {
this.list = list;
tq = Process.TQ;
gantt = new ArrayList<>();
map = new HashMap<>();
queue = new LinkedList<>();
}
@Override
public void schedule() {
sort(list);
generateListByAT(list);
// Logic of RoundRobin
ArrayList<Process> temp = map.get(min);
int exit = 0;
for (Process p : temp)
queue.add(p);
try {
Process p;
while (!queue.isEmpty()) {
p = queue.remove();
}
} catch (Exception e) {
e.printStackTrace();
}
String str = "";
for (String s : gantt)
str += s;
Process.AvgTat = avgTat / list.size();
Process.AvgWt = avgWt / list.size();
Process.Gantt = str;
}
private void generateListByAT(List<Process> list) {
int i;
Process p = list.get(0);
map.put(p.at, new ArrayList<Process>());
map.get(p.at).add(p);
min = p.at;
for (i = 1; i < list.size(); i++) {
p = list.get(i);
min = min < p.at ? min : p.at;
if (p.at != list.get(i - 1).at) {
map.put(p.at, new ArrayList<Process>());
}
map.get(p.at).add(p);
}
/* Printing the Map */
// for (int key : map.keySet()) {
// System.out.println("\n" + key + " : ");
// map.get(key).forEach(System.out::println);
// }
}
private void sort(List<Process> list) {
Comparator<Process> com = new Comparator<>() {
public int compare(Process a, Process b) {
return a.at - b.at;
}
};
Collections.sort(list, com);
}
}