The Wayback Machine - https://web.archive.org/web/20241001020023/https://www.geeksforgeeks.org/highest-response-ratio-next-hrrn-cpu-scheduling/
Open In App

Highest Response Ratio Next (HRRN) CPU Scheduling

Last Updated : 04 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CPU scheduling is the process of deciding which process will own the CPU to use while another process is suspended. The main function of CPU scheduling is to ensure that whenever the CPU remains idle, the OS has at least selected one of the processes available in the ready-to-use line. Highest Response Ratio Next (HRRN) Scheduling is a part of nonpreemptive CPU scheduling.

What is the Highest Response Ratio Next (HRRN) Scheduling?

One of the most optimal scheduling algorithms is the Highest Response Ratio Next (HRNN). This algorithm is a non-preemptive algorithm in which, HRRN scheduling is done based on an extra parameter, which is called Response Ratio. Given N processes with their Arrival times and Burst times, the task is to find the average waiting time and an average turnaround time using the 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. below is the formula to calculate the Response Ratio.

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.

Characteristics of HRRN Scheduling

  • Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is considered as one of the most optimal scheduling algorithms.
  • The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive. 
  • HRRN is considered as the modification of the Shortest Job First to reduce the problem of starvation.
  • In comparison with SJF, during the 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.

Performance of HRRN Scheduling

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

Examples of  Highest Response Ratio Next (HRRN) Scheduling

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

Processes Arrival time  Burst Time
P1   0ms 3ms
P2             2ms 6ms
P3  4ms 4ms
P4      6ms 5ms
P5 8ms 2ms

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 Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
0-2ms P1 0ms   P1 2ms 3ms 1ms

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 Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
2-3ms P1 0ms P2 P1 1ms 1ms 0ms
P2 2ms 0ms 6ms 6ms

At time = 3,

  • Process P1 finish its execution
  • Process P2 starts executing as it is only process available.
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
3-4ms P2 2ms   P2 1ms 6ms 5ms

At time = 4,

  • Process P3 arrives and wait for process P2 to execute
  • Process P2 continue its execution
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
4-6ms P2 2ms P3 P2 2ms 5ms 3ms
P3 4ms 0ms 4ms 4ms

At time = 6,

  • Process P4 arrives and wait for the process P2 to execute
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
6-8ms P2 2ms P3, P4 P2 2ms 3ms 1ms
P3 4ms 0ms 4ms 4ms
P4 6ms 0ms 5ms 5ms

At time = 8,

  • Process P5 arrives and wait for its execution in the ready queue
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
8-9ms P2 2ms P3, P4, P5 P2 1ms 1ms 0ms
P3 4ms 0ms 4ms 4ms
P4 6ms 0ms 5ms 5ms
P5 8ms 0ms 2ms 2ms

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 Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
9-13ms P3 4ms P4, P5 P3 4ms 4ms 0ms
P4 6ms 0ms 5ms 5ms
P5 8ms 0ms 2ms 2ms

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 Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
13-15ms P4 6ms P4 P5 0ms 5ms 5ms
P5 8ms 2ms 2ms 0ms

At time = 15,

  • After completion of process P5, Process P4 is selected at last and execute till it gets finished
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
15-20ms P4 6ms   P4 5ms 5ms 0ms

At time = 20,

  • Process P4 will finish its execution.
  • The overall execution of the processes will be as shown below:
Time Instance Process Arrival Time Ready Queue Running Queue Execution Time Initial Burst Time Remaining Burst 
Time
0-2ms P1 0ms   P1 2ms 3ms 1ms
2-3ms P1 0ms P2 P1 1ms 1ms 0ms
P2 2ms 0ms 6ms 6ms
3-4ms P2 2ms   P2 1ms 6ms 5ms
4-6ms P2 2ms P3 P2 2ms 5ms 3ms
P3 4ms 0ms 4ms 4ms
6-8ms P2 2ms P3, P4 P2 2ms 3ms 1ms
P3 4ms 0ms 4ms 4ms
P4 6ms 0ms 5ms 5ms
8-9ms P2 2ms P3, P4, P5 P2 1ms 1ms 0ms
P3 4ms 0ms 4ms 4ms
P4 6ms 0ms 5ms 5ms
P5 8ms 0ms 2ms 2ms
9-13ms P3 4ms P4, P5 P3 4ms 4ms 0ms
P4 6ms 0ms 5ms 5ms
P5 8ms 0ms 2ms 2ms
13-15ms P4 6ms P4 P5 0ms 5ms 5ms
P5 8ms 2ms 2ms 0ms
15-20ms P4 6ms   P4 5ms 5ms 0ms

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,  

