JavaAlgorithms/Others/SJF.java

185 lines
5.1 KiB
Java
Raw Normal View History

package Others;
2018-10-27 15:29:00 +08:00
/**
2020-10-24 18:23:28 +08:00
*
*
* <h2>Shortest job first.</h2>
2020-10-24 18:23:28 +08:00
*
* <p>Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting
* process with the smallest execution time to execute next Shortest Job first has the advantage of
* having minimum average waiting time among all scheduling algorithms. It is a Greedy Algorithm. It
* may cause starvation if shorter processes keep coming. This problem has been solved using the
* concept of aging.
*
* @author shivg7706
* @since 2018/10/27
*/
2020-10-24 18:23:28 +08:00
import java.util.*;
2018-10-26 20:02:49 +08:00
import java.util.ArrayList;
import java.util.Comparator;
2020-10-24 18:23:28 +08:00
import java.util.Scanner;
2018-10-26 20:02:49 +08:00
class Process {
2020-10-24 18:23:28 +08:00
public int pid;
public int arrivalTime;
public int burstTime;
public int priority;
public int turnAroundTime;
public int waitTime;
public int remainingTime;
2018-10-26 20:02:49 +08:00
}
class Schedule {
2020-10-24 18:23:28 +08:00
private int noOfProcess;
private int timer = 0;
private ArrayList<Process> processes;
private ArrayList<Process> remainingProcess;
private ArrayList<Integer> gantChart;
private float burstAll;
private Map<Integer, ArrayList<Process>> arrivals;
Schedule() {
Scanner in = new Scanner(System.in);
processes = new ArrayList<Process>();
remainingProcess = new ArrayList<Process>();
gantChart = new ArrayList<>();
arrivals = new HashMap<>();
System.out.print("Enter the no. of processes: ");
noOfProcess = in.nextInt();
System.out.println("Enter the arrival, burst and priority of processes");
for (int i = 0; i < noOfProcess; i++) {
Process p = new Process();
p.pid = i;
p.arrivalTime = in.nextInt();
p.burstTime = in.nextInt();
p.priority = in.nextInt();
p.turnAroundTime = 0;
p.waitTime = 0;
p.remainingTime = p.burstTime;
if (arrivals.get(p.arrivalTime) == null) {
arrivals.put(p.arrivalTime, new ArrayList<Process>());
}
arrivals.get(p.arrivalTime).add(p);
processes.add(p);
burstAll += p.burstTime;
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
in.close();
}
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
void startScheduling() {
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
processes.sort(
new Comparator<Process>() {
@Override
public int compare(Process a, Process b) {
return a.arrivalTime - b.arrivalTime;
}
});
while (!(arrivals.size() == 0 && remainingProcess.size() == 0)) {
removeFinishedProcess();
if (arrivals.get(timer) != null) {
remainingProcess.addAll(arrivals.get(timer));
arrivals.remove(timer);
}
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
remainingProcess.sort(
new Comparator<Process>() {
private int alpha = 6;
private int beta = 1;
2018-10-26 20:02:49 +08:00
@Override
public int compare(Process a, Process b) {
2020-10-24 18:23:28 +08:00
int aRem = a.remainingTime;
int bRem = b.remainingTime;
int aprior = a.priority;
int bprior = b.priority;
return (alpha * aRem + beta * aprior) - (alpha * bRem + beta * bprior);
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
});
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
int k = timeElapsed(timer);
ageing(k);
timer++;
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
System.out.println("Total time required: " + (timer - 1));
}
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
void removeFinishedProcess() {
ArrayList<Integer> completed = new ArrayList<Integer>();
for (int i = 0; i < remainingProcess.size(); i++) {
if (remainingProcess.get(i).remainingTime == 0) {
completed.add(i);
}
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
for (int i = 0; i < completed.size(); i++) {
int pid = remainingProcess.get(completed.get(i)).pid;
processes.get(pid).waitTime = remainingProcess.get(completed.get(i)).waitTime;
remainingProcess.remove(remainingProcess.get(completed.get(i)));
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
}
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
public int timeElapsed(int i) {
if (!remainingProcess.isEmpty()) {
gantChart.add(i, remainingProcess.get(0).pid);
remainingProcess.get(0).remainingTime--;
return 1;
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
return 0;
}
public void ageing(int k) {
for (int i = k; i < remainingProcess.size(); i++) {
remainingProcess.get(i).waitTime++;
if (remainingProcess.get(i).waitTime % 7 == 0) {
remainingProcess.get(i).priority--;
}
}
}
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
public void solve() {
System.out.println("Gant chart ");
for (int i = 0; i < gantChart.size(); i++) {
System.out.print(gantChart.get(i) + " ");
}
System.out.println();
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
float waitTimeTot = 0;
float tatTime = 0;
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
for (int i = 0; i < noOfProcess; i++) {
processes.get(i).turnAroundTime = processes.get(i).waitTime + processes.get(i).burstTime;
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
waitTimeTot += processes.get(i).waitTime;
tatTime += processes.get(i).turnAroundTime;
2018-10-26 20:02:49 +08:00
2020-10-24 18:23:28 +08:00
System.out.println(
"Process no.: "
+ i
+ " Wait time: "
+ processes.get(i).waitTime
+ " Turn Around Time: "
+ processes.get(i).turnAroundTime);
2018-10-26 20:02:49 +08:00
}
2020-10-24 18:23:28 +08:00
System.out.println("Average Waiting Time: " + waitTimeTot / noOfProcess);
System.out.println("Average TAT Time: " + tatTime / noOfProcess);
System.out.println("Throughput: " + (float) noOfProcess / (timer - 1));
}
2018-10-26 20:02:49 +08:00
}
public class SJF {
2020-10-24 18:23:28 +08:00
public static void main(String[] args) {
Schedule s = new Schedule();
s.startScheduling();
s.solve();
}
}