- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Longest Remaining Time First (LRTF) or Preemptive Longest Job First CPU Scheduling Algorithm
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. At intervals of time, such as every unit of time, the system checks to see if another process with a higher burst time has arrived. If such a process exists, it is scheduled for execution before continuing with the current process. The algorithm is designed to maximize the processor's utilization and minimize the waiting time of processes.
Characteristics of the Longest Remaining Time First (LRTF) Scheduling Algorithm
Longest Remaining Time First (LRTF) is a popular CPU scheduling algorithm used to determine the order in which incoming processes are executed in a systematic way.
The LRTF algorithm follows a preemptive approach, where the CPU is allocated only for a fixed slice of time, and the process with the highest burst size is selected for execution.
The process with the highest burst size is allowed to run for a fixed slice of time, after which the selection process takes place again.
However, LRTF is not an optimal scheduling algorithm due to its high average waiting time.
The LRTF algorithm prioritizes long-running processes, causing shorter processes to wait, resulting in an increased average waiting time.
Advantages
The LRTF algorithm is simple to implement and understand, making it widely used in various operating systems.
The completion time of almost all processes occurs before the longest job reaches completion.
The LRTF algorithm is starvation-free, meaning all processes receive a fair share of the CPU.
Disadvantages
The LRTF algorithm's context switching utilizes CPU time that could be more productively used to run processes.
Lower processes may have to stay for the CPU to finish executing longer processes with larger burst sizes
Despite having a smaller burst time for each process, the LRTF algorithm has a higher average waiting time and average turnaround time.
Longest Remaining Time First (LRTF) CPU scheduling algorithm
Sort the processes by their arrival time, in increasing order.
Choose the process having least arrival time but with the most Burst Time.
Execute the chosen process for one unit of time.
Check if any new process has arrived, and if so, compare its remaining burst time with the current process. If the new process has a longer remaining burst time, preempt the current process and start executing the new process instead.
Repeat steps 2 to 4 until all processes have been executed.
Example of Longest Remaining Time First (LRTF)
First, let's sort the processes based on their arrival time −
Process |
Arrival Time |
Burst Time |
---|---|---|
P1 |
0 |
3 |
P2 |
1 |
6 |
P3 |
3 |
2 |
P4 |
5 |
3 |
Now, let's create the Gantt chart based on the Longest Remaining First Scheduling −
Explanation
At time t=0, the only process available is P1 with a burst time of 3. So, P1 starts executing.
At time t=1, P2 arrives with a burst time of 6, but P1 still has 2 units of the remaining time. But compared to P1, P2 has more units. So, P2 is added to the ready queue.
So, Here P2 is repeated till t=5, after that compared with P1, P2, P3, and P4 have more units. So, P4 is added to the ready queue.
At t = 6 i.e., after P4 gets executed, we repeated the same process until all processes have been executed.
At time t=14, P2 completes its execution, and all processes are completed.
Now, let's calculate the waiting time and turnaround time for each process −
Since, completion time (C.T) can be directly determined by the Gantt chart, and
Turn Around Time (TAT)
= (Completion Time) – (Arrival Time)
Also, Waiting Time (WT)
= (Turn Around Time) – (Burst Time)
Process |
Arrival Time |
Burst Time |
Completion Time |
Turn around Time Turn Around Time = Completion Time – Arrival Time |
Waiting Time Waiting Time = Turn Around Time – Burst Time |
---|---|---|---|---|---|
P1 |
0 |
3 |
11 |
11-0=11 |
11-3=8 |
P2 |
1 |
6 |
12 |
12-1=11 |
11-6=5 |
P3 |
3 |
2 |
13 |
13-2=11 |
13-2=11 |
P4 |
5 |
3 |
14 |
14-5=11 |
11-3=8 |
Output
Total Turn Around Time = 36 ms
So, Average Turn Around Time = 44/4 = 11 ms
And, Total Waiting Time = 18 ms
So, Average Waiting Time = 32/4 = 8 ms
Conclusion
The Longest Remaining Time First (LRTF) scheduling algorithm is commonly used by operating systems to make sure the processor is used efficiently and processes don't have to wait too long. It works by giving priority to the process that has the longest time left to complete. However, this algorithm isn't perfect because smaller processes may have to wait longer for bigger processes to finish. Despite this, the LRTF algorithm is easy to understand and implement, and it usually finishes most processes before the longest one finishes. The LRTF algorithm makes sure that all processes get a fair chance to use the CPU. However, other scheduling algorithms may be better for certain systems, depending on their needs.