# Longest Remaining Time First (LRTF) CPU Scheduling Program

The Longest Remaining Time First (LRTF) scheduling algorithm is a variant of the Longest Job First (LJF) algorithm and is used by the operating system to schedule incoming processes. In LRTF, the process with the highest remaining execution time is given the highest priority and scheduled to be executed first.

## Algorithm

• Create a structure of process containing all necessary fields like AT (Arrival Time), BT(Burst Time), CT(Completion Time), TAT(Turn Around Time), and WT(Waiting Time).

• Sort the processes according to their Arrival Time (AT).

• Find the process with the largest Burst Time (BT) and execute it for one unit of time. Increment the Total Time by 1 and decrement the BT of the executing process by 1.

• When the BT of any process becomes zero, update the CT (Completion Time) of that process to the current Total Time

• After calculating the CT for each process, calculate the TAT (Turn Around Time) and WT (Waiting Time) for each process using the following formulas −

TAT = CT - AT

WT = TAT - BT

### Example of calculating turn around time and waiting time using Gantt chart

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

Process

Arrival time

Burst Time

P1

1 ms

2 ms

P2

2 ms

4 ms

P3

3 ms

6 ms

P4

4 ms

8 ms

In this problem, we are provided with a set of processes and their arrival time and burst time. Our task is determining the completion time, turn around time, and waiting time for each process. Once we have calculated these values for all processes, we can find the average turn around time and average waiting time for the entire set of processes.

Now, let's create the Gantt chart of the above example based on the Longest Remaining Time First Scheduling

After execution of all processes

Pno

AT

BT

CT

TAT

WT

1

1

2

18

17

15

2

2

4

19

17

13

3

3

6

20

17

11

4

4

8

21

17

9

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)

After calculating the turn around time and waiting time for all processes, we can calculate their total and average values as follows:

### Output

Total Turn Around Time = 2+5+9+52 = 68ms
Average Turn Around Time = 68/4 = 17.00ms

Total Waiting Time = 0+1+3+44 = 48ms
Average Waiting Time = 48/4 = 12.00ms


### Example

#include <bits/stdc++.h>
using namespace std;
// creating a structure of a process
struct process {
int process no;
int AT;
int BT;

// for backup purpose to print in last
int BT backup;
int WT;
int TAT;
int CT;
};
// creating a structure of 4 processes
struct process p[4];

// variable to find the total time
int total time = 0;
int prefinal total = 0;

// comparator function for sort()
bool compare(process p1, process p2)
{
// compare the Arrival time of two processes
return p1.AT < p2.AT;
}
// finding the largest Arrival Time among all the available
/ process at that time
int find largest(int at)
{
int max = 0, i;
for (i = 0; i < 4; i++) {
if (p[i].AT <= at) {
if (p[i].BT > p[max].BT)
max = i;
}
}
// returning the index of the process having the largest BT
return max;
}
// function to find the completion time of each process
int find CT() {
int index;
int flag = 0;
int i = p[0].AT;
while (1) {
if (i <= 4) {
index = find largest(i);
}
else
index = find largest(4);
cout << "Process executing at time " << total time
<< " is: P" << index + 1 << "\t";
p[index].BT -= 1;
total time += 1;
i++;
if (p[index].BT == 0) {
p[index].CT = total time;
cout << " Process P" << p[index].process no
<< " is completed at " << total time;
}
cout << end l;
// loop termination condition
if (total time == prefinal total)
break;
}
}
int main()
{
int i;
// initializing the process number
for (i = 0; i < 4; i++) {
p[i].process no = i + 1;
}
// cout<<"arrival time of 4 processes : ";
for (i = 0; i < 4; i++) // taking AT
{
p[i].AT = i + 1;
}
// cout<<" Burst time of 4 processes : ";
for (i = 0; i < 4; i++) {
// assigning {2, 4, 6, 8} as Burst Time to the processes
// backup for displaying the output in last
// calculating total required time for terminating
// the function().
p[i].BT = 2 * (i + 1);
p[i].BT backup = p[i].BT;
pre final total += p[i].BT;
}
// displaying the process before executing
cout << "PNo\tAT\tBT";
for (i = 0; i < 4; i++) {
cout << p[i].process no << "\t";
cout << p[i].AT << "\t";
cout << p[i].BT << "\t";
cout << end l;
}
cout << end l;
// sorting process according to Arrival Time
sort(p, p + 4, compare);
// calculating initial time when execution starts
total time += p[0].AT;
// calculating to terminate loop
prefinal total += p[0].AT;
find CT();
int total WT = 0;
int total TAT = 0;
for (i = 0; i < 4; i++) {
// since, TAT = CT - AT
p[i].TAT = p[i].CT - p[i].AT;
p[i].WT = p[i].TAT - p[i].BTbackup;
// finding total waiting time
total WT += p[i].WT;
// finding total turn around time
total TAT += p[i].TAT;
}
cout << "After execution of all processes ... ";
// after all process executes
cout << "PNo\tAT\tBT\tCT\tTAT\tWT";
for (i = 0; i < 4; i++) {
cout << p[i].processno << "\t";
cout << p[i].AT << "\t";
cout << p[i].BTbackup << "\t";
cout << p[i].CT << "\t";
cout << p[i].TAT << "\t";
cout << p[i].WT << "\t";
cout << endl;
}
cout << endl;
cout << "Total TAT = " << totalTAT << endl;
cout << "Average TAT = " << totalTAT / 4.0 << endl;
cout << "Total WT = " << totalWT << endl;
cout << "Average WT = " << totalWT / 4.0 << endl;
return 0;
}


### Output

PNo AT BT
1 1 2
2 2 4
3 3 6
4 4 8
Process executing at time 1 is: P1
Process executing at time 2 is: P2
Process executing at time 3 is: P3
Process executing at time 4 is: P4
Process executing at time 5 is: P4
Process executing at time 6 is: P4
Process executing at time 7 is: P3
Process executing at time 8 is: P4
Process executing at time 9 is: P3
Process executing at time 10 is: P4
Process executing at time 11 is: P2
Process executing at time 12 is: P3
Process executing at time 13 is: P4
Process executing at time 14 is: P2
Process executing at time 15 is: P3
Process executing at time 16 is: P4
Process executing at time 17 is: P1
Process executing at time 18 is: P2
Process executing at time 19 is: P3
Process executing at time 20 is: P4
After execution of all processes ...
Process P1 is completed at 18
Process P2 is completed at 19
Process P3 is completed at 20
Process P4 is completed at 21
PNo AT BT CT TAT WT
1 1 2 18 17 15
2 2 4 19 17 13
3 3 6 20 17 11
4 4 8 21 17 9
Total Turn Around Time = 2+5+9+52 = 68ms
Average Turn Around Time = 68/4 = 17.00ms
Total Waiting Time = 0+1+3+44 = 48ms
Average Waiting Time = 48/4 = 12.00ms


## Conclusion

To conclude, we have discussed the Longest Remaining Time First (LRTF) scheduling algorithm, which is a variant of the Longest Job First (LJF) algorithm. LRTF is used by the operating system to schedule incoming processes. We have also gone through an example of how to calculate the completion time, turnaround time, and waiting time for a set of processes using the LRTF algorithm. Finally, we have provided a C++ program that implements the LRTF algorithm and calculates the completion time, turnaround time, and waiting time for a set of processes. Overall, LRTF is an effective algorithm that can be used to improve the performance of an operating system.

Updated on: 25-Jul-2023

157 Views

##### Kickstart Your Career

Get certified by completing the course