Longest Remaining Time First (LRTF) Scheduling with Examples and Programs

Hello Friends! Today, we are going to explain all possible stuffs about what is Longest Remaining Time First scheduling algorithm in OS; involving with many LRTF scheduling examples and programs in C and C++ with their outputs. After reading this article, you will be getting fully understood about Longest Remaining Time First Scheduling with ease.

What is LRTF Scheduling in OS?

Longest Remaining Time First (LRTF) scheduling is a CPU scheduling algorithm used by operating systems to determine which process to run next on a CPU. It is a non preemptive scheduling algorithm, which means that once a process is allocated the CPU, it keeps it until it completes or voluntarily yields it.

LRTF-scheduling

In LRTF scheduling, the process with the longest remaining CPU burst time is given the highest priority and is executed first. The remaining CPU burst time of a process is the amount of time it still needs to run before it completes. So, if a process with a longer remaining burst time arrives while a shorter one is running, then the longer process will be given priority and will be executed first.

The LRTF scheduling algorithm is based on the idea that a process with a longer remaining CPU burst time is likely to take longer to complete, and so giving it priority can lead to shorter overall waiting times and faster completion times for all processes. However, LRTF scheduling may result in some processes with shorter CPU burst times being starved of CPU time if longer processes keep arriving.

‘LRTF Scheduling’ Tutorial Headlines:

In this section, we will show you all headlines about this entire article; you can check them as your choice; below shown all:

  1. What is LRTF Scheduling in OS?
  2. LRTF Scheduling Algorithm Implementation
  3. LRTF Scheduling Example with Gantt Chart
  4. Real-Life Example of LRTF Scheduling
  5. Advantages of LRTF Scheduling
  6. Disadvantages of LRTF Scheduling
  7. Characteristics of Longest Remaining Time First Scheduling
  8. Longest Remaining Time First Scheduling Program in C with Arrival Time
  9. Longest Remaining Time First Scheduling Program in C++

Let’s Get Started!!

LRTF Scheduling Algorithm Implementation

Here, we will try to explain about the working flow chart of the LRTF scheduling algorithm:

Also Read: Highest Response Ratio Next (HRRN) Scheduling with Examples & Programs!!

  1. When a process arrives, the system checks its expected execution time (ET) and compares it with the remaining time of the currently running process.
  2. If the remaining time of the currently running process is greater than the ET of the newly arrived process, the newly arrived process is given priority, and it is scheduled to run next.
  3. If the remaining time of the currently running process is less than or equal to the ET of the newly arrived process, the current process continues to run until completion.
  4. Once the current process completes or is pre-empted, the system checks for the process with the longest remaining time in the ready queue.
  5. The process with the longest remaining time is selected and scheduled to run next.
  6. This process continues until there are no more processes in the ready queue.
  7. If a new process arrives while a process is already running, it is added to the ready queue based on its expected execution time.
  8. The LRTF scheduling algorithm is repeated until all processes are completed.

LRTF Scheduling Example with Gantt Chart

First, let me clarify that there are a few different scheduling algorithms that use the acronym LRTF (Longest Remaining Time First). For this example, I will assume that you are referring to the non-preemptive version of LRTF, which is also sometimes called SJN (Shortest Job Next).

Also Read: Shortest Remaining Time First (SRTF) Scheduling Examples & Programs!!

Here’s an example scenario to work with:

Process    Arrival Time    Burst Time

P1                           0                    6

P2                           1                    4

P3                           2                    8

P4                           3                    3

To apply LRTF scheduling, we start by sorting the processes by their remaining burst time (i.e., the total amount of time they still need to run). For simplicity, let’s assume that all processes arrive at time 0, so we don’t need to worry about arrival time for this example.

After sorting the processes by remaining burst time, we get the following order:

Process      Remaining Burst Time

P3                           8

P1                           6

P2                           4

P4                           3

Since this is a non-preemptive scheduling algorithm, we will run each process to completion before moving on to the next one. So we start with P3, which has the longest remaining burst time.

