Open In App

Longest Remaining Time First (LRTF) or Preemptive Longest Job First CPU Scheduling Algorithm

Longest Remaining Time First (LRTF) is a preemptive version of Longest Job First (LJF) scheduling algorithm. In this scheduling algorithm, we find the process with the maximum remaining time and then process it, i.e. check for the maximum remaining time after some interval of time(say 1 unit each) to check if another process having more Burst Time arrived up to that time. 

Characteristics of Longest Remaining Time First (LRTF) 

Advantages of Longest Remaining Time First (LRTF) 

Disadvantages of Longest Remaining Time First (LRTF) 

Longest Remaining Time First (LRTF) CPU Scheduling Algorithm

Examples to show working of Preemptive Longest Job First CPU Scheduling Algorithm:

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



         

Processes Arrival time  Burst Time
P1  

    1 ms   

  2 ms

P2            

2 ms

 4 ms

P3 

3 ms

6 ms

P4     

  4 ms  

  8 ms  

          
The Longest Remaining Time First CPU Scheduling Algorithm will work on the basis of steps as mentioned below:
 



At time = 1, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
1-2ms P1 1ms   1ms 2ms 1ms

At time = 2, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
2-3ms P1 1ms P1 0ms 1ms 1ms
P2 2ms 1ms 4ms 3ms

At time = 3, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
3-4ms P1 1ms P1, P2 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 6ms 5ms

At time = 4,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
4-5ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 1ms 8ms 7ms

At time = 5,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
5-7ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 5ms 5ms
P4 4ms 2ms 7ms 5ms

At time = 7,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
7-8ms P1 1ms P1, P2, P4 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 5ms 4ms
P4 4ms 0ms 5ms 5ms

At time = 8,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
8-9ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 4ms 4ms
P4 4ms 1ms 5ms 4ms

At time = 9,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
9-10ms P1 1ms P1, P2, P4 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 4ms 3ms
P4 4ms 0ms 4ms 4ms

At time = 10,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
10-11ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 0ms 3ms 3ms
P4 4ms 1ms 4ms 3ms

At time = 11,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
11-12ms P1 1ms P1, P3, P4 0ms 1ms 1ms
P2 2ms 1ms 3ms 2ms
P3 3ms 0ms 3ms 3ms
P4 4ms 0ms 3ms 3ms

At time = 12, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
12-13ms P1 1ms P1, P2, P4 0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
P3 3ms 1ms 3ms 2ms
P4 4ms 0ms 3ms 3ms

At time = 13,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
13-14ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 2ms 2ms
P3 3ms 0ms 2ms 2ms
P4 4ms 1ms 3ms 2ms

At time = 14,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
14-15ms P1 1ms P1, P3, P4 0ms 1ms 1ms
P2 2ms 1ms 2ms 1ms
P3 3ms 0ms 2ms 2ms
P4 4ms 0ms 2ms 2ms

At time = 15,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
15-16ms P1 1ms P1, P2, P4 0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
P3 3ms 1ms 2ms 1ms
P4 4ms 0ms 2ms 2ms

At time = 16,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
16-17ms P1 1ms P1, P2, P3 0ms 1ms 1ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 1ms 2ms 1ms

At time = 17,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
17-18ms P1 1ms P2, P3, P4 1ms 1ms 0ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms

At time = 18,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
18-19ms P2 2ms P3, P4 1ms 1ms 0ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms

At time = 19,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
19-20ms P3 3ms P4 1ms 1ms 0ms
P4 4ms 0ms 1ms 1ms

At time = 20,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
20-21ms P4 4ms   1ms 1ms 0ms

At time = 22,

The overall execution of the processes will be as shown below

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
1-2ms P1 1ms   1ms 2ms 1ms
2-3ms P1 1ms P1 0ms 1ms 1ms
P2 2ms 1ms 4ms 3ms
3-4ms P1 1ms P1, P2 0ms 1ms 1ms
P2 2ms 0ms 3ms 3ms
P3 3ms 1ms 6ms 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ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 1ms 2ms 1ms
17-18ms P1 1ms P2, P3, P4 1ms 1ms 0ms
P2 2ms 0ms 1ms 1ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
18-19ms P2 2ms P3, P4 1ms 1ms 0ms
P3 3ms 0ms 1ms 1ms
P4 4ms 0ms 1ms 1ms
19-20ms P3 3ms P4 1ms 1ms 0ms
P4 4ms 0ms 1ms 1ms
20-21ms P4 4ms   1ms 1ms 0ms

Note: CPU will be idle for 0 to 1 unit time since there is no process available in the given interval. 

Gantt chart will be as following below: 
 

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, 

 

Total Turn Around Time = 68 ms
So, Average Turn Around Time = 68/4 = 17.00 ms

And, Total Waiting Time = 48 ms
So Average Waiting Time = 48/4 = 12.00 ms 

Example-2: 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 2ms
P2 0ms 3ms
P3 2ms 2ms
P4 3ms 5ms
P5 4ms 4ms

Similarly example-1, Gantt chart for this example, 

Since, completion time (CT) 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, 

Total Turn Around Time = 61 ms
So, Average Turn Around Time = 61/5 = 12.20 ms

And, Total Waiting Time = 45 ms
So, Average Waiting Time = 45/5 = 9.00 ms 


Article Tags :