Bird
Raised Fist0
Interview Prepoperating-systemseasyAmazonGoogleSwiggyPhonePe

Context Switch - Cost & Causes

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 Ready Queue

The scheduler initializes three processes P1, P2, and P3 with their CPU burst times and priorities. All processes start in the ready state, waiting to be scheduled.

💡 Initialization sets up the environment so we can observe how the scheduler manages multiple processes and triggers context switches.
Line:processes = [P1, P2, P3] ready_queue = [P1, P2, P3] active_pid = null
💡 The scheduler starts with all processes ready but none running yet.
📊
Context Switch - Cost & Causes - Watch the Algorithm Execute, Step by Step
Watching this step-by-step helps you understand how and why context switches happen, what triggers them, and how they impact CPU utilization.
Step 1/12
·Active fillAnswer cell
P1
ready
burst: 5
P2
ready
burst: 3
P3
ready
burst: 4
Ready Queue
P1P2P3
Waiting Queue
empty
🖥CPUidlet=0
Transition readyrunning pid:P1 - highest priority selected
P1
running
burst: 5
P2
ready
burst: 3
P3
ready
burst: 4
Ready Queue
P2P3
Waiting Queue
empty
🖥CPUP1t=0
P1
running
burst: 3
P2
ready
burst: 3
P3
ready
burst: 4
Ready Queue
P2P3
Waiting Queue
empty
🖥CPUP1t=2
P1
Transition runningready pid:P1 - preempted by higher priority P2
P1
ready
burst: 3
P2
running
burst: 3
P3
ready
burst: 4
Ready Queue
P1P3
Waiting Queue
empty
🖥CPUP2t=2
P1
P1
ready
burst: 3
P2
running
burst: 3
P3
ready
burst: 4
Ready Queue
P1P3
Waiting Queue
empty
🖥CPUP2t=3
P1
idle
P1
ready
burst: 3
P2
terminated
burst: 0
P3
ready
burst: 4
Ready Queue
P1P3
Waiting Queue
empty
🖥CPUP2t=6
P1
idle
P2
Transition readyrunning pid:P1 - next highest priority
P1
running
burst: 3
P2
terminated
burst: 0
P3
ready
burst: 4
Ready Queue
P3
Waiting Queue
empty
🖥CPUP1t=6
P1
idle
P2
P1
running
burst: 3
P2
terminated
burst: 0
P3
ready
burst: 4
Ready Queue
P3
Waiting Queue
empty
🖥CPUP1t=7
P1
idle
P2
idle
P1
terminated
burst: 0
P2
terminated
burst: 0
P3
ready
burst: 4
Ready Queue
P3
Waiting Queue
empty
🖥CPUP1t=10
P1
idle
P2
idle
P1
Transition readyrunning pid:P3 - last process
P1
terminated
burst: 0
P2
terminated
burst: 0
P3
running
burst: 4
Ready Queue
empty
Waiting Queue
empty
🖥CPUP3t=10
P1
idle
P2
idle
P1
P1
terminated
burst: 0
P2
terminated
burst: 0
P3
running
burst: 4
Ready Queue
empty
Waiting Queue
empty
🖥CPUP3t=11
P1
idle
P2
idle
P1
idle
P1
terminated
burst: 0
P2
terminated
burst: 0
P3
terminated
burst: 0
Ready Queue
empty
Waiting Queue
empty
🖥CPUP3t=15
P1
idle
P2
idle
P1
idle
P3

Key Takeaways

Context switches occur when a higher priority process arrives or a running process completes.

This is hard to see from code alone because the timing and priority changes are dynamic and interleaved.

Each context switch incurs a fixed overhead time during which no process runs, reducing CPU efficiency.

Visualizing the idle time during switches makes the cost of context switching concrete.

Processes can be preempted and later resumed, showing how the scheduler manages multiple processes fairly.

Seeing the burst times decrease and states change stepwise clarifies preemption mechanics.

Practice

(1/5)
1. Which of the following statements about the 'Mutual Exclusion' condition in deadlock is INCORRECT?
medium
A. Mutual exclusion means that at least one resource must be held in a non-shareable mode.
B. Mutual exclusion can be eliminated for all resources to prevent deadlock.
C. Mutual exclusion is necessary for deadlock but not sufficient alone.
D. Mutual exclusion applies only to resources that cannot be simultaneously used by multiple processes.