Here’s a Gantt chart that shows the order in which the processes are run:

Process                P3           P1           P2           P4

Time                      0              8              14           18

So, in this example, the order of execution would be P3, P1, P2, P4. The total completion time for all processes is 18, and the average turnaround time (i.e., the time from arrival to completion) is (8+14+12+15)/4 = 12.25.

Real-Life Example of LRTF Scheduling

One real-life example of LRTF scheduling can be found in a hospital emergency room. When a patient arrives at the emergency room, they are triaged based on the severity of their condition. Patients with life-threatening conditions are given the highest priority and are seen by medical staff first.

Also Read: Multilevel Queue (MLQ) Scheduling with Examples and Programs!!

Suppose there are two patients in the emergency room: Patient A with a severe head injury and Patient B with a minor laceration on their arm. In this scenario, the LRTF scheduling algorithm would prioritize Patient A over Patient B because the former has a longer remaining time to live if not treated immediately.

Another example of LRTF scheduling can be found in a software development project. In this case, the project manager assigns tasks to the development team based on the remaining time required to complete the task. If a task has a longer remaining time, it is given a higher priority than other tasks, and the development team is instructed to work on that task first.

For instance, suppose a software development project has three tasks: Task A, Task B, and Task C. Task A has a remaining time of two weeks, Task B has a remaining time of four weeks, and Task C has a remaining time of six weeks. In this scenario, the LRTF scheduling algorithm would prioritize Task C over Task B and Task A because it has the longest remaining time.

Advantages of LRTF (Longest Remaining Time First) Scheduling

Here, we will show you some advantages of using LRTF are:

Also Read: Multilevel Feedback Queue Scheduling (MLFQ) Algorithm with Examples & Programs!!

Better Response Time: LRTF gives priority to processes with shorter remaining execution time. As a result, processes with smaller burst times are executed first, resulting in faster response times.

Higher Throughput: Since shorter processes are executed first, there is less waiting time for these processes, which means more processes can be completed in a given amount of time, resulting in higher throughput.

Less Average Waiting Time: As shorter processes are executed first, longer processes are left to wait. However, since longer processes require more CPU time, the overall waiting time is reduced.

Fairness: LRTF provides a fair allocation of CPU time among processes, as processes with shorter burst times are given priority, while processes with longer burst times are left to wait.

Reduced Turnaround Time: Turnaround time is the time taken from the submission of a process to its completion. LRTF gives priority to processes with shorter burst times, which means they are executed faster and have a shorter turnaround time.

Dynamic Nature: LRTF is a dynamic algorithm that adjusts to changing conditions. As the remaining execution time of processes changes, the algorithm dynamically reorders the queue and prioritizes processes with the shortest remaining time.

More Efficient Utilization of Resources: Since LRTF prioritizes shorter processes, CPU time is utilized more efficiently. Shorter processes can complete quickly, freeing up the CPU for other processes, which leads to a more efficient use of resources.

Overall, LRTF can lead to better system performance and utilization compared to other scheduling algorithms, especially in situations where there are many short processes.

Disadvantages of LRTF (Longest Remaining Time First) Scheduling

While LRTF has some advantages over other scheduling algorithms, it also has some disadvantages, including:

Also Read: Shortest Job First (SJF) Scheduling with Examples and Programs!!

Starvation: LRTF can cause starvation for short processes as longer processes are given priority. This means that shorter processes may have to wait indefinitely for their turn to execute, which can affect the overall system performance.

Unpredictable Waiting Time: The waiting time for a process under LRTF is highly unpredictable, as it depends on the length of other processes in the queue. This can make it difficult for users to estimate when their processes will complete, which can be a problem in some real-time systems.

Complexity: LRTF requires a significant amount of computation to determine the process with the longest remaining time. This can lead to increased overhead and may not be practical in some systems with limited resources.

Inefficiency: LRTF may not be efficient in systems with a large number of short processes. This is because it may spend a lot of time determining the longest remaining time for each process, resulting in a slower response time for the system.

