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

Here are the characteristics of the Longest Job First (LJF) CPU scheduling algorithm, specifically for its non-preemptive version −

  • 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 or process completes entirely.

Advantages

  • The process with the largest burst time is given priority, ensuring that no other process can execute until the longest job or process completes entirely.

  • All processes finish approximately at the same time, which can be beneficial in specific scenarios.

Disadvantages

  • LJF algorithm can result in a high average waiting time and average turn-around time for a given set of processes, which can negatively affect system performance.

  • The convoy effect may occur, which means that short processes have to wait for long processes to complete, causing unnecessary delays.

  • It is possible for a short process to never get executed, and the system keeps executing longer processes, which can reduce system efficiency.

  • LJF reduces processing speed, resulting in lower efficiency and utilization of the system.

Longest Job First CPU Scheduling Algorithm

  • 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.

  • If a new process arrives, compare its burst time with the remaining time of the current process. If the new process has a larger burst time, stop the current process and execute the new process. If the new process has a smaller burst time, put it in the ready queue and continue with the current process until completion.

  • Repeat steps 2-5 until all processes are executed.

Example

First, let's sort the processes based on their arrival time −

Process

Arrival Time

Burst Time

P1

1

2

P2

2

5

P3

3

3

P4

4

8

Now, let's create the Gantt chart based on Longest Job First Scheduling −

Process

P1

P2

P4

P3

Time

3

8

16

19

Note − CPU will be idle for 0 to 1 unit time since there is no process available in the given interval.

Explanation

  • At t=1, the only available process is P1 with a burst time of 2. So, P1 is executed first for 2ms.

  • At t = 3 i.e. after P1 gets executed, The Available Processes are P2, and P3. As you can see the burst time of P2 is more than P3. So, select P2 and execute it for 5ms.

  • At t = 8 i.e. after the execution of P2, the Available Processes are P3 and P4. As you can see the burst time of P4 is more than P3. So, select P4 and execute it for 8ms.

  • At t=16, only P3 is available and it is executed for 3ms.

  • Finally, P4 is executed again for the remaining 4ms

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

1

2

3

3-1=2

2-2=0

P2

2

5

8

8-2=6

6-5=1

P3

3

3

19

19-3=16

16-3=13

P4

4

8

16

16-4=12

12-8=4

Output

Total Turn Around Time = 36 ms

So, Average Turn Around Time = 36/4 = 9.00 ms

And, Total Waiting Time = 18 ms

So, Average Waiting Time = 18/4 = 4.5 ms

Conclusion

The Longest Job First (LJF) CPU scheduling algorithm works by giving the highest priority to the process with the longest burst time. However, LJF has several drawbacks, including the potential for high average waiting time, the possibility of the convoy effect, and reduced system efficiency. While LJF can be implemented as either preemptive or non-preemptive, the non-preemptive version ensures that no other process can execute until the longest job completes entirely. It's important to note that LJF may not be the most efficient scheduling algorithm and may not be suitable for all situations.

Updated on: 04-May-2023

795 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements