Bird
Raised Fist0
Interview Prepoperating-systemseasyAmazonGoogleSwiggyPhonePe

Context Switch - Cost & Causes

Choose your preparation mode3 modes available
▶
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 fill★Answer cell
P1
ready
burst: 5
P2
ready
burst: 3
P3
ready
burst: 4
Ready Queue
P1P2P3
Waiting Queue
empty
🖥CPUidlet=0
Transition ready → running 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 running → ready 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 ready → running 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 ready → running 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. Given a disk with pending requests at tracks 10, 22, 20, 2, and 40, and the disk head currently at track 15 moving towards higher track numbers, trace the order in which SCAN will service these requests.
easy
A. 22, 20, 10, 2, 40
B. 20, 22, 40, 2, 10
C. 20, 22, 40, 10, 2
D. 10, 20, 22, 40, 2

Solution

  1. Step 1: Identify head direction and requests

    Head at 15 moving up; requests: 2, 10, 20, 22, 40.
  2. Step 2: SCAN moves towards higher tracks first

    It services requests in ascending order from current position: 20, 22, 40.
  3. Step 3: After reaching the highest request, SCAN reverses direction

    Then it services remaining requests in descending order: 10, 2.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    20, 22, 40, 10, 2 correctly reflects SCAN's elevator movement servicing requests in one direction then reversing.
Hint: SCAN goes up servicing requests, then reverses down
Common Mistakes:
  • Assuming SCAN skips requests on the return trip
  • Mixing up order of requests on the return trip
  • Confusing SCAN with C-SCAN which only moves in one direction
2. Which of the following statements about memory compaction is INCORRECT?
medium
A. Compaction can be performed without halting all running processes
B. Compaction does not affect internal fragmentation
C. Compaction requires additional CPU overhead and may cause temporary performance degradation
D. Compaction reduces external fragmentation by relocating allocated memory blocks

Solution

  1. Step 1: Understand compaction process

    Compaction moves allocated blocks to create contiguous free memory, reducing external fragmentation (A correct).
  2. Step 2: Analyze process halting

    Compaction usually requires pausing processes to safely move memory (D incorrect).
  3. Step 3: Consider overhead and fragmentation

    Compaction adds CPU overhead and can degrade performance temporarily (C correct); it does not affect internal fragmentation (B correct).
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Compaction generally requires halting processes [OK]
Hint: Compaction = pause + move blocks -> reduce external fragmentation [OK]
Common Mistakes:
  • Assuming compaction is transparent to running processes
  • Confusing internal and external fragmentation effects
  • Underestimating compaction overhead
3. Suppose a file system uses indexed allocation with a single-level index block. What happens when a file grows beyond the capacity of the index block, and how can this limitation be addressed?
hard
A. The file cannot grow further; to support larger files, multi-level or combined index schemes must be used
B. The file system automatically converts the file to contiguous allocation to accommodate growth
C. The index block dynamically expands by linking to additional index blocks without performance penalty
D. The file system switches to linked allocation for that file to handle the extra blocks

Solution

  1. Step 1: Understand single-level index block capacity

    A single-level index block can only hold pointers to a fixed number of blocks.
  2. Step 2: What if file grows beyond index block capacity?

    The single index block cannot hold more pointers, so the file cannot grow further under this scheme.
  3. Step 3: Analyze the file cannot grow further; to support larger files, multi-level or combined index schemes must be used

    Correct: multi-level or combined index schemes (e.g., multi-level indexing, inode structures) are used to support larger files.
  4. Step 4: Analyze the file system automatically converts the file to contiguous allocation to accommodate growth

    File systems do not convert allocation methods automatically; contiguous allocation is inflexible for growth.
  5. Step 5: Analyze the index block dynamically expands by linking to additional index blocks without performance penalty

    Index blocks do not dynamically expand by linking; multi-level indexing is a designed solution.
  6. Step 6: Analyze the file system switches to linked allocation for that file to handle the extra blocks

    File systems do not switch allocation methods on the fly; allocation method is fixed per file.
  7. Final Answer:

    Option A -> Option A
  8. Quick Check:

    Single-level index block limits file size; multi-level indexing needed for large files.
Hint: Single-level index block -> fixed max file size; multi-level indexing needed [OK]
Common Mistakes:
  • Believing index blocks can dynamically expand
  • Thinking allocation methods switch automatically
  • Assuming contiguous allocation can handle dynamic growth easily
4. 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
5. In a system using priority scheduling with aging to prevent starvation, what unexpected behavior might occur if a process holding a critical resource is preempted by an aged higher-priority process, and how can this be mitigated?
hard
A. Livelock will occur due to continuous priority changes; mitigation involves fixed priorities.
B. Deadlock will occur immediately; mitigation requires disabling aging.
C. Priority inversion may occur, delaying the critical resource release; mitigation involves priority inheritance protocols.
D. Starvation will worsen for low-priority processes; mitigation involves removing aging.

Solution

  1. Step 1: Identify the problem

    When a low-priority process holding a critical resource is preempted by a higher-priority process (due to aging), priority inversion can occur because the resource holder cannot proceed to release the resource.
  2. Step 2: Understand mitigation

    Priority inheritance protocols temporarily raise the priority of the resource-holding process to prevent inversion and reduce blocking time.
  3. Step 3: Analyze options

    Priority inversion may occur, delaying the critical resource release; mitigation involves priority inheritance protocols correctly identifies priority inversion and its mitigation. Deadlock will occur immediately; mitigation requires disabling aging incorrectly claims deadlock occurs immediately and suggests disabling aging, which is not a solution. Livelock will occur due to continuous priority changes; mitigation involves fixed priorities confuses livelock with priority changes. Starvation will worsen for low-priority processes; mitigation involves removing aging incorrectly states starvation worsens and suggests removing aging, which would increase starvation risk.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Priority inversion + priority inheritance = correct diagnosis and fix.
Hint: Priority inversion = resource holder preempted; fix with inheritance
Common Mistakes:
  • Confusing priority inversion with deadlock
  • Thinking aging always prevents all scheduling issues
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard