SJF (algorithm)

Example of Non-Preemptive SJF

Given Processes:

ProcessArrival TimeBurst Time
P107
P224
P341
P454

Step 1: Order of Execution

  • At time t=0, only P1 is available, so it starts execution.
  • At time t=2, P2 arrives, but P1 continues execution because this is non-preemptive SJF.
  • At time t=4, P3 arrives, and after P1 completes, P3 (shortest burst time) is executed.
  • At time t=5, P4 arrives, and P2 is scheduled next after P3.

Gantt Chart:

Time0 – 77 – 88 – 1212 – 16
PP1P3P2P4

Step 2: Calculations

Turnaround Time (TAT) = Completion Time – Arrival Time
Waiting Time (WT) = Turnaround Time – Burst Time

ProcessArrival TimeBurst TimeCompletion TimeTATWT
P107770
P22412106
P341843
P45416117

Step 3: Averages

  • Average Turnaround Time (TAT) = 7+10+4+114=8\frac{7 + 10 + 4 + 11}{4} = 847+10+4+11​=8
  • Average Waiting Time (WT) = 0+6+3+74=4\frac{0 + 6 + 3 + 7}{4} = 440+6+3+7​=4

Advantages of SJF

  1. Minimizes average waiting time.
  2. Optimal for systems where short tasks dominate.

Disadvantages of SJF

  1. Difficult to predict the exact burst time of a process.
  2. Starvation of longer processes if shorter ones keep arriving.

Example -2:Below is the full solution for SJF (Shortest Job First) Non-Preemptive scheduling for the given processes.


Given Processes

ProcessArrival TimeBurst Time
P105
P223
P332
P413

SJF (Non-Preemptive) Scheduling

Step 1: Sort by arrival, choose shortest burst among available processes

Timeline Execution

At time = 0

Available: P1
→ Run P1 (burst 5) until completion (non-preemptive)

P1 runs from 0 → 5

At time = 5

Now available: P2 (3), P3 (2), P4 (3)
Shortest = P3

P3 runs from 5 → 7

At time = 7

Remaining: P2 (3), P4 (3)
Tie → choose the one that arrived first → P4

P4 runs from 7 → 10

At time = 10

Last: P2 (3)

P2 runs from 10 → 13


Gantt Chart

|  P1  | P3 |  P4  |  P2  |
0      5    7     10     13

Completion, Turnaround, and Waiting Times

ProcessATBTCTTAT = CT-ATWT = TAT-BT
P105550
P332742
P4131096
P22313118

Average Times

Average Turnaround Time

5+4+9+11/=4 29/4 ​

=7.25 ms

Average Waiting Time

0+2+6+8/4=16/4= 4 ms


Final Answer Summary

Gantt Chart:

0–5(P1), 5–7(P3), 7–10(P4), 10–13(P2)

Average Waiting Time = 4 ms

Average Turnaround Time = 7.25 ms

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *