-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathNonPreemptivePriorityScheduling.java
133 lines (116 loc) · 4.77 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.thealgorithms.scheduling;
import java.util.Collections;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* 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() {
}
/**
* Represents a process with an ID, burst time, priority, arrival time, and start time.
*/
static class Process implements Comparable<Process> {
int id;
int arrivalTime;
int startTime;
int burstTime;
int priority;
/**
* Constructs a Process instance with the specified parameters.
*
* @param id Unique identifier for the process
* @param arrivalTime Time when the process arrives in the system
* @param burstTime Time required for the process execution
* @param priority Priority of the process
*/
Process(int id, int arrivalTime, int burstTime, int priority) {
this.id = id;
this.arrivalTime = arrivalTime;
this.startTime = -1;
this.burstTime = burstTime;
this.priority = priority;
}
/**
* Compare based on priority for scheduling. The process with the lowest
* priority is selected first.
* If two processes have the same priority, the one that arrives earlier is selected.
*
* @param other The other process to compare against
* @return A negative integer, zero, or a positive integer as this process
* is less than, equal to, or greater than the specified process.
*/
@Override
public int compareTo(Process other) {
if (this.priority == other.priority) {
return Integer.compare(this.arrivalTime, other.arrivalTime);
}
return Integer.compare(this.priority, other.priority);
}
}
/**
* Schedules processes based on their priority in a non-preemptive manner, considering their arrival times.
*
* @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<>();
Queue<Process> waitingQueue = new LinkedList<>();
int currentTime = 0;
int index = 0;
Process[] executionOrder = new Process[processes.length];
Collections.addAll(waitingQueue, processes);
while (!waitingQueue.isEmpty() || !pq.isEmpty()) {
// Add processes that have arrived to the priority queue
while (!waitingQueue.isEmpty() && waitingQueue.peek().arrivalTime <= currentTime) {
pq.add(waitingQueue.poll());
}
if (!pq.isEmpty()) {
Process currentProcess = pq.poll();
currentProcess.startTime = currentTime;
executionOrder[index++] = currentProcess;
currentTime += currentProcess.burstTime;
} else {
// If no process is ready, move to the next arrival time
currentTime = waitingQueue.peek().arrivalTime;
}
}
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;
for (Process process : executionOrder) {
int waitingTime = process.startTime - process.arrivalTime;
totalWaitingTime += waitingTime;
}
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;
for (Process process : executionOrder) {
int turnaroundTime = process.startTime + process.burstTime - process.arrivalTime;
totalTurnaroundTime += turnaroundTime;
}
return (double) totalTurnaroundTime / processes.length;
}
}