-
Notifications
You must be signed in to change notification settings - Fork 0
/
SJFAlgorithm.java
75 lines (61 loc) · 2.75 KB
/
SJFAlgorithm.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
import java.util.*;
class Process {
int pid; // Process ID
int arrivalTime; // Arrival time of the process
int burstTime; // Burst time of the process
public Process(int pid, int arrivalTime, int burstTime) {
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
}
}
public class SJFAlgorithm {
// Function to implement Shortest Job First (SJF) scheduling algorithm
public static void sjfScheduling(Process[] processes) {
// Sort processes based on arrival time
Arrays.sort(processes, Comparator.comparingInt(p -> p.arrivalTime));
// Create a priority queue to store processes based on burst time
PriorityQueue<Process> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.burstTime));
int currentTime = 0;
float totalWaitingTime = 0;
float totalTurnaroundTime = 0;
// Process each process
for (Process process : processes) {
// Add processes that have arrived to the queue
while (!pq.isEmpty() && currentTime < process.arrivalTime) {
Process currentProcess = pq.poll();
currentTime += currentProcess.burstTime;
totalTurnaroundTime += currentTime - currentProcess.arrivalTime;
}
// If process hasn't arrived yet, update current time
if (currentTime < process.arrivalTime) {
currentTime = process.arrivalTime;
}
// Add current process to the queue
pq.offer(process);
// Process the current process
currentTime += process.burstTime;
totalTurnaroundTime += currentTime - process.arrivalTime;
// Calculate waiting time for the current process
totalWaitingTime += currentTime - process.arrivalTime - process.burstTime;
}
// Calculate average waiting time and average turnaround time
float avgWaitingTime = totalWaitingTime / processes.length;
float avgTurnaroundTime = totalTurnaroundTime / processes.length;
// Display average waiting time and average turnaround time
System.out.println("Average Waiting Time: " + avgWaitingTime);
System.out.println("Average Turnaround Time: " + avgTurnaroundTime);
}
public static void main(String[] args) {
// Create an array of processes
Process[] processes = {
new Process(1, 0, 6),
new Process(2, 1, 3),
new Process(3, 2, 7),
new Process(4, 3, 2),
new Process(5, 4, 5)
};
// Call SJF scheduling algorithm
sjfScheduling(processes);
}
}