Solution

  1. Step 1: Understand mutual exclusion

    It requires that some resources be non-shareable, meaning only one process can use them at a time.
  2. Step 2: Analyze why eliminating mutual exclusion for all resources is impractical

    Eliminating mutual exclusion for all resources is impossible because some resources (like printers) inherently cannot be shared.
  3. Step 3: Evaluate other options

    Mutual exclusion means that at least one resource must be held in a non-shareable mode correctly defines mutual exclusion. Mutual exclusion is necessary for deadlock but not sufficient alone correctly states it is necessary but not sufficient. Mutual exclusion applies only to resources that cannot be simultaneously used by multiple processes correctly limits mutual exclusion to non-shareable resources.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Mutual exclusion cannot be eliminated for all resources; some must be exclusive.
Hint: Mutual exclusion is about non-shareable resources, which can't all be made shareable [OK]
Common Mistakes:
  • Thinking mutual exclusion can be removed entirely
  • Confusing necessity with sufficiency
  • Misapplying mutual exclusion to shareable resources
2. Which of the following statements about the convoy effect in FCFS scheduling is INCORRECT?
medium
A. The convoy effect occurs when a long process delays all subsequent shorter processes
B. The convoy effect can be mitigated by introducing preemption in the scheduling algorithm
C. The convoy effect causes the average waiting time to always be minimal in FCFS
D. The convoy effect leads to poor CPU utilization and increased waiting times

Solution

  1. Step 1: Analyze each statement

    A is correct; long processes delaying short ones is the convoy effect.
    B is correct; preemption can reduce the convoy effect.
    C is incorrect; the convoy effect increases average waiting time, not minimizes it.
    D is correct; convoy effect can cause poor CPU utilization and longer waits.
  2. Step 2: Identify the incorrect statement

    Only The convoy effect causes the average waiting time to always be minimal in FCFS falsely claims minimal average waiting time due to the convoy effect.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Convoy effect worsens waiting time, not improves it.
Hint: Convoy effect -> longer waits, not shorter
Common Mistakes:
  • Assuming convoy effect improves waiting time
  • Ignoring preemption as a mitigation
  • Misunderstanding convoy effect impact on CPU utilization
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. If a process requests resources that would keep the system in a safe state but the system is currently in an unsafe state, what does the Banker's Algorithm do and why?
hard
A. It denies the request because the system must always remain in a safe state, and starting from an unsafe state invalidates the algorithm's assumptions.
B. It restarts the system to reset resource allocations and ensure safety.
C. It preempts resources from other processes to restore a safe state before granting the request.
D. It grants the request because the immediate allocation is safe, ignoring the current unsafe state.

Solution

  1. Step 1: Recall Banker's Algorithm assumptions

    The algorithm assumes the system starts in a safe state to guarantee deadlock avoidance.
  2. Step 2: Analyze the scenario

    If the system is already unsafe, granting requests--even if individually safe--cannot guarantee overall safety.
  3. Step 3: Evaluate options

    A ignores the unsafe starting state.
    B and C describe actions outside the algorithm's scope.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Banker's Algorithm cannot recover from unsafe states; it only avoids entering them.
Hint: Banker's Algorithm requires starting safe state to function correctly [OK]
Common Mistakes:
  • Assuming safe requests can fix unsafe states
  • Believing the algorithm preempts resources
  • Thinking system restarts are part of the algorithm
5. If a system uses preemptive SJF scheduling but the burst times of processes are not known in advance and must be estimated, which challenge is most likely to affect scheduling accuracy?
hard
A. Incorrect burst time estimates can cause frequent unnecessary preemptions
B. The scheduler will always pick the wrong process due to estimation errors
C. Non-preemptive scheduling must be used instead to avoid errors
D. Starvation is eliminated because estimates prevent preemption

Solution

  1. Step 1: Understand burst time estimation impact

    Estimations can be inaccurate, causing the scheduler to preempt based on wrong assumptions.
  2. Step 2: Analyze consequences

    Frequent unnecessary preemptions increase overhead and reduce efficiency.
  3. Step 3: Why other options are incorrect

    B: Scheduler won't always pick wrong process; estimates can be close.
    C: Non-preemptive scheduling is a design choice, not forced by estimation.
    D: Starvation is not eliminated by estimates; it depends on scheduling policy.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Estimation errors cause scheduling inefficiency via unnecessary preemptions.
Hint: Bad burst time estimates cause too many preemptions in preemptive SJF [OK]
Common Mistakes:
  • Assuming estimates always cause wrong scheduling
  • Believing estimation removes starvation