-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityScheduling.java
More file actions
32 lines (24 loc) · 1.17 KB
/
Copy pathPriorityScheduling.java
File metadata and controls
32 lines (24 loc) · 1.17 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
import java.util.*;
public class PriorityScheduling {
public void schedule(List<PCB> readyQueue) {
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;
System.out.println("\n--- Priority Scheduling ---");
readyQueue.sort((a, b) -> b.priority - a.priority); // 8 highest
for (PCB job : readyQueue) {
job.startTime = currentTime;
job.finishTime = currentTime + job.burstTime;
currentTime = job.finishTime;
job.calculateTimes();
totalWaitingTime += job.waitingTime;
totalTurnaroundTime += job.turnaroundTime;
System.out.println("Job " + job.id + " | Priority: " + job.priority + " | Start: " + job.startTime + "ms | End: " + job.finishTime + "ms");
if (job.waitingTime > job.priority * 10) {
System.out.println("-> Job " + job.id + " suffered from STARVATION.");
}
}
System.out.println("Average Waiting Time: " + (totalWaitingTime / readyQueue.size()) + "ms");
System.out.println("Average Turnaround Time: " + (totalTurnaroundTime / readyQueue.size()) + "ms");
}
}