- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathHighestResponseRatioNextScheduling.java
158 lines (140 loc) · 5.83 KB
/
HighestResponseRatioNextScheduling.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
packagecom.thealgorithms.scheduling;
importjava.util.Arrays;
importjava.util.Comparator;
/**
* The {@code HighestResponseRatioNextScheduling} class implements the
* Highest Response Ratio Next (HRRN) scheduling algorithm.
* HRRN is a non-preemptive scheduling algorithm that selects the process with
* the highest response ratio for execution.
* The response ratio is calculated as:
*
* <pre>
* Response Ratio = (waiting time + burst time) / burst time
* </pre>
*
* HRRN is designed to reduce the average waiting time and improve overall
* system performance by balancing between short and long processes,
* minimizing process starvation.
*/
publicfinalclassHighestResponseRatioNextScheduling {
privatestaticfinalintPROCESS_NOT_FOUND = -1;
privatestaticfinaldoubleINITIAL_MAX_RESPONSE_RATIO = -1.0;
privateHighestResponseRatioNextScheduling() {
}
/**
* Represents a process in the scheduling algorithm.
*/
privatestaticclassProcess {
Stringname;
intarrivalTime;
intburstTime;
intturnAroundTime;
booleanfinished;
Process(Stringname, intarrivalTime, intburstTime) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.turnAroundTime = 0;
this.finished = false;
}
/**
* Calculates the response ratio for this process.
*
* @param currentTime The current time in the scheduling process.
* @return The response ratio for this process.
*/
doublecalculateResponseRatio(intcurrentTime) {
return (double) (burstTime + currentTime - arrivalTime) / burstTime;
}
}
/**
* Calculates the Turn Around Time (TAT) for each process.
*
* <p>Turn Around Time is calculated as the total time a process spends
* in the system from arrival to completion. It is the sum of the burst time
* and the waiting time.</p>
*
* @param processNames Array of process names.
* @param arrivalTimes Array of arrival times corresponding to each process.
* @param burstTimes Array of burst times for each process.
* @param noOfProcesses The number of processes.
* @return An array of Turn Around Times for each process.
*/
publicstaticint[] calculateTurnAroundTime(finalString[] processNames, finalint[] arrivalTimes, finalint[] burstTimes, finalintnoOfProcesses) {
intcurrentTime = 0;
int[] turnAroundTime = newint[noOfProcesses];
Process[] processes = newProcess[noOfProcesses];
for (inti = 0; i < noOfProcesses; i++) {
processes[i] = newProcess(processNames[i], arrivalTimes[i], burstTimes[i]);
}
Arrays.sort(processes, Comparator.comparingInt(p -> p.arrivalTime));
intfinishedProcessCount = 0;
while (finishedProcessCount < noOfProcesses) {
intnextProcessIndex = findNextProcess(processes, currentTime);
if (nextProcessIndex == PROCESS_NOT_FOUND) {
currentTime++;
continue;
}
ProcesscurrentProcess = processes[nextProcessIndex];
currentTime = Math.max(currentTime, currentProcess.arrivalTime);
currentProcess.turnAroundTime = currentTime + currentProcess.burstTime - currentProcess.arrivalTime;
currentTime += currentProcess.burstTime;
currentProcess.finished = true;
finishedProcessCount++;
}
for (inti = 0; i < noOfProcesses; i++) {
turnAroundTime[i] = processes[i].turnAroundTime;
}
returnturnAroundTime;
}
/**
* Calculates the Waiting Time (WT) for each process.
*
* @param turnAroundTime The Turn Around Times for each process.
* @param burstTimes The burst times for each process.
* @return An array of Waiting Times for each process.
*/
publicstaticint[] calculateWaitingTime(int[] turnAroundTime, int[] burstTimes) {
int[] waitingTime = newint[turnAroundTime.length];
for (inti = 0; i < turnAroundTime.length; i++) {
waitingTime[i] = turnAroundTime[i] - burstTimes[i];
}
returnwaitingTime;
}
/**
* Finds the next process to be scheduled based on arrival times and the current time.
*
* @param processes Array of Process objects.
* @param currentTime The current time in the scheduling process.
* @return The index of the next process to be scheduled, or PROCESS_NOT_FOUND if no process is ready.
*/
privatestaticintfindNextProcess(Process[] processes, intcurrentTime) {
returnfindHighestResponseRatio(processes, currentTime);
}
/**
* Finds the process with the highest response ratio.
*
* <p>The response ratio is calculated as:
* (waiting time + burst time) / burst time
* where waiting time = current time - arrival time</p>
*
* @param processes Array of Process objects.
* @param currentTime The current time in the scheduling process.
* @return The index of the process with the highest response ratio, or PROCESS_NOT_FOUND if no process is ready.
*/
privatestaticintfindHighestResponseRatio(Process[] processes, intcurrentTime) {
doublemaxResponseRatio = INITIAL_MAX_RESPONSE_RATIO;
intmaxIndex = PROCESS_NOT_FOUND;
for (inti = 0; i < processes.length; i++) {
Processprocess = processes[i];
if (!process.finished && process.arrivalTime <= currentTime) {
doubleresponseRatio = process.calculateResponseRatio(currentTime);
if (responseRatio > maxResponseRatio) {
maxResponseRatio = responseRatio;
maxIndex = i;
}
}
}
returnmaxIndex;
}
}