Low Throughput: LRTF may not be suitable for systems that prioritize high throughput as it may spend too much time on individual processes, which can reduce the overall number of processes completed within a given time frame.

Susceptible to Process Arrival Order: LRTF is highly dependent on the order in which processes arrive. If a long process arrives first, it will receive priority over all other processes, even if they have shorter remaining execution times. This can lead to inefficient utilization of resources and may not be fair to all processes.

Pre-Emption Overhead: LRTF involves frequent pre-emption of processes, which can lead to increased overhead due to the need to save and restore process states. This can slow down the system and reduce overall performance.

Not Suitable for Real-Time Systems: LRTF is not suitable for real-time systems where deadlines need to be met. Since the waiting time for a process is unpredictable, it can be difficult to ensure that processes are completed within their specified deadlines.

Prioritizes Longer Processes: While LRTF can ensure that long processes are completed quickly, it may not be suitable for systems where short processes are critical. Short processes may need to complete quickly to avoid delays in other processes or to meet critical deadlines.

May not be Fair: LRTF may not be fair to all processes as it prioritizes longer processes. This can result in shorter processes being starved of resources and having to wait longer for their turn to execute.

Therefore, LRTF can be an effective scheduling algorithm in some situations, but it may not be suitable for all systems and can have some significant disadvantages.

Characteristics of Longest Remaining Time First Scheduling

Here are some of the characteristics of LRTF:

Also Read: Priority Scheduling Algorithm in OS with Examples & Programs!!

Non-Preemptive: LRTF is a non-preemptive algorithm, which means that once a process is selected to run, it will continue to run until it completes or voluntarily relinquishes the CPU.

Burst Time: LRTF selects the process with the longest remaining burst time to execute next. This means that longer processes are given priority over shorter ones.

Starvation: Shorter processes may suffer from starvation under LRTF, as they may never get a chance to execute if longer processes keep arriving.

Predictability: LRTF is not a predictable algorithm, as it depends on the remaining burst time of the processes in the ready queue.

Response Time: LRTF can have a high response time for shorter processes, as they may have to wait for longer processes to complete before getting a chance to execute.

Throughput: LRTF can have a lower throughput compared to other algorithms, as longer processes may take up more CPU time, leaving less time for other processes to execute.

Overall, LRTF is a useful algorithm in certain situations, such as when longer processes need to be given priority or when there are no real-time constraints on the processes. However, it may not be the best choice in situations where shorter processes need to be executed quickly or when there are real-time constraints.

Longest Remaining Time First Scheduling Program in C with Arrival Time

Here’s an implementation of Longest Remaining Time First (LRTF) scheduling algorithm in C with Arrival Time:

#include<stdio.h>

void main(){

    int n, i, j, k, time = 0, remain, flag = 0, largest;

    float wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], remain_time[10];

    printf(“Enter the number of processes: “);

    scanf(“%d”,&n);

    remain = n;

    for(i=0;i<n;i++){

        printf(“Enter arrival time and burst time for Process %d : “,i+1);

        scanf(“%f”,&arrival_time[i]);

        scanf(“%f”,&burst_time[i]);

        remain_time[i] = burst_time[i];

    }

    printf(“\n\nProcess\t|Turnaround Time| Waiting Time\n\n”);

    remain_time[9] = -9999;

    while(remain!=0){

        largest=9;

        for(i=0;i<n;i++){

            if(arrival_time[i]<=time && remain_time[i]>remain_time[largest] && remain_time[i]>0){

                largest = i;

            }

        }

        if(largest==9){

            time++;

            continue;

        }

        remain_time[largest]–;

        if(remain_time[largest]==0){

            remain–;

            flag=1;

            printf(“\nProcess[%d]\t|\t%.2f\t|\t%.2f”,largest+1,time-arrival_time[largest],time-arrival_time[largest]-burst_time[largest]);

            wait_time += time – arrival_time[largest] – burst_time[largest];

            turnaround_time += time – arrival_time[largest];

        }

        if(flag==0){

            time++;

        }

        flag=0;

    }

    printf(“\n\nAverage waiting time = %f\n”,wait_time*1.0/n);

    printf(“Average Turnaround time = %f\n”,turnaround_time*1.0/n);

}

