Skip to content
Related Articles

Related Articles

Highest Response Ratio Next (HRRN) CPU Scheduling

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Nov, 2022
Improve Article
Save Article

Given N processes with their Arrival times and Burst times, the task is to find average waiting time and an average turn around time using HRRN scheduling algorithm. 

The name itself states that we need to find the response ratio of all available processes and select the one with the highest Response Ratio. A process once selected will run till completion.

Characteristics of HRRN CPU Scheduling:

  • Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is considered as one of the most optimal scheduling algorithm.
  • The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive. 
  • HRRN is basically considered as the modification of Shortest Job First in order to reduce the problem of starvation.
  • In comparison with SJF, during HRRN scheduling algorithm, the CPU is allotted to the next process which has the highest response ratio and not to the process having less burst time.

 Response Ratio = (W + S)/S

Here, W is the waiting time of the process so far and S is the Burst time of the process.

Advantages of HRRN CPU Scheduling

  • HRRN Scheduling algorithm generally gives better performance than the shortest job first Scheduling.
  • There is a reduction in waiting time for longer jobs and also it encourages shorter jobs.

Disadvantages of HRRN CPU Scheduling

  • The on ground implementation of HRRN scheduling is not possible as it is not possible know the burst time of every job in advance.
  • In this scheduling, there may occur overload on the CPU.

Performance of HRRN –  

  • Shorter Processes are favoured.
  • Aging without service increases ratio, longer jobs can get past shorter jobs.

Let us consider the following examples. 

Example-1: Consider the following table of arrival time and burst time for four processes P1, P2, P3, P4 and P5.  

ProcessesArrival time Burst Time
P1  0ms3ms
P2            2ms6ms
P3 4ms4ms
P4     6ms5ms
P58ms2ms

The Highest Response Ratio Next CPU Scheduling Algorithm will work on the basis of steps as mentioned below:

At time= 0,

  • Available Processes: P1, Hence P1 starts executing till its completion.
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
0-2msP10ms P12ms3ms1ms

At time = 2,

  • Available Processes: P1, P2
  • But P1 keep executing as HRRN is a non-preemptive algorithm and thus it will finish its execution
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
2-3msP10msP2P11ms1ms0ms
P22ms0ms6ms6ms

At time = 3,

  • Process P1 finish its execution
  • Process P2 starts executing as it is only process available.
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
3-4msP22ms P21ms6ms5ms

At time = 4,

  • Process P3 arrives and wait for process P2 to execute
  • Process P2 continue its execution
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
4-6msP22msP3P22ms5ms3ms
P34ms0ms4ms4ms

At time = 6,

  • Process P4 arrives and wait for the process P2 to execute
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
6-8msP22msP3, P4P22ms3ms1ms
P34ms0ms4ms4ms
P46ms0ms5ms5ms

At time = 8,

  • Process P5 arrives and wait for its execution in the ready queue
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
8-9msP22msP3, P4, P5P21ms1ms0ms
P34ms0ms4ms4ms
P46ms0ms5ms5ms
P58ms0ms2ms2ms

At time = 9,

  • Process P2 completes its execution 
  • Now there are 3 processes available, P3, P4 and P5. Since, P3, P4, P5 were available after 4, 6 and 8 units respectively.
  • Therefore, waiting time for P3, P4 and P5 are (9 – 4 =)5, (9 – 6 =)3, and (9 – 8 =)1 unit respectively.
  • Using the formula given above,  (Response Ratio = (W + S)/S) calculate the Response Ratios of P3, P4 and P5 respectively as 2.25, 1.6 and 1.5.
  • Clearly, P3 has the highest Response Ratio and so it gets scheduled
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
9-13msP34msP4, P5P34ms4ms0ms
P46ms0ms5ms5ms
P58ms0ms2ms2ms

At time = 13,

  • Available Processes: P4 and P5
  • Response Ratios of P4 and P5 are 2.4 and 3.5 respectively using the above formula.
  • Clearly, P5 has the highest Response Ratio and so it gets scheduled
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
13-15msP46msP4P50ms5ms5ms
P58ms2ms2ms0ms

