Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Longest Job First (LJF) CPU Scheduling Algorithm
Longest Job First (LJF) is a CPU scheduling algorithm that prioritizes processes based on their burst time. In LJF, the processes with the largest burst time are given priority over the shorter ones. This algorithm works on a non-preemptive basis, meaning once a process is started, it will continue to run until it completes, and no other process can preempt it.
To implement the LJF algorithm, processes are sorted in the ready queue based on their burst times in descending order. The process with the largest burst time among all the processes that have arrived until that time is selected and processed. This process continues until all processes have been executed.
LJF's preemptive version, called Longest Remaining Time First (LRTF), is similar but allows the running process to be preempted if a new process arrives with a larger burst time.
Characteristics of Longest Job First
-
Processes are prioritized based on their burst time, with the longest process getting the highest priority.
-
The running process cannot be preempted by other processes, meaning it will continue to run until it completes its entire burst time.
-
If two processes have the same burst time, the FCFS tie-breaking rule is applied, meaning the process that arrived first will be processed first.
-
LJF can be implemented as both preemptive and non-preemptive scheduling algorithms, but the non-preemptive version ensures that no other process can execute until the longest job completes entirely.
Algorithm Steps
-
Sort the processes in increasing order of their arrival time.
-
Choose the process with the largest burst time among all the processes that have arrived until that time.
-
Execute the selected process for its entire burst time, until completion.
-
Check if any other process arrives while the current process is executing and add them to the ready queue.
-
Repeat steps 2-4 until all processes are executed.
Example
Consider the following processes sorted by arrival time:
| Process | Arrival Time | Burst Time |
|---|---|---|
| P1 | 1 | 2 |
| P2 | 2 | 5 |
| P3 | 3 | 3 |
| P4 | 4 | 8 |
Execution Order
-
At t=1, only P1 is available (burst time: 2ms). P1 executes from t=1 to t=3.
-
At t=3, available processes are P2 (5ms) and P3 (3ms). P2 has longer burst time, so it executes from t=3 to t=8.
-
At t=8, available processes are P3 (3ms) and P4 (8ms). P4 has longer burst time, so it executes from t=8 to t=16.
-
At t=16, only P3 remains and executes from t=16 to t=19.
Note: CPU remains idle from t=0 to t=1 since no process is available.
Performance Calculation
| Process | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time |
|---|---|---|---|---|---|
| P1 | 1 | 2 | 3 | 2 | 0 |
| P2 | 2 | 5 | 8 | 6 | 1 |
| P3 | 3 | 3 | 19 | 16 | 13 |
| P4 | 4 | 8 | 16 | 12 | 4 |
Average Turnaround Time: (2+6+16+12)/4 = 9.0 ms
Average Waiting Time: (0+1+13+4)/4 = 4.5 ms
Advantages
-
Ensures longer processes get executed without starvation from shorter processes.
-
Simple to implement and understand.
Disadvantages
-
Results in high average waiting time and turnaround time, especially for shorter processes.
-
Causes convoy effect where short processes wait unnecessarily for long processes to complete.
-
Short processes may face starvation if longer processes keep arriving.
-
Reduces overall system efficiency and throughput.
Conclusion
The Longest Job First scheduling algorithm prioritizes processes with longer burst times, which can lead to poor performance metrics like high waiting times and convoy effects. While simple to implement, LJF is generally not recommended for practical systems due to its inefficiency in handling shorter processes.
