Skip to content
Related Articles

Related Articles

Longest Job First (LJF) CPU Scheduling Algorithm

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 28 Apr, 2022

Prerequisite – Process Management | CPU Scheduling 

Longest Job First (LJF) is a non-preemptive scheduling algorithm. This algorithm is based upon 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 upon 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

 

Characteristics of Longest Job First(Non-Preemptive)

  • Among all the processes waiting in a waiting queue, CPU is always assigned to the process having largest burst time.
  • If two processes have the same burst time then the tie is broken using FCFS i.e. the process that arrived first is processed first. 
  • LJF CPU Scheduling can be of both preemptive and non-preemptive type.

Advantages of Longest Job First(LJF)

  • No other process can execute until the longest job or process executes completely.
  • All the jobs or processes finishes at the same time approximately.

Disadvantages of Longest Job First CPU Scheduling Algorithm

  • This algorithm gives very high average waiting time and average turn-around time for a given set of processes.
  • This may lead to convoy effect.
  • It may happen that a short process may never get executed and the system keeps on executing the longer processes.
  • It reduces the processing speed and thus reduces the efficiency and utilization of the system.

Longest Job First CPU Scheduling Algorithm

  • Step-1: First, sort the processes in increasing order of their Arrival Time. 
  • Step-2: Choose the process having highest Burst Time among all the processes that have arrived till that time. 
  • Step-3: Then process it for its burst time. Check if any other process arrives until this process completes execution. 
  • Step-4: Repeat the above three steps until all the processes are executed. 

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.  

ProcessesArrival 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 InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
1-2msP11ms 1ms2ms1ms

At time = 2, 

  • Process P2 arrives 
  • As P1 is executing thus, Process P2 will wait in the waiting queue.
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
2-3msP11msP21ms1ms0ms
P22ms0ms4ms4ms

At time = 3, 

  • P1 gets executed, and process P3 arrives
  • Available Process: P2, P3. So, select P3 and execute 6 ms (since B.T(P3)=6 which is higher than B.T(P2) = 4) 
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
3-4msP22msP20ms4ms4ms
P33ms1ms6ms5ms

At time = 4,

  • Process P4 arrives,
  • As P3 is executing thus, Process P4 will wait in the waiting queue
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
4-5msP22msP2, P40ms4ms4ms
P33ms1ms5ms4ms
P44ms0ms8ms8ms

At time = 5,

  • Process P3 is executing and P2 and P4 are in the waiting Table.
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
5-9msP22msP2, P40ms4ms4ms
P33ms4ms4ms0ms
P44ms0ms8ms8ms

At time = 9, 

  • Process P3 completes its execution, 
  • Available Process : P2, P4. So, select P4 and execute 8 ms (since, B.T(P4) = 8, B.T(P2) = 4) 
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
9-17msP22msP20ms4ms4ms
P44ms8ms8ms0ms

At time = 17,

  • Finally execute the process P2 for 4 ms. 
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
17-21msP22ms 4ms4ms0ms

At time = 21,

  • Process P2 will finish its execution.
  • The overall execution of the processes will be as shown below:
Time InstanceProcessArrival TimeWaiting TableExecution TimeInitial Burst TimeRemaining Burst 
Time
1-2msP11ms 1ms2ms1ms
2-3msP11msP21ms1ms0ms
P22ms0ms4ms4ms
3-4msP22msP20ms4ms4ms
P33ms1ms6ms5ms
4-5msP22msP2, P40ms4ms4ms
P33ms1ms5ms4ms
P44ms0ms8ms8ms
5-9msP22msP2, P40ms4ms4ms
P33ms4ms4ms0ms
P44ms0ms8ms8ms
9-17msP22msP20ms4ms4ms
P44ms8ms8ms0ms
17-21msP22ms 4ms4ms0ms

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   
P10ms2ms
P20ms3ms
P32ms2ms
P43ms5ms
P54ms4ms

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, final table look like,  

Output :  

Total Turn Around Time = 40 ms
So, Average Turn Around Time = 40/5 = 8 ms

And, Total Waiting Time = 24 ms
So, Average Waiting Time = 24/5 = 4.8 ms 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!