Bird
Raised Fist0
Interview Prepoperating-systemsmediumGoogleAmazonSwiggy

Shortest Job First (SJF) - Preemptive vs Non-Preemptive

Choose your preparation mode3 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Initialize variables and start scheduling

Initialize remaining times, counters, and time to start scheduling from time 0.

💡 Setting up these variables is essential to track progress and remaining burst times for each process.
Line:n = len(processes) remaining_time = [bt for _, bt in processes] complete = 0 current_time = 0
💡 We now have the initial state with all processes' burst times ready to be decremented as they run.
📊
Shortest Job First (SJF) - Preemptive vs Non-Preemptive - Watch the Algorithm Execute, Step by Step
Watching each scheduling decision and time increment reveals how the algorithm dynamically chooses processes, which is hard to grasp from code alone.
Step 1/20
·Active fillAnswer cell
Transition initready0 - start scheduling
0
ready
burst: 8
1
new
burst: 4
2
new
burst: 9
3
new
burst: 5
Ready Queue
empty
Waiting Queue
empty
🖥CPUidlet=0
Transition readyrunning0 - selected shortest job
0
running
burst: 8
1
new
burst: 4
2
new
burst: 9
3
new
burst: 5
Ready Queue
empty
Waiting Queue
empty
🖥CPU0t=0
Transition runningrunning0 - executed 1 time unit
0
running
burst: 7
1
ready
burst: 4
2
new
burst: 9
3
new
burst: 5
Ready Queue
1
Waiting Queue
empty
🖥CPU0t=1
idle
Transition runningrunning pid:1 - preempted by shorter job
0
ready
burst: 7
1
running
burst: 4
2
new
burst: 9
3
new
burst: 5
Ready Queue
0
Waiting Queue
empty
🖥CPU1t=1
idle
Transition runningrunning pid:1 - executed 1 time unit
0
ready
burst: 7
1
running
burst: 3
2
ready
burst: 9
3
new
burst: 5
Ready Queue
02
Waiting Queue
empty
🖥CPU1t=2
idle
1
Transition runningrunning pid:1 - continue running shortest job
0
ready
burst: 7
1
running
burst: 3
2
ready
burst: 9
3
ready
burst: 5
Ready Queue
023
Waiting Queue
empty
🖥CPU1t=2
idle
1
Transition runningrunning pid:1 - executed 1 time unit
0
ready
burst: 7
1
running
burst: 2
2
ready
burst: 9
3
ready
burst: 5
Ready Queue
023
Waiting Queue
empty
🖥CPU1t=3
idle
1
Transition runningrunning pid:1 - continue running shortest job
0
ready
burst: 7
1
running
burst: 2
2
ready
burst: 9
3
ready
burst: 5
Ready Queue
023
Waiting Queue
empty
🖥CPU1t=3
idle
1
Transition runningrunning pid:1 - executed 1 time unit
0
ready
burst: 7
1
running
burst: 1
2
ready
burst: 9
3
ready
burst: 5
Ready Queue
023
Waiting Queue
empty
🖥CPU1t=4
idle
1
Transition runningterminated pid:1 - process completed
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
ready
burst: 5
Ready Queue
023
Waiting Queue
empty
🖥CPU1t=5
idle
1
Transition readyrunning pid:3 - selected shortest job
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 5
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=5
idle
1
Transition runningrunning pid:3 - executed 1 time unit
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 4
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=6
idle
1
3
Transition runningrunning pid:3 - continue running shortest job
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 4
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=6
idle
1
3
Transition runningrunning pid:3 - executed 1 time unit
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 3
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=7
idle
1
3
Transition runningrunning pid:3 - continue running shortest job
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 3
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=7
idle
1
3
Transition runningrunning pid:3 - executed 1 time unit
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
running
burst: 2
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=8
idle
1
3
Transition runningterminated pid:3 - process completed
0
ready
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
terminated
burst: 0
Ready Queue
02
Waiting Queue
empty
🖥CPU3t=10
idle
1
3
Transition readyrunning0 - selected shortest job
0
running
burst: 7
1
terminated
burst: 0
2
ready
burst: 9
3
terminated
burst: 0
Ready Queue
2
Waiting Queue
empty
🖥CPU0t=10
idle
1
3
Transition runningterminated0 - process completed
0
terminated
burst: 0
1
terminated
burst: 0
2
ready
burst: 9
3
terminated
burst: 0
Ready Queue
2
Waiting Queue
empty
🖥CPU0t=17
idle
1
3
idle
Transition runningterminated pid:2 - process completed
0
terminated
burst: 0
1
terminated
burst: 0
2
terminated
burst: 0
3
terminated
burst: 0
Ready Queue
empty
Waiting Queue
empty
🖥CPU2t=26
idle
1
3
idle
2

Key Takeaways

SJF preemptive scheduling always picks the process with the shortest remaining burst time at every time unit.

This dynamic selection is difficult to visualize from code alone but is clear when watching each scheduling decision.

Preemption occurs immediately when a shorter job arrives, interrupting the currently running process.

Seeing the CPU switch processes step-by-step reveals how preemption minimizes waiting time.

Waiting time is calculated precisely at process completion using finish time, burst time, and arrival time.

Understanding waiting time calculation is easier when you see the exact moment a process finishes and how the formula applies.

Practice

(1/5)
1. In which of the following scenarios is the concept of 'Hold and Wait' most likely to cause a deadlock?
easy
A. A process can forcibly preempt resources from other processes.
B. A process requests all required resources at once before execution begins.
C. A process releases all resources before requesting new ones.
D. A process holds one resource and waits to acquire another resource already held by another process.

