-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonPreemptivePriority.java
More file actions
42 lines (34 loc) · 1.72 KB
/
NonPreemptivePriority.java
File metadata and controls
42 lines (34 loc) · 1.72 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
import java.util.*;
public class NonPreemptivePriority {
public static List<String> schedule(List<Process> processes) {
// Sort the processes by arrival time to ensure they are considered in the correct order
processes.sort(Comparator.comparingInt(p -> p.arrivalTime));
PriorityQueue<Process> queue = new PriorityQueue<>(
Comparator.comparingInt((Process p) -> p.priority)
.thenComparingInt(p -> p.arrivalTime)
);
int currentTime = processes.get(0).arrivalTime; // Start from the first process's arrival time
List<String> ganttChart = new ArrayList<>();
int index = 0;
while (true) {
// Add all processes that have arrived by the current time
while (index < processes.size() && processes.get(index).arrivalTime <= currentTime) {
queue.add(processes.get(index));
index++;
}
if (queue.isEmpty()) {
if (index == processes.size()) break; // All processes are done
// Jump to the next arrival time if the queue is empty
currentTime = processes.get(index).arrivalTime;
continue;
}
Process currentProcess = queue.poll();
ganttChart.add("P" + currentProcess.id + " (" + currentTime + "-" + (currentTime + currentProcess.burstTime) + ")");
currentTime += currentProcess.burstTime;
currentProcess.finishTime = currentTime;
currentProcess.turnaroundTime = currentProcess.finishTime - currentProcess.arrivalTime;
currentProcess.waitingTime = currentProcess.turnaroundTime - currentProcess.burstTime;
}
return ganttChart;
}
}