At time = 15,

  • After completion of process P5, Process P4 is selected at last and execute till it gets finished
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
15-20msP46ms P45ms5ms0ms

At time = 20,

  • Process P4 will finish its execution.
  • The overall execution of the processes will be as shown below:
Time InstanceProcessArrival TimeReady QueueRunning QueueExecution TimeInitial Burst TimeRemaining Burst 
Time
0-2msP10ms P12ms3ms1ms
2-3msP10msP2P11ms1ms0ms
P22ms0ms6ms6ms
3-4msP22ms P21ms6ms5ms
4-6msP22msP3P22ms5ms3ms
P34ms0ms4ms4ms
6-8msP22msP3, P4P22ms3ms1ms
P34ms0ms4ms4ms
P46ms0ms5ms5ms
8-9msP22msP3, P4, P5P21ms1ms0ms
P34ms0ms4ms4ms
P46ms0ms5ms5ms
P58ms0ms2ms2ms
9-13msP34msP4, P5P34ms4ms0ms
P46ms0ms5ms5ms
P58ms0ms2ms2ms
13-15msP46msP4P50ms5ms5ms
P58ms2ms2ms0ms
15-20msP46ms P45ms5ms0ms

Gantt Chart – 

HRRN Scheduling Example Soolution

Since, completion time (C.T) can be directly determined by Gantt chart, and  

Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)

Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time) 

Therefore, final table look like,  

ProcessesATBTCTTATWT
P10333-0 = 33-3 = 0
P22699-2 = 77-6 = 1
P3441313-4 = 99-4 = 5
P4652020-6 = 1414-5 = 9
P5821515-8 = 77-2 = 5

Output:  

Total Turn Around Time = 40 ms
So, Average Turn Around Time = 40/5 = 8.00 ms

And, Total Waiting Time = 20 ms
So, Average Waiting Time = 20/5 = 4.00 ms 

Implementation of HRRN Scheduling –  

  • Input the number of processes, their arrival times and burst times.
  • Sort them according to their arrival times.
  • At any given time calculate the response ratios and select the appropriate process to be scheduled.
  • Calculate the turn around time as completion time – arrival time.
  • Calculate the waiting time as turn around time – burst time.
  • Turn around time divided by the burst time gives the normalized turn around time.
  • Sum up the waiting and turn around times of all processes and divide by the number of processes to get the average waiting and turn around time.

Below is the implementation of above approach: 

C++




// C++ program for Highest Response Ratio Next (HRRN)
// Scheduling
#include <bits/stdc++.h>
using namespace std;
// Defining process details
struct process {
    char name;
    int at, bt, ct, wt, tt;
    int completed;
    float ntt;
} p[10];
 
int n;
 
