-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.java
232 lines (215 loc) · 8.62 KB
/
Simulator.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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Simulator{
private double lambda1;
private double lambda2;
private List<Process> processes;
private int timeNow;
public Simulator(double lambda1, double lambda2){
this.lambda1 = lambda1;
this.lambda2 = lambda2;
this.processes = new LinkedList<>();
timeNow=0;
}
public List<Process> getProcesses(){
return this.processes;
}
public Process createProcess(int pid){
timeNow+=(int)(-Math.floor(Math.log(1-Math.random())/lambda1));
return new Process(pid, timeNow, (int)(-Math.floor(Math.log(1-Math.random())/lambda2)) );
}
public void addProcess(Process p){
processes.add(p);
Collections.sort(processes, Process.startTimeComparator());
}
public void showProcesses(){
System.out.println("PID\tEntryTime\tTimeToCompletion");
for(Process p: processes) System.out.println(p.getPid()+"\t"+p.getStartTime()+"\t\t"+p.getRunTime());
}
public void createProcesses(int n){
clearProcesses();
for(int i=1;i<=n;i++) this.processes.add(createProcess(i));
}
public void clearProcesses(){
processes.clear();
}
public Map<Integer, List<CPU>> scheduleFCFS(){
Map<Integer, List<CPU>> schedule = new HashMap<>();
int time=0;
for(Process p:processes){
time=Math.max(time, p.getStartTime());
if(schedule.get(p.getPid())==null) schedule.put(p.getPid(), new LinkedList<CPU>());
schedule.get(p.getPid()).add(new CPU(time,time+=p.getRunTime()));
}
return schedule;
}
public Map<Integer, List<CPU>> scheduleSJF(){
Map<Integer, List<CPU>> schedule = new HashMap<>();
PriorityQueue<Process> heap = new PriorityQueue<>();
int i=0, time=0;
while(i<processes.size() || heap.size()>0){
if(heap.size()==0) time = Math.max(time, processes.get(i).getStartTime());
for(;i<processes.size() && processes.get(i).getStartTime()<=time;i++) heap.add(processes.get(i));
Process p = heap.poll();
time = Math.max(time, p.getStartTime());
if(schedule.get(p.getPid())==null) schedule.put(p.getPid(), new LinkedList<CPU>());
schedule.get(p.getPid()).add(new CPU(time, time+=p.getRunTime()));
}
return schedule;
}
public Map<Integer, List<CPU>> scheduleRR(int slice){
Map<Integer, List<CPU>> schedule = new HashMap<>();
Queue<Process> queue = new LinkedList<>();
int i=0, time = 0;
while(queue.size()>0 || i<processes.size()){
if(queue.size()==0) time = Math.max(time, processes.get(i).getStartTime());
for(;i<processes.size() && processes.get(i).getStartTime()<=time+slice;i++) queue.add(processes.get(i));
Process p = queue.poll();
time = Math.max(time, p.getStartTime());
if(schedule.get(p.getPid())==null) schedule.put(p.getPid(), new LinkedList<CPU>());
schedule.get(p.getPid()).add(new CPU(time, time+=Math.min(slice, p.getRunTime())));
if(p.getRunTime()>slice) queue.add(new Process(p.getPid(), p.getStartTime(), p.getRunTime()-slice));
}
return schedule;
}
public Map<Integer, List<CPU>> scheduleSTC(){
Map<Integer, List<CPU>> schedule = new HashMap<>();
PriorityQueue<Process> heap = new PriorityQueue<>();
int i=0, time=0;
while(i<processes.size() || heap.size()>0){
if(heap.size()==0) time = Math.max(time, processes.get(i).getStartTime());
for(;i<processes.size() && processes.get(i).getStartTime()<=time;i++) heap.add(processes.get(i));
Process p = heap.poll();
for(;i<processes.size() && processes.get(i).getStartTime()<time+p.getRunTime() && processes.get(i).getRunTime()+processes.get(i).getStartTime()>=time+p.getRunTime(); i++)
heap.add(processes.get(i));
if(schedule.get(p.getPid())==null) schedule.put(p.getPid(), new LinkedList<CPU>());
if(i<processes.size() && processes.get(i).getStartTime()<time+p.getRunTime()){
Process temp = processes.get(i++);
schedule.get(p.getPid()).add(new CPU(time, temp.getStartTime()));
heap.add(temp);
heap.add(new Process(p.getPid(), temp.getStartTime(), time+p.getRunTime()-temp.getStartTime()));
time = temp.getStartTime();
}
else{
time = Math.max(time, p.getStartTime());
schedule.get(p.getPid()).add(new CPU(time, time+=p.getRunTime()));
}
}
return schedule;
}
public Map<Integer, List<CPU>> scheduleMLFQ(int slice1, int slice2, int slice3, int boost){
Map<Integer, List<CPU>> schedule = new HashMap<>();
Map<Integer, Integer> sl2 = new HashMap<>();
Map<Integer, Integer> sl3 = new HashMap<>();
List<Process> q1=new LinkedList<>(), q2=new LinkedList<>(), q3=new LinkedList<>();
int i=0, time = 0;
while(i<processes.size() || q1.size()>0 || q2.size()>0 || q3.size()>0){
if(q1.size()==0 && q2.size()==0 && q3.size()==0){
time = Math.max(time, processes.get(i).getStartTime());
q1.add(processes.get(i++));
}
if(time>=boost){
q1.addAll(q2); q2.clear();
q1.addAll(q3); q3.clear();
boost+=boost;
}
while(q1.size()>0){
Process p = q1.remove(0);
time = Math.max(time, p.getStartTime());
for(;i<processes.size() && processes.get(i).getStartTime()<=time+slice1;i++) q1.add(processes.get(i));
if(schedule.get(p.getPid())==null) schedule.put(p.getPid(), new LinkedList<CPU>());
if(time+Math.min(slice1, p.getRunTime())>boost){
schedule.get(p.getPid()).add(new CPU(time, boost));
q1.add(0, new Process(p.getPid(), boost, time+p.getRunTime()-boost));
q1.addAll(q2); q2.clear();
q1.addAll(q3); q3.clear();
time=boost; boost+=boost;
}
else{
schedule.get(p.getPid()).add(new CPU(time, time+=Math.min(slice1, p.getRunTime())));
if(p.getRunTime()>slice1) q2.add(new Process(p.getPid(), time, p.getRunTime()-slice1));
}
}
while(q2.size()>0 && q1.size()==0){
Process p = q2.remove(0);
if(!sl2.containsKey(p.getPid())) sl2.put(p.getPid(), slice2);
time = Math.max(time, p.getStartTime());
if(i<processes.size() && processes.get(i).getStartTime()<time+Math.min(p.getRunTime(), sl2.get(p.getPid())))
q1.add(processes.get(i++));
if(q1.size()==0){
if(time+Math.min(sl2.get(p.getPid()), p.getRunTime())>boost){
schedule.get(p.getPid()).add(new CPU(time, boost));
q2.add(0, new Process(p.getPid(), boost, time+p.getRunTime()-boost));
q1.addAll(q2); q2.clear();
q1.addAll(q3); q3.clear();
sl2.clear();
time=boost; boost+=boost;
}
else{
schedule.get(p.getPid()).add(new CPU(time, time+=Math.min(sl2.get(p.getPid()), p.getRunTime())));
if(p.getRunTime()>sl2.get(p.getPid())) q3.add(new Process(p.getPid(), time, p.getRunTime()-sl2.get(p.getPid())));
sl2.remove(p.getPid());
}
}
else{
Process t = q1.get(0);
if(boost<t.getStartTime()){
schedule.get(p.getPid()).add(new CPU(time, boost));
q2.add(0, new Process(p.getPid(), boost, time+p.getRunTime()-boost));
q2.addAll(q3); q3.clear();
q2.addAll(q1); q1.clear();
q1.addAll(q2); q2.clear();
sl2.clear();
time=boost; boost+=boost;
}
else{
schedule.get(p.getPid()).add(new CPU(time, t.getStartTime()));
q2.add(0, new Process(p.getPid(), t.getStartTime(), time+p.getRunTime()-t.getStartTime()));
sl2.put(p.getPid(), sl2.get(p.getPid())-t.getStartTime()+time);
}
}
}
while(q3.size()>0 && q1.size()==0 && q2.size()==0){
Process p = q3.remove(0);
if(!sl3.containsKey(p.getPid())) sl3.put(p.getPid(), slice3);
time = Math.max(time, p.getStartTime());
if(i<processes.size() && processes.get(i).getStartTime()<=time+Math.min(sl3.get(p.getPid()), p.getRunTime()))
q1.add(processes.get(i++));
if(q1.size()==0){
if(time+Math.min(sl3.get(p.getPid()), p.getRunTime())>boost){
schedule.get(p.getPid()).add(new CPU(time, boost));
q3.add(0, new Process(p.getPid(), boost, time+p.getRunTime()-boost));
q1.addAll(q2); q2.clear();
q1.addAll(q3); q3.clear();
sl3.clear();
time=boost; boost+=boost;
}
else{
schedule.get(p.getPid()).add(new CPU(time, time+=Math.min(sl3.get(p.getPid()), p.getRunTime())));
if(p.getRunTime()>sl3.get(p.getPid())) q3.add(new Process(p.getPid(), time, p.getRunTime()-sl3.get(p.getPid())));
sl3.remove(p.getPid());
}
}
else{
Process t = q1.get(0);
if(boost<t.getStartTime()){
schedule.get(p.getPid()).add(new CPU(time, boost));
q3.add(0, new Process(p.getPid(), boost, time+p.getRunTime()-boost));
q2.addAll(q3); q3.clear();
q2.addAll(q1); q1.clear();
q1.addAll(q2); q2.clear();
sl3.clear();
time=boost; boost+=boost;
}
else{
schedule.get(p.getPid()).add(new CPU(time, t.getStartTime()));
q3.add(0, new Process(p.getPid(), t.getStartTime(), time+p.getRunTime()-t.getStartTime()));
sl3.put(p.getPid(), sl3.get(p.getPid())+time-t.getStartTime());
}
}
}
}
return schedule;
}
}