Processes AT BT CT TAT WT
P1 0 3 3 3-0 = 3 3-3 = 0
P2 2 6 9 9-2 = 7 7-6 = 1
P3 4 4 13 13-4 = 9 9-4 = 5
P4 6 5 20 20-6 = 14 14-5 = 9
P5 8 2 15 15-8 = 7 7-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);
}


Java




// Java equivalent
import java.util.Arrays;
   
// Defining process details
class Process {
    char name;
    int at, bt, ct, wt, tt;
    int completed;
    float ntt;
}
   
public class HRRN {
   
    // Sorting Processes by Arrival Time
    static void sortByArrival(Process p[], int n)
    {
        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;
                }
            }
        }
    }
   
    public static void main(String[] args)
    {
        int i, j, sum_bt = 0;
        char c;
        float t, avgwt = 0, avgtt = 0;
        int n = 5;
   
        // predefined arrival times
        int arriv[] = { 0, 2, 4, 6, 8 };
   
        // predefined burst times
        int burst[] = { 3, 6, 4, 5, 2 };
   
        Process[] p = new Process[n];
   
        // Initializing the structure variables
        for (i = 0, c = 'A'; i < n; i++, c++) {
            p[i] = new Process();
            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(p, n);
        System.out.println("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 = -1;
            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 = (int)(t - p[loc].at - p[loc].bt);
   
            // Calculation of Turn Around Time
            p[loc].tt = (int)(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;
            System.out.println(p[loc].name + "\t" + p[loc].at + "\t" + p[loc].bt
                               + "\t" + p[loc].wt + "\t" + p[loc].tt
                               + "\t" + p[loc].ntt);
        }
        System.out.println("Average waiting time: " + (avgwt / n));
        System.out.println("Average Turn Around time:" + (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


C#




using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
// C# equivalent
   
// Defining process details
class Process {
    public char name;
    public int at, bt, ct, wt, tt;
    public int completed;
    public float ntt;
}
 
class HelloWorld {
     
    // Sorting Processes by Arrival Time
    public static void sortByArrival(Process[] p, int n)
    {
        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;
                }
            }
        }
    }
     
    static void Main() {
        int i, j, sum_bt = 0;
        char c;
        float t, avgwt = 0, avgtt = 0;
        int n = 5;
   
        // predefined arrival times
        int[] arriv = { 0, 2, 4, 6, 8 };
   
        // predefined burst times
        int[] burst = { 3, 6, 4, 5, 2 };
   
        Process[] p = new Process[n];
   
        // Initializing the structure variables
        for (i = 0, c = 'A'; i < n; i++, c++) {
            p[i] = new Process();
            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(p, n);
        Console.WriteLine("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 = -1;
            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 = (int)(t - p[loc].at - p[loc].bt);
   
            // Calculation of Turn Around Time
            p[loc].tt = (int)(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;
            Console.WriteLine(p[loc].name + "\t" + p[loc].at + "\t" + p[loc].bt
                               + "\t" + p[loc].wt + "\t" + p[loc].tt
                               + "\t" + p[loc].ntt);
        }
        Console.WriteLine("Average waiting time: " + (avgwt / n));
        Console.WriteLine("Average Turn Around time:" + (avgtt / n));
    }
}
 
// The code is contributed by Nidhi goel.


Javascript




// javascript program for Highest Response Ratio Next (HRRN)
// Scheduling
 
// Defining process details
class process {
     
    constructor(){
        this.name = '#';
        this.at = 0;
        this.bt = 0;
        this.ct = 0;
        this.wt = 0;
        this.tt = 0;
        this.completed= 0;
        this.ntt = 0;
    }
 
}
let p = new Array(10);
for(let i = 0; i < 10; i++){
    p[i] = new process();
}
 
let n;
 
// Sorting Processes by Arrival Time
function sortByArrival()
{
    let temp;
    let 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;
            }
        }
    }
}
 
 
let i, j, sum_bt = 0;
let c;
let t, avgwt = 0, avgtt = 0;
n = 5;
 
// predefined arrival times
let arriv = [0, 2, 4, 6, 8];
 
// predefined burst times
let burst = [3, 6, 4, 5, 2];
 
// Initializing the structure variables
for (i = 0, c = 'A'; i < n; i++) {
    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;
    c = String.fromCharCode(c.charCodeAt(0) + 1);
}
 
// Sorting the structure by arrival times
sortByArrival();
console.log("PN\tAT\tBT\tWT\tTAT\tNTT");
 
for (t = p[0].at; t < sum_bt;) {
 
    // Set lower limit to response ratio
    let hrr = -9999;
 
    // Response Ratio Variable
    let temp;
 
    // Variable to store next process selected
    let 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 = (p[loc].tt / p[loc].bt);
 
    // Updating Completion Status
    p[loc].completed = 1;
 
    // Sum Waiting Time for average
    avgwt += p[loc].wt;
    console.log(p[loc].name + "\t" + p[loc].at + "\t" + p[loc].bt + "\t" + p[loc].wt + "\t" + p[loc].tt + "\t" + p[loc].ntt);
}
console.log("\nAverage waiting time: " + avgwt / n);
console.log("Average Turn Around time:" + avgtt / n);
 
// This code is contributed by Nidhi goel.


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



Advantages of HRRN 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.
  • Using HRRN algorithm, the throughput increases.

Disadvantages of HRRN 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.

FAQs on Highest Response Ratio Next (HRRN) Scheduling

Q.1: Is HRRN preemptive or non preemptive?

Answer:

The HRRN algorithm is non-preemptive in nature, and it bases scheduling decisions on an additional parameter known as response ratio. Every job that is open is assigned a response ratio, the job with the highest response ratio takes precedence over the others.

Q.2: What are the problems with HRRN?

Answer:

The following is the HRNN scheduling algorithm’s drawback:

  • The inability to predict the peak period of each operation makes the practical application of HRRN scheduling unfeasible.

Q.3: How does HRRN scheduling work?

Answer:

basically, HRRN is thought of as a modification of Shortest Job First to lessen the problem of starvation. Between SJF and HRRN scheduling algorithm, the CPU is automatically allotted to the next process which has the highest response ratio.



Similar Reads

HRRN Scheduling Full Form
HRRN stands for Highest Response Ratio Next. HRRN scheduling is non-preemptive scheduling, i.e. Non-preemptive scheduling is scheduling in which priority of each job depends on its estimated run time and the amount of time spent on waiting. The longer they wait they gain higher priority which prevents the process of starvation. i.e. The Response ra
2 min read
Difference between Priority Scheduling and Round Robin (RR) CPU scheduling
1. Priority Scheduling Algorithm : Priority scheduling algorithm executes the processes depending upon their priority. Each process is allocated a priority and the process with the highest priority is executed first. Priorities can be defined internally as well as externally. Internal priorities are decided by the system depending upon the number o
3 min read
Difference between Priority scheduling and Shortest Job First (SJF) CPU scheduling
1. Priority Scheduling Algorithm : Priority scheduling algorithm executes the processes depending upon their priority. Each process is allocated a priority and the process with the highest priority is executed first. Priorities can be defined internally as well as externally. Internal priorities are decided by the system depending upon the number o
3 min read
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling is like Multilevel Queue(MLQ) Scheduling but in this process can move between the queues. And thus, much more efficient than multilevel queue scheduling. Characteristics of Multilevel Feedback Queue Scheduling: In a multilevel queue-scheduling algorithm, processes are permanently assigned t
5 min read
Difference Between User-CPU-Time and System-CPU-Time in UNIX
There are two kinds of CPU time in any UNIX system: User CPU Time and System CPU Time. These metrics help in understanding the way in which the overall execution time is spent by any process using CPU resources. User CPU time is the total amount of time that the CPU spends executing both user processes and those parts of programs that run on behalf
8 min read
Operating Systems | CPU Scheduling | Question 1
Consider three processes (process id 0, 1, 2 respectively) with compute time bursts 2, 4 and 8 time units. All processes arrive at time zero. Consider the longest remaining time first (LRTF) scheduling algorithm. In LRTF ties are broken by giving priority to the process with the lowest process id. The average turn around time is: (A) 13 units (B) 1
1 min read
Operating Systems | CPU Scheduling | Question 2
Consider three processes, all arriving at time zero, with total execution time of 10, 20 and 30 units, respectively. Each process spends the first 20% of execution time doing I/O, the next 70% of time doing computation, and the last 10% of time doing I/O again. The operating system uses a shortest remaining compute time first scheduling algorithm a
2 min read
Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
Prerequisite - CPU Scheduling Priority Scheduling : In priority scheduling, each process has a priority which is an integer value assigned to it. The smallest integer is considered as the highest priority and the largest integer is considered as the lowest priority. The process with the highest priority gets the CPU first. In rare systems, the larg
4 min read
Longest Remaining Time First (LRTF) CPU Scheduling Program
We have given some processes with arrival time and Burst Time and we have to find the completion time (CT), Turn Around Time(TAT), Average Turn Around Time (Avg TAT), Waiting Time(WT), Average Waiting Time (AWT) for the given processes. Prerequisite: CPU Scheduling | Longest Remaining Time First (LRTF) algorithm LRTF is a preemptive scheduling algo
15+ min read
Advantages and Disadvantages of various CPU scheduling algorithms
CPU Scheduling involves many different scheduling algorithms which have their Advantages and Disadvantages. 1. First Come First Serve (FCFS): Advantages: It is simple and easy to understand.FCFS provides fairness by treating all processes equally and giving them an equal opportunity to run.FCFS guarantees that every process will eventually get a ch
3 min read
Difference between SJF and LJF CPU scheduling algorithms
Shortest Job First: The shortest job first (SJF) algorithm is a CPU scheduling algorithm designed to reorder the jobs so that the process having the smallest burst time is chosen for the next execution. It is used to reduce the average waiting time for other processes waiting for execution. This may be preemptive or non-preemptive. Its preemptive v
4 min read
Fair-share CPU scheduling
Fair-share scheduling is a scheduling algorithm that was first designed by Judy Kay and Piers Lauder at Sydney University in the 1980s. It is a scheduling algorithm for computer operating systems that dynamically distributes the time quanta "equally" to its users. Time quantum is the processor time allowed for a process to run. But in a round-robin
2 min read
Difference between SJF and SRJF CPU scheduling algorithms
1. Shortest Job First (SJF) : The Shortest Job First (SJF) is a scheduling policy that selects the waiting process with the smallest execution time to execute next. It is also known as Shortest Job Next (SJN) or Shortest Process Next (SPN). It is a non-preemptive scheduling algorithm. 2. Shortest Remaining Job First (SRTF) : The Shortest Remaining
2 min read
Difference between SRJF and LRJF CPU scheduling algorithms
1. Shortest remaining job first (SRJF) : Shortest remaining job first also called the shortest remaining time first is the preemptive version of the shortest job first scheduling algorithm. In the shortest remaining job first, the process with the smallest runtime to complete (i.e remaining time) is scheduled to run next, In SRJF, a running process
3 min read
Difference between First Come First Served (FCFS) and Longest Job First (LJF) CPU scheduling algorithms
1.First Come First Served (FCFS) : First Come First Served (FCFS) is the simplest type of algorithm. It is a non-preemptive algorithm i.e. the process cannot be interrupted once it starts executing. The FCFS is implemented with the help of a FIFO queue. The processes are put into the ready queue in the order of their arrival time. The process that
3 min read
Difference between FCFS and Priority CPU scheduling
1. First Come First Served (FCFS) : First Come First Served (FCFS) is the simplest type of algorithm. It is a non-preemptive algorithm i.e. the process cannot be interrupted once it starts executing. The FCFS is implemented with the help of a FIFO queue. The processes are put into the ready queue in the order of their arrival time. The process that
3 min read
Longest Job First (LJF) CPU Scheduling Algorithm
Longest Job First (LJF) is a non-preemptive scheduling algorithm. This algorithm is based on the burst time of the processes. The processes are put into the ready queue based on their burst times i.e., in descending order of the burst times. As the name suggests this algorithm is based on the fact that the process with the largest burst time is pro
5 min read
Deadline Monotonic CPU Scheduling
Prerequisites - CPU Scheduling Preemptive Scheduling Program for Preemptive Priority CPU Scheduling Deadline Monotonic Scheduling : It is a fixed priority based algorithm in which priorities are assigned to each task based on their relative deadline. Task with shortest deadline is assigned highest priority. It is a Preemptive Scheduling Algorithm t
3 min read
Difference between EDF and LST CPU scheduling algorithms
1. Earliest Deadline First (EDF) : In Earliest Deadline First scheduling algorithm, at every scheduling point the task having the shortest deadline is scheduled for the execution. It is an optimal dynamic priority-driven scheduling algorithm used in real-time systems. It uses priorities of the tasks for scheduling. In EDF, priorities to the task ar
4 min read
Difference between LJF and LRJF CPU scheduling algorithms
1. Longest Job First (LJF) : It CPU Scheduling algorithm where the process with the largest burst line is executed first. Once the process enters the ready queue, the process exits only after the completion of execution, therefore it is a non-preemptive process. In case, the burst times of the processes are same, the job with overall lowest time is
2 min read
Time Slicing in CPU scheduling
CPUs kernel doesn't simply distribute the entirety of our PCs' resources to single process or service. CPU is continuously running many processes that are essential for it to operate, so our kernel needs to manage these processes without moment's delay. When program needs to run, process must be created for it. This process needs to have important
3 min read
CPU Scheduling Numerical Questions
1. Find the size of the memory if its address consists of 22 bits. Assume the memory is 2-byte addressable.Solution - If the given address consists of ‘k’ bits, then 2k locations are possible.Size of memory = 2k x Size of one location.According to the question, number of locations with 22 bits = 222 locationsGiven that the size of the memory is 2-b
3 min read
Comparison of Different CPU Scheduling Algorithms in OS
A scheduling algorithm is used to estimate the CPU time required to allocate to the processes and threads. The prime goal of any CPU scheduling algorithm is to keep the CPU as busy as possible for improving CPU utilization. Scheduling Algorithms 1. First Come First Serve(FCFS): As the name implies that the jobs are executed on a first come first se
5 min read
Shortest Job First CPU Scheduling with Predicted Burst Time
Shortest Job First (SJF) is an optimal scheduling algorithm as it gives maximum Throughput and minimum average waiting time(WT) and turnaround time (TAT) but it is not practically implementable because the burst time of a process can't be predicted in advance. We may not know the length of the next CPU burst, but we may be able to predict its value
5 min read
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the advantage of having a minimum average waiting time
13 min read
Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenarios, when processes have different arrival times, are discussed. Given n processes with their burst times and arrival times, the task is to find the average waiting time and an average turn around time using FCFS scheduling algorithm. FIFO simply queu
15+ min read
Program for Priority CPU Scheduling | Set 1
Priority scheduling is one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. The process with the highest priority is to be executed first and so on. Processes with the same priority are executed on a first-come first served basis. Priority can be decided based on memory requirements, time requirements
11 min read
Multilevel Queue (MLQ) CPU Scheduling
It may happen that processes in the ready queue can be divided into different classes where each class has its own scheduling needs. For example, a common division is a foreground (interactive) process and a background (batch) process. These two classes have different scheduling needs. For this kind of situation, Multilevel Queue Scheduling is used
6 min read
Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.   Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.It is a Greedy Algorithm.It may cause starvation i
15+ min read
Selfish Round Robin CPU Scheduling
Prerequisite - Program for Round Robin scheduling  In the traditional Round Robin scheduling algorithm, all processes were treated equally for processing. The objective of the Selfish Round Robin is to give better service to processes that have been executing for a while than to newcomers. It's a more logical and superior implementation compared to
5 min read
Article Tags :