// Sorting Processes by Arrival Time
void sortByArrival()
{
    struct process temp;
    int i, j;
 
    // Selection Sort applied
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
 
            // Check for lesser arrival time
            if (p[i].at > p[j].at) {
 
                // Swap earlier process to front
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
}
 
int main()
{
    int i, j, sum_bt = 0;
    char c;
    float t, avgwt = 0, avgtt = 0;
    n = 5;
 
    // predefined arrival times
    int arriv[] = { 0, 2, 4, 6, 8 };
 
    // predefined burst times
    int burst[] = { 3, 6, 4, 5, 2 };
 
    // Initializing the structure variables
    for (i = 0, c = 'A'; i < n; i++, c++) {
        p[i].name = c;
        p[i].at = arriv[i];
        p[i].bt = burst[i];
 
        // Variable for Completion status
        // Pending = 0
        // Completed = 1
        p[i].completed = 0;
 
        // Variable for sum of all Burst Times
        sum_bt += p[i].bt;
    }
 
    // Sorting the structure by arrival times
    sortByArrival();
    cout << "PN\tAT\tBT\tWT\tTAT\tNTT";
    for (t = p[0].at; t < sum_bt;) {
 
        // Set lower limit to response ratio
        float hrr = -9999;
 
        // Response Ratio Variable
        float temp;
 
        // Variable to store next process selected
        int loc;
        for (i = 0; i < n; i++) {
 
            // Checking if process has arrived and is
            // Incomplete
            if (p[i].at <= t && p[i].completed != 1) {
 
                // Calculating Response Ratio
                temp = (p[i].bt + (t - p[i].at)) / p[i].bt;
 
                // Checking for Highest Response Ratio
                if (hrr < temp) {
 
                    // Storing Response Ratio
                    hrr = temp;
 
                    // Storing Location
                    loc = i;
                }
            }
        }
 
        // Updating time value
        t += p[loc].bt;
 
        // Calculation of waiting time
        p[loc].wt = t - p[loc].at - p[loc].bt;
 
        // Calculation of Turn Around Time
        p[loc].tt = t - p[loc].at;
 
        // Sum Turn Around Time for average
        avgtt += p[loc].tt;
 
        // Calculation of Normalized Turn Around Time
        p[loc].ntt = ((float)p[loc].tt / p[loc].bt);
 
        // Updating Completion Status
        p[loc].completed = 1;
 
        // Sum Waiting Time for average
        avgwt += p[loc].wt;
        cout << "\n" << p[loc].name << "\t" << p[loc].at;
        cout << "\t" << p[loc].bt << "\t" << p[loc].wt;
        cout << "\t" << p[loc].tt << "\t" << p[loc].ntt;
    }
    cout << "\nAverage waiting time: " << avgwt / n << endl;
    cout << "Average Turn Around time:" << avgtt / n;
}
// This code is contributed by shivi_Aggarwal

C




// C program for Highest Response Ratio Next (HRRN) Scheduling
#include <stdio.h>
 
// Defining process details
struct process {
    char name;
    int at, bt, ct, wt, tt;
    int completed;
    float ntt;
} p[10];
 
int n;
 
// Sorting Processes by Arrival Time
void sortByArrival()
{
    struct process temp;
    int i, j;
 
    // Selection Sort applied
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
 
            // Check for lesser arrival time
            if (p[i].at > p[j].at) {
 
                // Swap earlier process to front
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
}
 
void main()
{
    int i, j, t, sum_bt = 0;
    char c;
    float avgwt = 0, avgtt = 0;
    n = 5;
 
    // predefined arrival times
    int arriv[] = { 0, 2, 4, 6, 8 };
 
    // predefined burst times
    int burst[] = { 3, 6, 4, 5, 2 };
 
    // Initializing the structure variables
    for (i = 0, c = 'A'; i < n; i++, c++) {
        p[i].name = c;
        p[i].at = arriv[i];
        p[i].bt = burst[i];
 
        // Variable for Completion status
        // Pending = 0
        // Completed = 1
        p[i].completed = 0;
 
        // Variable for sum of all Burst Times
        sum_bt += p[i].bt;
    }
 
    // Sorting the structure by arrival times
    sortByArrival();
    printf("\nName\tArrival Time\tBurst Time\tWaiting Time");
    printf("\tTurnAround Time\t Normalized TT");
    for (t = p[0].at; t < sum_bt;) {
 
        // Set lower limit to response ratio
        float hrr = -9999;
 
        // Response Ratio Variable
        float temp;
 
        // Variable to store next process selected
        int loc;
        for (i = 0; i < n; i++) {
 
            // Checking if process has arrived and is Incomplete
            if (p[i].at <= t && p[i].completed != 1) {
 
                // Calculating Response Ratio
                temp = (p[i].bt + (t - p[i].at)) / p[i].bt;
 
                // Checking for Highest Response Ratio
                if (hrr < temp) {
 
                    // Storing Response Ratio
                    hrr = temp;
 
                    // Storing Location
                    loc = i;
                }
            }
        }
 
        // Updating time value
        t += p[loc].bt;
 
        // Calculation of waiting time
        p[loc].wt = t - p[loc].at - p[loc].bt;
 
        // Calculation of Turn Around Time
        p[loc].tt = t - p[loc].at;
 
        // Sum Turn Around Time for average
        avgtt += p[loc].tt;
 
        // Calculation of Normalized Turn Around Time
        p[loc].ntt = ((float)p[loc].tt / p[loc].bt);
 
        // Updating Completion Status
        p[loc].completed = 1;
 
        // Sum Waiting Time for average
        avgwt += p[loc].wt;
        printf("\n%c\t\t%d\t\t", p[loc].name, p[loc].at);
        printf("%d\t\t%d\t\t", p[loc].bt, p[loc].wt);
        printf("%d\t\t%f", p[loc].tt, p[loc].ntt);
    }
    printf("\nAverage waiting time:%f\n", avgwt / n);
    printf("Average Turn Around time:%f\n", avgtt / n);
}

Python3




# Python3 program for Highest Response Ratio
# Next (HRRN) Scheduling
 
# Function to sort process by arrival time
def sortByArrival(at, n):
     
    # Selection Sort applied 
    for i in range(0, n - 1):
        for j in range(i + 1, n):
             
            # Check for lesser arrival time 
            if at[i] > at[j]:
                 
                # Swap earlier process to front
                at[i], at[j] = at[j], at[i]
 
# Driver code
if __name__ == '__main__':
     
    sum_bt = 0
    avgwt = 0
    avgTT = 0
    n = 5
     
    completed =[0] * n
    waiting_time = [0] * n
    turnaround_time = [0] * n
    normalised_TT = [0] * n
     
    # Predefined arrival times 
    arrival_time = [ 0, 2, 4, 6, 8 ]
     
    # Predefined burst times 
    burst_time = [ 3, 6, 4, 5, 2 ]
    process = []
     
    # Initializing the structure variables 
    for i in range(0, n):
        process.append(chr(65 + i))
        sum_bt += burst_time[i]
         
    # Sorting the structure by arrival times 
    sortByArrival(arrival_time, n)
    print("Name", "Arrival time",
          "Burst time", "Waiting Time",
          "Turnaround ", "Normalized TT")
    t = arrival_time[0]
     
    while(t < sum_bt):
         
        # Set lower limit to response ratio 
        hrr = -9999
        temp, loc = 0, 0
         
        for i in range(0, n):
             
            # Checking if process has arrived
            # and is Incomplete 
            if arrival_time[i] <= t and completed[i] != 1:
                 
                # Calculating Response Ratio 
                temp = ((burst_time[i] +
                        (t - arrival_time[i])) /
                         burst_time[i])
                          
                # Checking for Highest Response Ratio 
                if hrr < temp:
                     
                    # Storing Response Ratio 
                    hrr = temp
                     
                    # Storing Location
                    loc = i
                     
        # Updating time value 
        t += burst_time[loc]
         
        # Calculation of waiting time
        waiting_time[loc] = (t - arrival_time[loc] -
                                 burst_time[loc])
         
        # Calculation of Turn Around Time 
        turnaround_time[loc] = t - arrival_time[loc]
         
        # Sum Turn Around Time for average 
        avgTT += turnaround_time[loc]
         
        # Calculation of Normalized Turn Around Time 
        normalised_TT = float(turnaround_time[loc] /
                              burst_time[loc])
         
        # Updating Completion Status
        completed[loc] = 1
         
        # Sum Waiting Time for average 
        avgwt += waiting_time[loc]
         
        print(process[loc], "\t\t", arrival_time[loc],
              "\t\t", burst_time[loc], "\t\t",
              waiting_time[loc], "\t\t",
              turnaround_time[loc], "\t\t",
              "{0:.6f}".format(normalised_TT))
 
    print("Average waiting time: {0:.6f}".format(avgwt / n))
    print("Average Turn Around time:  {0:.6f}".format(avgTT / n))
 
# This code is contributed by etcharla revanth rao

Output

PN    AT    BT    WT    TAT    NTT
A    0    3    0    3    1
B    2    6    1    7    1.16667
C    4    4    5    9    2.25
E    8    2    5    7    3.5
D    6    5    9    14    2.8
Average waiting time: 4
Average Turn Around time:8

This article is contributed by Siddhant Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!