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.
💡 Running a process consumes CPU time and reduces its remaining burst.
compare
Context Switch: Preempt P1 for Higher Priority P2
Process P2's priority is increased to 4, higher than P1's priority of 3, triggering a context switch. P1 is moved back to ready, and P2 is scheduled to run.
💡 This step highlights a preemptive context switch caused by a higher priority process becoming ready.
Transitionrunning → 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
Transitionready → 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
Transitionready → 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
Step 1: Identify head direction and requests
Head at 15 moving up; requests: 2, 10, 20, 22, 40.
Step 2: SCAN moves towards higher tracks first
It services requests in ascending order from current position: 20, 22, 40.
Step 3: After reaching the highest request, SCAN reverses direction
Then it services remaining requests in descending order: 10, 2.
Final Answer:
Option C -> Option C
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
Step 1: Understand compaction process
Compaction moves allocated blocks to create contiguous free memory, reducing external fragmentation (A correct).
Step 2: Analyze process halting
Compaction usually requires pausing processes to safely move memory (D incorrect).
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).
Final Answer:
Option A -> Option A
Quick Check:
Compaction generally requires halting processes [OK]
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
Step 1: Understand single-level index block capacity
A single-level index block can only hold pointers to a fixed number of blocks.
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.
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.
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.
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.
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.
Final Answer:
Option A -> Option A
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
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.
Step 2: Contrast with processes
Processes have isolated memory, so faults are contained, making debugging easier.
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.
Final Answer:
Option A -> Option A
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
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.
Step 2: Understand mitigation
Priority inheritance protocols temporarily raise the priority of the resource-holding process to prevent inversion and reduce blocking time.
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.
Final Answer:
Option C -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.