-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJFScheduler.java
More file actions
59 lines (53 loc) · 1.9 KB
/
SJFScheduler.java
File metadata and controls
59 lines (53 loc) · 1.9 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
import java.util.ArrayList;
import java.util.Collections;
public class SJFScheduler {
public void Schedule(ArrayList<Process> pList) throws CloneNotSupportedException {
Collections.sort(pList, (p1, p2) -> p1.arrivalTime - p2.arrivalTime);
ArrayList<Process> readyList=new ArrayList<Process>();
ArrayList<Process> pList2=new ArrayList<Process>();// Update waiting times
int totalWaitingTime=0;
for(Process p : pList) {
pList2.add(p.clone());
}
int time=0;
while(!pList.isEmpty() || !readyList.isEmpty()) {
time++;
for( int i=0; i<pList.size(); i++ ) {
if(pList.get(i).arrivalTime==time) { // coming process are added to
readyList.add(pList.get(i).clone()); //readyList.
pList.remove(pList.get(i)); //
i--;
}
}
if(!readyList.isEmpty()) {
Collections.sort(readyList, (p1, p2) -> p1.getBurstTime() - p2.getBurstTime());
readyList.get(0).setBurstTime((readyList.get(0).getBurstTime())-1);
if(readyList.get(0).getBurstTime()==0) { // if process is terminated
readyList.remove(0);
for(int i=0; i<readyList.size(); i++) {
for(Process j:pList2) {
if(j.pName.equals(readyList.get(i).pName)) {
j.setWaitingTime((j.getWaitingTime()) + 1);
totalWaitingTime++;
}
}
}
}else {
for(int i=1; i<readyList.size(); i++) { //if process is not terminated
for(Process j:pList2) {
if(j.pName.equals(readyList.get(i).pName)) {
j.setWaitingTime((j.getWaitingTime()) + 1);
totalWaitingTime++;
}
}
}
}
}
}
System.out.println("ProcessName ArrivalTime BurstTime WaitingTime");
for(Process p : pList2)
System.out.println(p);
System.out.println("Total waiting time: "+totalWaitingTime);
System.out.println("Average waiting time: "+(double)totalWaitingTime/(double)pList2.size());
}
}