-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.java
More file actions
330 lines (316 loc) · 15.8 KB
/
Scheduler.java
File metadata and controls
330 lines (316 loc) · 15.8 KB
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
import java.time.Clock;
import java.util.*;
public class Scheduler {
// PidMap adds process to hashMap to be used for finding PIDs quickly from userland.
HashMap<Integer, PCB> PidMap = new HashMap<Integer, PCB>();
// WaitMap adds processes to Hashmap to be used for adding and removing processes from queues.
HashMap<Integer, PCB> WaitMap = new HashMap<Integer, PCB>();
// sleepingProcesses stores all the processes which are in sleep mode.
private LinkedList<PCB> sleepingProcesses;
// realTimeProcesses, interactiveProcesses, and backGroundProcesses contains each process corresponding to their priority.
private LinkedList<PCB> realTimeProcesses;
private LinkedList<PCB> interactiveProcesses;
private LinkedList<PCB> backGroundProcesses;
// Timer used to regulate the processes being currently run.
private Timer timer;
// Clock used to store processes which will be used towake processes.
private Clock clock = Clock.systemUTC();
// currentProcess is the current process that the kernel will start.
public PCB currentProcess;
// constructor schedules the currentProcess to the stopped at a fixed rate. This is to
// regulate the rate at which the scheduler regulates the tasks. allocated to it.
public Scheduler(){
//System.out.println("Scheduler is being created...");
// processes declared as a new LinkedList.
sleepingProcesses = new LinkedList<>();
realTimeProcesses = new LinkedList<>();
interactiveProcesses = new LinkedList<>();
backGroundProcesses = new LinkedList<>();
// timer is declared as well as currentProcess.
timer = new Timer();
currentProcess = null;
// timertask is given to timer in order to requestStop on
// currentProcess every time it isn't null.
timer.schedule(new TimerTask() {
@Override
public void run() {
if (currentProcess != null) {
currentProcess.getProcess().requestStop();
}
}
}, 0, 250);
}
// CreateProcess takes a userland process, adds it to the list of corresponding processes of the same priority, then
// calls SwitchProcess if the currentProcess is null. It will then return the PID of the
// process being run. Adds the process to a PID map.
public int CreateProcess(PCB p){
//System.out.println(p.getProcess().getClass() + " process is being created in the scheduler...");
if(p.getPriority() == OS.Priority.REAL_TIME)
realTimeProcesses.add(p);
else if(p.getPriority() == OS.Priority.INTERACTIVE)
interactiveProcesses.add(p);
else
backGroundProcesses.add(p);
if(currentProcess == null){
SwitchProcess();
}
//System.out.println("CreateProcess (Scheduler) Complete! Process #" + p.getPid() + " of " + p.getProcess().getClass() + " added to scheduler.");
PidMap.put(p.getPid(), p);
return p.getPid();
}
// SwitchProcess checks to see if the currentProcess isn't null & isn't finished processing. If so then demote the current process,
// demotes the currentProcess if it's been demoted at least 5 times, and then adds the process to its corresponding priority list.
// Finally, it checks to see if the sleepingProcess is empty. If it's not empty then awake any sleeping processes. After that,
// change the current process.
public void SwitchProcess(){
//System.out.println("SwitchProcess being called in scheduler.");
//printAllLists();
if(currentProcess != null){
if(!currentProcess.isDone()){
currentProcess.demote();
checkDemote();
}
if(currentProcess.getPriority() == OS.Priority.REAL_TIME)
realTimeProcesses.add(currentProcess);
else if(currentProcess.getPriority() == OS.Priority.INTERACTIVE)
interactiveProcesses.add(currentProcess);
else
backGroundProcesses.add(currentProcess);
}
if(!sleepingProcesses.isEmpty())
awakeSleepingProcesses();
ProcessChange();
//System.out.println("SwitchProcess complete!");
//printAllLists();
}
// ExitProcess calls ProcessChange, and doesn't queue the current Process. Removes process from the PID map.
public void ExitProcess(){
PidMap.remove(currentProcess.getPid());
//System.out.println("ExitProcess (scheduler) called!");
if(!realTimeProcesses.isEmpty() || !interactiveProcesses.isEmpty() || !backGroundProcesses.isEmpty())
ProcessChange();
//System.out.println("ExitProcess (scheduler) complete!");
}
// Sleep take miliseconds from kernel, gets current milliseconds and adds milliseconds to it and sets the currentProcess
// to be in sleepingProcess until after the time allocated. Sleep then calls ProcessChange()
public void Sleep(int milliseconds){
//System.out.println("Sleep method (scheduler) called!");
long currentMs = clock.millis();
long addedMs = currentMs + milliseconds;
sleepingProcesses.add(currentProcess);
currentProcess.setSleepTime(addedMs);
currentProcess.setOrigTime(currentMs);
//System.out.println("Added " + milliseconds + " to " + currentMs + " in " + " Current process#" + currentProcess.getPid() + " class: " + currentProcess.getProcess().getClass() + "(" + addedMs + ")");
ProcessChange();
//System.out.println("Sleep (Scheduler) complete!");
}
// ProcessChange, depending on the quantity of the lists, makes a problabistic model which generates a number between 1 and 10 repeatidly until
// currentProcess has been changed due to one of the lists popping out a process to run.
public void ProcessChange(){
//System.out.println("ProcessChange called!");
//printAllLists();
Random rand = new Random();
currentProcess = null;
if(!realTimeProcesses.isEmpty() || interactiveProcesses.isEmpty()) {
while(true) {
int num = rand.nextInt(10) + 1;
if (num < 7 && !realTimeProcesses.isEmpty()){
currentProcess = realTimeProcesses.pop();
break;
}
else if (num > 7 && num < 10 && !interactiveProcesses.isEmpty()){
currentProcess = interactiveProcesses.pop();
break;
}
else if (!backGroundProcesses.isEmpty()){
currentProcess = backGroundProcesses.pop();
break;
}
num = rand.nextInt(10) + 1;
//System.out.println(num);
}
}
else{
while(true){
double num = rand.nextDouble(10) + 1;
if(num < 7.5 && !interactiveProcesses.isEmpty()){
currentProcess = interactiveProcesses.pop();
break;
}
else if(num > 7.5 && !backGroundProcesses.isEmpty()){
currentProcess = backGroundProcesses.pop();
break;
}
num = rand.nextDouble(10) + 1;
//System.out.println(num);
}
}
if(currentProcess.isWaitingForMessage()){
OS.returnValue = currentProcess.popMessageList();
currentProcess.setWaitingForMessage(false);
}
//System.out.println(currentProcess.toString());
//System.out.println(currentProcess.getPid() + ", " +currentProcess.getName());
//System.out.println("ProcessChange complete!");
//printAllLists();
}
// Check demote sees if the currentProcess is able to be demoted, will demote it and will put it down a priority in the queue.
public void checkDemote(){
//System.out.println("checkDemote called!");
//System.out.println("Current Processs demote value: " + currentProcess.getDemote());
if(currentProcess.getDemote() >= 5){
OS.Priority p = currentProcess.getPriority();
OS.Priority p2;
if(currentProcess.getPriority() == OS.Priority.REAL_TIME){
currentProcess.setPriority(OS.Priority.INTERACTIVE);
p2 = OS.Priority.INTERACTIVE;
}
else if(currentProcess.getPriority() == OS.Priority.INTERACTIVE){
currentProcess.setPriority(OS.Priority.BACKGROUND);
p2 = OS.Priority.BACKGROUND;
}
else
p2 = OS.Priority.BACKGROUND;
//System.out.println("Process #" + currentProcess.getPid() + " demoted from " + p + " to " + p2 + ".");
currentProcess.resetDemote();
}
//System.out.println("checkDemote complete!");
}
// awakeSleepingProcesses gets current milliseconds, parses through sleepingProcesses and checks to see if currentMillis is more than the sleeping process.
// It will then add the process to its corresponding queue if the test passes and removed the process from the sleeping queue.
public void awakeSleepingProcesses(){
//System.out.println("awakeSleepingProcesses called!");
//printAllLists();
for(PCB p : sleepingProcesses){
long currentMillis = clock.millis();
if(currentMillis > p.getSleepTime()){
if(p.getPriority() == OS.Priority.REAL_TIME)
realTimeProcesses.add(p);
else if(p.getPriority() == OS.Priority.INTERACTIVE)
interactiveProcesses.add(p);
else
backGroundProcesses.add(p);
sleepingProcesses.remove(p);
//System.out.println("Process #" + p.getPid() + ", class " + p.getProcess().getClass() + " of has been awoken after " + (currentMillis - p.getOrigTime()) +" milliseconds!");
}
}
//System.out.println("awakeSleepingProcesses complete!");
//printAllLists();
}
// returns the current process’ pid
public int GetPid(){
return currentProcess.getPid();
}
// returns the pid of a process with that name.
public int GetPidByName(String s){
for (int entry : PidMap.keySet()){
PCB e = PidMap.get(entry);
if(e.getName().equals(s))
return e.getPid();
}
return -1;
}
/*
* Adds km to the sender PID, then adds km to the receiver's PID. It then checks to see if the target's in the waitMap and adds it to its
* corresponding priority queue. It then removes the process of the target from the waitlist.
* */
public void SendMessage(KernelMessage km){
// populate sender's message queue
PCB sender = PidMap.get(km.getSenderPid());
sender.appendToMessageList(km);
KernelMessage km2 = new KernelMessage(km);
// populate target's kernelMessage
PCB target = PidMap.get(km2.getTargetPid());
target.appendToMessageList(km2);
// if in target list, restore it to the queue?
if(WaitMap.containsKey(target.getPid())){
if(target.getPriority() == OS.Priority.REAL_TIME)
realTimeProcesses.add(target);
else if(target.getPriority() == OS.Priority.INTERACTIVE)
interactiveProcesses.add(target);
else
backGroundProcesses.add(target);
WaitMap.remove(target.getPid());
}
}
// Waits for message and returns the popped process from the currentProcess. If not it will put the currentProcess in the WaitMap and return null.
public KernelMessage WaitForMessage(){
if(!currentProcess.isNotOccupied())
return currentProcess.popMessageList();
WaitMap.put(currentProcess.getPid(), currentProcess);
currentProcess.setWaitingForMessage(true);
ProcessChange();
return null;
}
// Method to check the mininum about of bytes needed for currentProcess to run properly.
private int checkRequiredInts(){
VirtualToPhysicalMapping[] mappings = currentProcess.getVirtualToPhysicalMappings();
for(int i = 0; i < mappings.length; i++){
if(mappings[i] == null)
return i;
}
return mappings.length;
}
// goes through list, finds a random process suitable for getRandomProcess to return.
private PCB getSuitableProcess(LinkedList<PCB> list){
// Iterate through list
int i = 0;
int MIN = checkRequiredInts()-1;
LinkedList<PCB> finList = new LinkedList<PCB>();
while(i < list.size()){
// retrieve mappings, check while null if it can hold all of currentProcess' data.
VirtualToPhysicalMapping[] mappings = list.get(i).getVirtualToPhysicalMappings();
int n = 0;
while((mappings[n]) != null && (n < mappings.length-1))
n++;
// if the minimum is less than n, add i to finList.
if(MIN <= n)
finList.add(list.get(i));
i++;
}
// get the random pages with mappings, return a random pcb of this pool.
if(finList.size() > 0){
Random rand = new Random();
int j = rand.nextInt(finList.size());
return finList.get(j);
}
else
return null;
}
// getRandomProcess gets a random process from a random list which has a suitable PCB for a swap.
public PCB getRandomProcess(){
Random rand = new Random();
PCB finPCB = null;
while(true){
int randNum = rand.nextInt(2);
if(randNum == 0 && !backGroundProcesses.isEmpty())
finPCB = getSuitableProcess(backGroundProcesses);
else if(randNum == 1 && !interactiveProcesses.isEmpty())
finPCB = getSuitableProcess(interactiveProcesses);
else if(randNum == 2 && !realTimeProcesses.isEmpty())
finPCB = getSuitableProcess(backGroundProcesses);
if(finPCB != null)
return finPCB;
}
}
// PrintAllLists made to debug the lists and currentProcess in the scheduler.
public void printAllLists(){
System.out.println("---------------------------------------------------------------");
System.out.println("Processes data:");
if(currentProcess != null)
System.out.println("Current Process: #" + currentProcess.getPid() + ", class: " + currentProcess.getProcess().getClass()+", priority: " + currentProcess.getPriority());
System.out.println("Real time Processes:");
for(PCB p : realTimeProcesses)
System.out.println("Process: #" + p.getPid() + ", class: " + p.getProcess().getClass()+", priority: " + p.getPriority());
System.out.println("Interactive processes:");
for(PCB p : interactiveProcesses)
System.out.println("Process: #" + p.getPid() + ", class: " + p.getProcess().getClass()+", priority: " + p.getPriority());
System.out.println("Background processes:");
for(PCB p : backGroundProcesses)
System.out.println("Process: #" + p.getPid() + ", class: " + p.getProcess().getClass()+", priority: " + p.getPriority());
System.out.println("Sleeping processes:");
for(PCB p : sleepingProcesses)
System.out.println("Process: #" + p.getPid() + ", class: " + p.getProcess().getClass()+", priority: " + p.getPriority());
System.out.println("---------------------------------------------------------------");
}
}