forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNonPreemptivePriorityScheduling.java
111 lines (91 loc) · 3.8 KB
/
NonPreemptivePriorityScheduling.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.thealgorithms.scheduling;
import java.util.PriorityQueue;
/**
* This class implements the Non-Preemptive Priority Scheduling algorithm.
* Processes are executed in order of their priority. The process with the
* highest priority (lower priority number) is executed first,
* and once a process starts executing, it cannot be preempted.
*/
public final class NonPreemptivePriorityScheduling {
private NonPreemptivePriorityScheduling() {
}
static class Process implements Comparable<Process> {
int id; // Process ID
int burstTime; // Time required by the process for execution
int priority; // Priority of the process (lower value indicates higher priority)
Process(int id, int burstTime, int priority) {
this.id = id;
this.burstTime = burstTime;
this.priority = priority;
}
/**
* Compare based on priority for scheduling. The process with the lowest
* priority is selected first.
*/
@Override
public int compareTo(Process other) {
return Integer.compare(this.priority, other.priority);
}
}
/**
* Schedules processes based on their priority in a non-preemptive manner.
*
* @param processes Array of processes to be scheduled.
* @return Array of processes in the order they are executed.
*/
public static Process[] scheduleProcesses(Process[] processes) {
PriorityQueue<Process> pq = new PriorityQueue<>();
for (Process process : processes) {
pq.add(process);
}
Process[] executionOrder = new Process[processes.length];
int index = 0;
// Execute processes based on their priority
while (!pq.isEmpty()) {
executionOrder[index++] = pq.poll(); // Poll the process with the highest priority
}
return executionOrder;
}
/**
* Calculates the average waiting time of the processes.
*
* @param processes Array of processes.
* @param executionOrder Array of processes in execution order.
* @return Average waiting time.
*/
public static double calculateAverageWaitingTime(Process[] processes, Process[] executionOrder) {
int totalWaitingTime = 0;
int currentTime = 0;
for (Process process : executionOrder) {
totalWaitingTime += currentTime;
currentTime += process.burstTime;
}
return (double) totalWaitingTime / processes.length;
}
/**
* Calculates the average turn-around time of the processes.
*
* @param processes Array of processes.
* @param executionOrder Array of processes in execution order.
* @return Average turn-around time.
*/
public static double calculateAverageTurnaroundTime(Process[] processes, Process[] executionOrder) {
int totalTurnaroundTime = 0;
int currentTime = 0;
for (Process process : executionOrder) {
currentTime += process.burstTime;
totalTurnaroundTime += currentTime;
}
return (double) totalTurnaroundTime / processes.length;
}
public static void main(String[] args) {
Process[] processes = {new Process(1, 10, 2), new Process(2, 5, 1), new Process(3, 8, 3)};
Process[] executionOrder = scheduleProcesses(processes);
System.out.println("Process ID | Burst Time | Priority");
for (Process process : executionOrder) {
System.out.printf("%d %d %d%n", process.id, process.burstTime, process.priority);
}
System.out.printf("Average Waiting Time: %.2f%n", calculateAverageWaitingTime(processes, executionOrder));
System.out.printf("Average Turnaround Time: %.2f%n", calculateAverageTurnaroundTime(processes, executionOrder));
}
}