The above code takes the number of processes, arrival time, and burst time as inputs. It then calculates the waiting time and turnaround time for each process using LRTF scheduling algorithm with arrival time. Finally, it outputs the average waiting time and turnaround time.

Note that in the code above, the arrival time is stored in the arrival_time array, and the burst time is stored in the burst_time array. The remaining time for each process is stored in the remain_time array.

Also, the largest variable stores the index of the process with the largest remaining time. The flag variable is used to check if the process has completed or not. The wait_time and turnaround_time variables store the total waiting time and turnaround time of all the processes. The time variable keeps track of the current time. The remain variable stores the number of remaining processes.

Finally, the output displays the turnaround time and waiting time for each process and the average turnaround time and waiting time for all processes.

Longest Remaining Time First Scheduling Program in C++

Here’s an example program for Longest Remaining Time First (LRTF) scheduling algorithm in C++:

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

struct process {

    int id;

    int burst_time;

    int remaining_time;

};

bool cmp(process a, process b) {

    return a.remaining_time > b.remaining_time;

}

int main() {

    int n;

    cout << “Enter the number of processes: “;

    cin >> n;

    vector<process> processes(n);

    for (int i = 0; i < n; i++) {

        cout << “Enter the burst time for process ” << i+1 << “: “;

        cin >> processes[i].burst_time;

        processes[i].remaining_time = processes[i].burst_time;

        processes[i].id = i+1;

    }

    sort(processes.begin(), processes.end(), cmp);

    int current_time = 0, total_wait_time = 0, total_turnaround_time = 0;

    cout << “\nGantt chart:\n|”;

    while (!processes.empty()) {

        process current_process = processes.back();

        processes.pop_back();

        cout << ” P” << current_process.id << ” |”;

        total_wait_time += current_time;

        total_turnaround_time += current_time + current_process.burst_time;

        current_time += current_process.burst_time;

        while (!processes.empty() && processes.back().burst_time <= current_time) {

            current_time += processes.back().burst_time;

            total_wait_time += current_time – processes.back().burst_time;

            total_turnaround_time += current_time;

            processes.pop_back();

        }

        if (!processes.empty()) {

            processes.back().remaining_time -= current_time;

            if (processes.back().remaining_time < current_process.remaining_time) {

                swap(processes.back(), current_process);

            }

        }

    }

    cout << endl;

    double average_wait_time = (double)total_wait_time / n;

    double average_turnaround_time = (double)total_turnaround_time / n;

    cout << “\nAverage wait time: ” << average_wait_time << endl;

    cout << “Average turnaround time: ” << average_turnaround_time << endl;

    return 0;

}

In this program, we define a process struct to hold the information about each process, including its ID, burst time, and remaining time. We also define a comparison function cmp to sort the processes in decreasing order of their remaining time.

The main function first reads in the number of processes and their burst times from the user, and initializes their remaining times to be the same as their burst times. It then sorts the processes using cmp.

The program then enters a loop that repeatedly selects the process with the highest remaining time, runs it until completion, and updates the remaining times of the other processes. This loop also prints out a Gantt chart of the schedule, and calculates the total wait time and turnaround time.

Finally, the program calculates and prints out the average wait time and turnaround time.

Final Remarks

Through this article, you have been fully learnt about what is Longest Remaining Time First scheduling algorithm in OS; involving with many LRTF scheduling examples and programs in C and C++ with their outputs. If this post is helpful for you, then please share it along with your friends, family members or relatives over social media platforms like as Facebook, Instagram, Linked In, Twitter, and more.

Also Read: Round Robin Scheduling Algorithm with Examples and Programs!!

If you have any experience, tips, tricks, or query regarding this issue? You can drop a comment!

Happy Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *