-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJF.java
More file actions
93 lines (80 loc) · 2 KB
/
SJF.java
File metadata and controls
93 lines (80 loc) · 2 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
/** -----------------------------------------------------------------------
SJF.java
@author William Clift
Operating Systems
Ursinus College
Project 2 - Scheduling Schemes
14 April 2020
Compile Instructions:
Compile: javac SJF.java
------------------------------------------------------------------- **/
import java.util.*;
public class SJF extends Scheme{
public String scheme = "SJF";
public String fileName;
public CircularLL toSchedule;
public CircularLL processed;
public CircularLL incoming;
/**
* Shortest Job First Algorithm
*
*/
public SJF(CircularLL incoming, String scheme){
super(incoming, scheme);
this.toSchedule = new CircularLL();
this.processed = new CircularLL();
this.incoming = incoming;
checkArrival();
}
/**
* Runs the Algorithm
*
*/
public void run(){
System.out.println("============================================================");
while(incoming.getSize()>0 && toSchedule.getSize()>0){
if(incoming.getSize() > 0){
checkArrival();
}
if(toSchedule.getSize()>0){ // If there are processes left
PCB current = getSmallest();
PCB result = cpuProcess(current, current.burst_time, toSchedule, incoming); // Run the next Process in line
processed.push(result);
}else{
cpuTick();
}
}
printEndMetrics(processed);
}
/**
* @return smallest - the smallest burst time in the list
*/
private PCB getSmallest(){
PCB smallest = toSchedule.work();
PCB current;
for(int i = 0; i< toSchedule.getSize(); i++){
current = toSchedule.work();
if(smallest.burst_time > current.burst_time){
smallest = current;
}
}
toSchedule.complete(smallest.pid);
return smallest;
}
/**
* Check if any processes have arrived.
*
*/
private void checkArrival(){
PCB current = incoming.head;
for(int i = 0; i < incoming.getSize(); i++){
current = incoming.head;
if(current.arrival_time == cpuTime){
PCB in = incoming.pop();
toSchedule.push(in);
}else{
incoming.work();
}
}
}
}