0
0
Operating Systemsknowledge~10 mins

SJF (Shortest Job First) in Operating Systems - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SJF (Shortest Job First)
Jobs arrive
Check ready queue
Select job with shortest burst time
Execute selected job
Job completes
Remove job from queue
Repeat until no jobs left
SJF schedules jobs by always picking the one with the shortest execution time next, repeating until all jobs finish.
Execution Sample
Operating Systems
Jobs = [("P1", 6), ("P2", 2), ("P3", 8), ("P4", 3)]
Order = []
while Jobs:
  # Pick job with shortest burst
  job = min(Jobs, key=lambda x: x[1])
  # Execute job
  # Remove job
  Jobs.remove(job)
  # Add to Order
  Order.append(job[0])
This simulates picking and running the shortest job from a list until all are done.
Analysis Table
StepReady QueueSelected JobActionOrder of Execution
1P1(6), P2(2), P3(8), P4(3)P2(2)Execute P2P2
2P1(6), P3(8), P4(3)P4(3)Execute P4P2, P4
3P1(6), P3(8)P1(6)Execute P1P2, P4, P1
4P3(8)P3(8)Execute P3P2, P4, P1, P3
5EmptyNoneAll jobs doneP2, P4, P1, P3
💡 All jobs executed; ready queue is empty.
State Tracker
VariableStartAfter 1After 2After 3After 4Final
Ready QueueP1(6), P2(2), P3(8), P4(3)P1(6), P3(8), P4(3)P1(6), P3(8)P3(8)EmptyEmpty
Order[][P2][P2, P4][P2, P4, P1][P2, P4, P1, P3][P2, P4, P1, P3]
Key Insights - 3 Insights
Why does the scheduler pick P2 before P4 even though both are short?
Because P2 has the shortest burst time (2) compared to P4 (3), so it is selected first as shown in step 1 of the execution_table.
What happens if two jobs have the same shortest burst time?
The scheduler picks the one that arrived first or is listed first in the queue. This is not shown here but is a common rule to break ties.
Why is the ready queue smaller after each step?
Because after executing a job, it is removed from the queue, reducing the number of jobs left to schedule, as seen in the variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. Which job is selected and why?
AP4 because it has the shortest burst time left
BP1 because it arrived first
CP3 because it has the longest burst time
DP2 because it is still in the queue
💡 Hint
Check the 'Selected Job' column at Step 2 in the execution_table.
At which step does the ready queue become empty?
AStep 4
BStep 5
CStep 3
DNever
💡 Hint
Look at the 'Ready Queue' column in the execution_table to see when it shows 'Empty'.
If P4 had a burst time of 1 instead of 3, which job would be selected at Step 2?
AP1
BP3
CP4
DP2
💡 Hint
Refer to the selection rule: pick the job with the shortest burst time from the ready queue.
Concept Snapshot
SJF (Shortest Job First) schedules jobs by always running the job with the smallest burst time next.
It reduces average waiting time but can cause longer jobs to wait.
Jobs are selected from the ready queue until all are done.
Tie-breakers usually pick the earliest arrived job.
Non-preemptive SJF runs each job fully once started.
Full Transcript
SJF (Shortest Job First) is a scheduling method where the system picks the job with the shortest execution time from the ready queue to run next. This process repeats until all jobs finish. In the example, jobs P1, P2, P3, and P4 have burst times 6, 2, 8, and 3 respectively. The scheduler picks P2 first because it has the shortest burst time, then P4, then P1, and finally P3. After each job completes, it is removed from the queue, shrinking the ready queue until empty. This method helps reduce average waiting time but may delay longer jobs. If two jobs have the same burst time, the one that arrived first is chosen. The execution table and variable tracker show these steps clearly, helping beginners visualize how SJF works step-by-step.