Solution

  1. Step 1: Understand 'Hold and Wait'

    This condition requires a process to hold at least one resource while waiting to acquire additional resources. A process holds one resource and waits to acquire another resource already held by another process directly describes this scenario.
  2. Step 2: Analyze other options

    A process requests all required resources at once before execution begins avoids hold and wait by requesting all resources upfront. A process releases all resources before requesting new ones releases resources before requesting new ones, preventing hold and wait. A process can forcibly preempt resources from other processes describes preemption, which is a different condition.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Only a process holding one resource and waiting to acquire another resource already held by another process matches the hold and wait condition necessary for deadlock.
Hint: Hold and Wait means holding some resources while waiting for others [OK]
Common Mistakes:
  • Confusing hold and wait with requesting all resources at once
  • Thinking preemption is part of hold and wait
2. In which scenario is First-Come, First-Served (FCFS) scheduling most appropriate to use?
easy
A. When all processes have similar CPU burst times and arrival times
B. When minimizing average waiting time is the highest priority
C. When process arrival times are unpredictable and preemption is required
D. When simplicity and fairness in process order are more important than responsiveness

Solution

  1. Step 1: Understand FCFS characteristics

    FCFS schedules processes in the order they arrive without preemption, making it simple and fair in terms of arrival order.
  2. Step 2: Analyze each option

    A is incorrect because FCFS does not optimize for similar burst times; it just queues by arrival.
    B is incorrect because FCFS can cause long waiting times if a long process arrives first.
    C is incorrect because FCFS is non-preemptive and does not handle unpredictable arrivals well.
    D is correct because FCFS's main advantage is simplicity and fairness in order, not responsiveness or waiting time optimization.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    FCFS is chosen for simplicity and fairness, not for minimizing waiting time or handling preemption.
Hint: FCFS = simple, fair order, not optimized for waiting time
Common Mistakes:
  • Assuming FCFS minimizes average waiting time
  • Confusing FCFS with preemptive scheduling
  • Believing FCFS handles unpredictable arrivals efficiently
3. Which of the following statements best describes a limitation of FCFS scheduling related to waiting time and system responsiveness?
medium
A. FCFS can cause long waiting times for short processes due to the convoy effect
B. FCFS guarantees the shortest average waiting time among all scheduling algorithms
C. FCFS allows preemption to improve responsiveness for interactive processes
D. FCFS scheduling complexity grows exponentially with the number of processes

Solution

  1. Step 1: Evaluate each statement

    A is correct because FCFS can cause long waiting times for short processes due to the convoy effect.
    B is incorrect because FCFS does not guarantee the shortest average waiting time; algorithms like SJF do.
    C is incorrect because FCFS is non-preemptive and does not allow preemption.
    D is incorrect because FCFS scheduling complexity is O(n), simple queue processing.
  2. Step 2: Identify the limitation

    The convoy effect causing long waiting times for short processes is a key limitation.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    FCFS is simple but can cause poor responsiveness due to the convoy effect.
Hint: FCFS = simple but convoy effect hurts short jobs
Common Mistakes:
  • Believing FCFS minimizes average waiting time
  • Confusing FCFS with preemptive algorithms
  • Overestimating FCFS scheduling complexity
4. Which of the following statements about the FIFO page replacement algorithm is INCORRECT?
medium
A. FIFO always evicts the least recently used page
B. FIFO can suffer from Belady's anomaly where increasing frames increases page faults
C. FIFO is simple to implement with a queue data structure
D. FIFO does not consider how frequently or recently a page was accessed

Solution

  1. Step 1: Understand FIFO Eviction Policy

    FIFO evicts the oldest loaded page, not the least recently used.
  2. Step 2: Evaluate Each Option

    FIFO can suffer from Belady's anomaly where increasing frames increases page faults is correct; Belady's anomaly can occur with FIFO.
    FIFO always evicts the least recently used page is incorrect; FIFO does not track recency of use.
    FIFO is simple to implement with a queue data structure is correct; FIFO uses a queue for eviction order.
    FIFO does not consider how frequently or recently a page was accessed is correct; FIFO ignores frequency and recency.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    FIFO evicts by arrival order, not usage recency.
Hint: FIFO evicts oldest loaded page, not least recently used [OK]
Common Mistakes:
  • Confusing FIFO with LRU eviction criteria
  • Assuming FIFO tracks page usage
  • Believing FIFO cannot have anomalies
5. Suppose a multithreaded application crashes due to a segmentation fault. Which of the following scenarios best explains why debugging is more challenging compared to a multi-process application?
hard
A. Because threads share the same memory, a fault in one thread can corrupt shared data, making root cause analysis harder
B. Because each thread has its own memory space, faults are isolated and easier to debug
C. Because thread context switches are slower, the fault is harder to reproduce
D. Because processes share memory, faults propagate easily between them

Solution

  1. Step 1: Understand memory sharing in threads

    Threads share the same address space, so a fault in one thread can corrupt shared data affecting others.
  2. Step 2: Contrast with processes

    Processes have isolated memory, so faults are contained, making debugging easier.
  3. Step 3: Evaluate options

    Because each thread has its own memory space, faults are isolated and easier to debug is false because threads share memory. Because thread context switches are slower, the fault is harder to reproduce is false; thread context switch speed does not affect fault reproducibility. Because processes share memory, faults propagate easily between them is false; processes do not share memory by default.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Shared memory in threads complicates debugging due to data corruption [OK]
Hint: Shared memory means shared bugs [OK]
Common Mistakes:
  • Assuming threads have isolated memory like processes
  • Confusing context switch speed with debugging difficulty
  • Believing processes share memory by default