Bird
Raised Fist0
Interview Prepoperating-systemsmediumAmazonGoogleMicrosoftTCSInfosys

Starvation vs Deadlock vs Livelock - Differences & Examples

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 Processes and Resources

Three processes P1, P2, and P3 are created. Two resources R1 and R2 are available but not allocated. All processes start in the ready state.

💡 Setting up the initial state with all processes ready and resources free is essential to observe how resource requests lead to concurrency problems.
Line:processes = ['P1', 'P2', 'P3'] resources = {'R1': None, 'R2': None} process_states = {p: 'ready' for p in processes}
💡 Initial conditions are clear: no resource is held, no process is waiting or blocked.
📊
Starvation vs Deadlock vs Livelock - Differences & Examples - Watch the Algorithm Execute, Step by Step
Watching the processes' states and resource allocations step-by-step reveals the subtle differences between these common concurrency problems that are hard to grasp from code or definitions alone.
Step 1/10
·Active fillAnswer cell
Transition noneready - initialization
P1
ready
P2
ready
P3
ready
Ready Queue
P1P2P3
Waiting Queue
empty
🖥CPUidlet=0
Transition readyrunning pid:P1 - allocated R1
P1
running
P2
ready
P3
ready
Ready Queue
P2P3
Waiting Queue
empty
🖥CPUP1t=1
P1
Transition readyrunning pid:P2 - allocated R2
P1
running
P2
running
P3
ready
Ready Queue
P3
Waiting Queue
empty
🖥CPUP2t=2
P1
P2
Transition readywaiting pid:P3 - R1 held by P1
P1
running
P2
running
P3
waiting
Ready Queue
empty
Waiting Queue
P3 (R1)
🖥CPUP1t=3
P1
P2
P1
Transition runningwaiting pid:P1 - R2 held by P2
P1
waiting
P2
running
P3
waiting
Ready Queue
empty
Waiting Queue
P3 (R1)P1 (R2)
🖥CPUP2t=4
P1
P2
P1
P2
Transition runningwaiting pid:P2 - R1 held by P1
P1
waiting
P2
waiting
P3
waiting
Ready Queue
empty
Waiting Queue
P3 (R1)P1 (R2)P2 (R1)
🖥CPUidlet=5
P1
P2
P1
P2
idle
Transition waitingrunning pid:P3 - attempting livelock resource cycling
P1
waiting
P2
waiting
P3
running
Ready Queue
empty
Waiting Queue
P1 (R2)P2 (R1)
🖥CPUP3t=6
P1
P2
P1
P2
idle
P3
Transition waitingwaiting pid:P1 - starvation due to deadlock
P1
waiting
P2
waiting
P3
running
Ready Queue
empty
Waiting Queue
P1 (R2)P2 (R1)
🖥CPUP3t=7
P1
P2
P1
P2
idle
P3
Transition waitingwaiting pid:system - deadlock detection
P1
waiting
P2
waiting
P3
running
Ready Queue
empty
Waiting Queue
P1 (R2)P2 (R1)
🖥CPUP3t=8
P1
P2
P1
P2
idle
P3
Transition variousfinal - summary of concurrency problems
P1
waiting (starved)
P2
waiting (deadlocked)
P3
running (livelocked)
Ready Queue
empty
Waiting Queue
P1 (R2)P2 (R1)
🖥CPUP3t=9
P1
P2
P1
P2
idle
P3

Key Takeaways

Deadlock occurs when processes form a circular wait, each holding a resource the other needs.

This is hard to see from code alone because it requires visualizing resource dependencies and waiting chains.

Starvation is a form of indefinite waiting caused by unfair resource allocation or scheduling bias.

Understanding starvation requires seeing how some processes never get resources despite availability.

Livelock involves processes actively changing states and resources but making no forward progress.

Livelock is subtle because processes are not blocked but stuck in a loop of futile actions.

Practice

(1/5)
1. 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
2. In which scenario is the Process Control Block (PCB) primarily used during the process state transitions in the five-state model?
easy
A. When a process moves from New to Ready state to store initial process information
B. When a process is terminated to delete all its data from memory
C. When a process is in the Ready state to execute instructions directly
D. When a process moves from Running to Waiting state to save CPU registers and state

