Open In App

Longest Job First (LJF) CPU Scheduling Algorithm

Longest Job First (LJF) is a non-preemptive scheduling algorithm. This algorithm is based on the burst time of the processes. The processes are put into the ready queue based on their burst times i.e., in descending order of the burst times. As the name suggests this algorithm is based on the fact that the process with the largest burst time is processed first. The burst time of only those processes is considered that have arrived in the system until that time. Its preemptive version is called Longest Remaining Time First (LRTF) algorithm

Prerequisite: Process Management | CPU Scheduling 

 

Characteristics of Longest Job First(Non-Preemptive)

Advantages of Longest Job First(LJF)

Disadvantages of Longest Job First CPU Scheduling Algorithm

Longest Job First CPU Scheduling Algorithm

Let us consider the following examples. 



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 Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned below:



At time = 1, Available Process : P1. So, select P1 and start executing.

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 P2 1ms 1ms 0ms
P2 2ms 0ms 4ms 4ms

At time = 3, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
3-4ms P2 2ms P2 0ms 4ms 4ms
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 P2 2ms P2, P4 0ms 4ms 4ms
P3 3ms 1ms 5ms 4ms
P4 4ms 0ms 8ms 8ms

At time = 5,

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

At time = 9, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
9-17ms P2 2ms P2 0ms 4ms 4ms
P4 4ms 8ms 8ms 0ms

At time = 17,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
17-21ms P2 2ms   4ms 4ms 0ms

At time = 21,

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 P2 1ms 1ms 0ms
P2 2ms 0ms 4ms 4ms
3-4ms P2 2ms P2 0ms 4ms 4ms
P3 3ms 1ms 6ms 5ms
4-5ms P2 2ms P2, P4 0ms 4ms 4ms
P3 3ms 1ms 5ms 4ms
P4 4ms 0ms 8ms 8ms
5-9ms P2 2ms P2, P4 0ms 4ms 4ms
P3 3ms 4ms 4ms 0ms
P4 4ms 0ms 8ms 8ms
9-17ms P2 2ms P2 0ms 4ms 4ms
P4 4ms 8ms 8ms 0ms
17-21ms P2 2ms   4ms 4ms 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,  

Output :  

Total Turn Around Time = 40 ms
So, Average Turn Around Time = 40/4 = 10.00 ms

And, Total Waiting Time = 20 ms
So, Average Waiting Time = 20/4 = 5.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

Gantt chart for this example:

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, the final table looks like:

P.No Arrival Time (AT) Completion Time (CT) Burst Time (BT) Turn Around (TAT) Waiting Time (WT)
P1 0 14 2 14 12
P2 0 3 3 3 0
P3 2 16 2 14 12
P4 3 8 5 5 0
P5 4 12 4 8 4

Output :  

Total Turn Around Time = 44 ms
So, Average Turn Around Time = 44/5 = 8.8 ms

And, Total Waiting Time = 28 ms
So, Average Waiting Time = 28/5 = 5.6 ms 


Article Tags :