Skip to content

Commit 8a58aae

Browse files
authored
Merge pull request #1534 from khwaishchawla/patch-1
Create hrrn.c
2 parents 78260c8 + f6c6f4a commit 8a58aae

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
The Highest Response Ratio Next (HRRN) scheduling algorithm is a non-preemptive scheduling algorithm used in operating systems for process management. It is designed to reduce the average waiting time and improve overall efficiency. The key feature of HRRN is that it schedules the process with the highest response ratio first, which is calculated as:
2+
3+
Response Ratio= (Waiting Time+Burst Time)/Burst Time
4+
5+
6+
Where:
7+
8+
1.Waiting Time is the time the process has been waiting in the queue.
9+
2.Burst Time is the estimated execution time of the process.
10+
11+
Understanding the Formula:
12+
13+
1.If a process has been waiting for a long time, its waiting time becomes large, increasing the response ratio and its priority.
14+
2.If a process has a short burst time, it also gets a higher response ratio relative to its burst time, promoting shorter processes as well.
15+
16+
This formula balances between short and long processes, allowing processes with both high burst times and long waiting times to get scheduled without starving others.
17+
18+
Key Characteristics of HRRN:
19+
20+
1.Non-Preemptive: Once a process starts execution, it cannot be interrupted until it completes.
21+
2.Dynamic Priority Adjustment: Each process's priority is adjusted dynamically based on its waiting time and burst time, making it fairer than some other scheduling algorithms.
22+
3.Starvation Reduction: By increasing the response ratio over time, processes that have been waiting for a long period will eventually get scheduled, preventing them from being starved indefinitely.
23+
24+
Working of HRRN Scheduling Algorithm:
25+
26+
1.Arrival and Sorting: When a new set of processes arrive, they are sorted by arrival time to ensure that they are handled in the correct sequence.
27+
2.Response Ratio Calculation: For each process that has arrived and is waiting, the response ratio is calculated.
28+
3.Selection of Process: The process with the highest response ratio is selected to execute next.
29+
4.Execution: The selected process is executed completely (non-preemptively), updating the current time.
30+
5.Re-calculation: After the completion of each process, the response ratio is re-calculated for the remaining waiting processes.
31+
32+
Time Complexity
33+
The complexity of HRRN largely depends on two factors:
34+
35+
Sorting the processes initially by arrival time: O(n^2) of O(nlogn)
36+
Calculating response ratios and selecting the process with the highest ratio at each scheduling step: O(n^2)
37+
overal time complexity-O(n^2)
38+
Space Complexity
39+
The algorithm uses an array of structs to store process details:
40+
41+
Space complexity:
42+
O(n), where
43+
n is the number of processes.
44+
45+
Pros and Cons of HRRN
46+
Pros:
47+
48+
1.Fairly distributes CPU time among processes.
49+
2.Reduces waiting time and turnaround time compared to FCFS and SJF.
50+
3.Prevents starvation effectively.
51+
Cons:
52+
53+
1.It is more complex than simpler algorithms like FCFS and SJF.
54+
2.Requires periodic calculation of response ratios, increasing computational overhead.
55+
3.The HRRN scheduling algorithm is highly efficient in scenarios where fairness is essential, and the system needs to manage both short and long processes equitably without risking starvation.
56+
57+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdio.h>
2+
3+
typedef struct {
4+
int id; // Process ID
5+
int arrival; // Arrival Time
6+
int burst; // Burst Time
7+
int wait; // Waiting Time
8+
int turnAround; // Turnaround Time
9+
float response; // Response Ratio
10+
} Process;
11+
12+
void calculateHRRN(Process processes[], int n) {
13+
int completed = 0, time = 0;
14+
int idx = -1;
15+
16+
while (completed != n) {
17+
float maxRatio = -1;
18+
19+
// Calculate Response Ratio for each process
20+
for (int i = 0; i < n; i++) {
21+
if (processes[i].arrival <= time && processes[i].burst > 0) {
22+
float ratio = (float)(time - processes[i].arrival + processes[i].burst) / processes[i].burst;
23+
if (ratio > maxRatio) {
24+
maxRatio = ratio;
25+
idx = i;
26+
}
27+
}
28+
}
29+
30+
if (idx == -1) {
31+
time++;
32+
continue;
33+
}
34+
35+
// Process the selected task
36+
time += processes[idx].burst;
37+
processes[idx].wait = time - processes[idx].arrival - processes[idx].burst;
38+
processes[idx].turnAround = time - processes[idx].arrival;
39+
processes[idx].response = maxRatio;
40+
processes[idx].burst = 0;
41+
completed++;
42+
}
43+
}
44+
45+
void printProcesses(Process processes[], int n) {
46+
printf("ID\tArrival\tBurst\tWaiting\tTurnaround\tResponse\n");
47+
for (int i = 0; i < n; i++) {
48+
printf("%d\t%d\t%d\t%d\t%d\t\t%.2f\n",
49+
processes[i].id, processes[i].arrival, processes[i].burst,
50+
processes[i].wait, processes[i].turnAround, processes[i].response);
51+
}
52+
}
53+
54+
int main() {
55+
int n;
56+
57+
printf("Enter the number of processes: ");
58+
scanf("%d", &n);
59+
60+
Process processes[n];
61+
for (int i = 0; i < n; i++) {
62+
processes[i].id = i + 1;
63+
printf("Enter arrival and burst time for process %d: ", i + 1);
64+
scanf("%d %d", &processes[i].arrival, &processes[i].burst);
65+
processes[i].wait = 0;
66+
processes[i].turnAround = 0;
67+
processes[i].response = 0.0;
68+
}
69+
70+
calculateHRRN(processes, n);
71+
printProcesses(processes, n);
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)