Solution

  1. Step 1: Understand the role of PCB during state transitions

    The PCB stores the process state, CPU registers, and scheduling information during context switches.
  2. Step 2: Analyze each option

    A: PCB is created at process creation but its primary use is during context switches, not just at New to Ready.
    B: PCB is involved in termination but mainly to release resources; it is not primarily used to delete data.
    C: Processes in Ready state do not execute instructions directly; they wait for CPU allocation.
    D: Correct. When a process moves from Running to Waiting, the PCB saves CPU registers and state for resumption.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    PCB is essential for saving CPU state during Running to Waiting transitions [OK]
Hint: PCB saves CPU state during Running to Waiting transitions [OK]
Common Mistakes:
  • Thinking PCB is only used at process creation
  • Assuming Ready state processes execute instructions
  • Believing PCB is deleted immediately at termination
3. Which of the following statements about thrashing and the working set model is INCORRECT?
medium
A. Thrashing occurs when the sum of all processes' working sets exceeds total available frames
B. The working set model dynamically adjusts the number of frames allocated to each process based on recent page usage
C. Increasing the total number of processes always reduces thrashing by distributing memory pressure
D. Load control can be used alongside the working set model to prevent thrashing by limiting the number of active processes

Solution

  1. Step 1: Analyze each statement

    A is correct: thrashing happens when total working sets exceed memory.
    B is correct: working set model adjusts frames dynamically.
    C is incorrect: increasing processes usually increases memory pressure, worsening thrashing.
    D is correct: load control limits active processes to prevent thrashing.
  2. Final Answer:

    Option C -> Option C
  3. Quick Check:

    More processes usually increase thrashing risk, not reduce it.
Hint: More processes -> more memory pressure -> more thrashing
Common Mistakes:
  • Believing more processes reduce thrashing
  • Confusing load control with working set adjustments
  • Ignoring total memory constraints
4. Which of the following statements about the trade-offs of increasing TLB size is TRUE?
medium
A. A bigger TLB eliminates the need for page tables
B. Increasing TLB size always reduces effective access time without any downside
C. TLB size does not affect power consumption or chip area significantly
D. A larger TLB may increase lookup time, potentially offsetting hit rate benefits

Solution

  1. Step 1: Understand TLB size impact

    Larger TLB can cache more translations, increasing hit rate.
  2. Step 2: Consider trade-offs

    However, larger TLBs have longer lookup times and consume more power and chip area.
  3. Step 3: Analyze options

    A: Correct, bigger TLBs have diminishing returns and increased latency.
    B: Incorrect, increasing size does not always reduce access time without downsides.
    C: Incorrect, larger TLBs increase power and area.
    D: Incorrect, page tables remain necessary for full address translation.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Trade-offs include lookup latency and hardware cost, not just hit rate.
Hint: Bigger TLB -> better hit rate but slower lookup and more power [OK]
Common Mistakes:
  • Assuming bigger TLB is always better
  • Ignoring hardware cost and latency
5. If a system call is invoked from an interrupt handler already running in kernel mode, what is the expected behavior regarding mode switching and privilege checks?
hard
A. The system call is deferred until the interrupt handler finishes and user mode resumes.
B. The CPU switches to user mode before executing the system call to prevent privilege escalation.
C. The system call executes immediately without a mode switch since already in kernel mode.
D. The system call triggers a nested mode switch to a higher privilege level.

Solution

  1. Step 1: Understand current mode

    Interrupt handlers run in kernel mode with full privileges.
  2. Step 2: System call invoked in kernel mode

    Since already in kernel mode, no mode switch is needed; system call executes directly.
  3. Step 3: Why other options are incorrect

    The CPU switches to user mode before executing the system call to prevent privilege escalation. is incorrect; switching to user mode would reduce privileges improperly. The system call is deferred until the interrupt handler finishes and user mode resumes. is wrong; system calls are not deferred. The system call triggers a nested mode switch to a higher privilege level. is false; kernel mode is the highest privilege level, no nested switch occurs.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    System call in kernel mode -> no mode switch -> immediate execution [OK]
Hint: No mode switch if already in kernel mode
Common Mistakes:
  • Assuming mode switch always occurs on system call
  • Thinking system calls are deferred inside kernel
  • Believing nested privilege levels exist beyond kernel mode