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.
💡 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. In a system where multiple producers and consumers share a fixed-size buffer, which synchronization mechanism best ensures that producers wait when the buffer is full and consumers wait when the buffer is empty?
easy
A. Mutex lock only to protect buffer access
B. Spinlocks to busy-wait until buffer space is available
C. Counting semaphores to track empty and full slots along with a mutex for mutual exclusion
D. Condition variables without any semaphore or mutex
Solution
Step 1: Identify synchronization needs
Producers must wait if the buffer is full; consumers must wait if empty. This requires tracking counts of empty and full slots.
Step 2: Role of counting semaphores
Counting semaphores elegantly track available slots (empty) and filled slots (full), enabling blocking waits without busy-waiting.
Step 3: Need for mutual exclusion
A mutex is required to protect concurrent access to the buffer itself to avoid race conditions.
Step 4: Why other options fail
Mutex alone (A) cannot block producers/consumers based on buffer state; spinlocks (C) waste CPU cycles; condition variables (D) require mutexes and signaling to work correctly in this scenario.
Final Answer:
Option C -> Option C
Quick Check:
Counting semaphores + mutex is the classic producer-consumer solution [OK]
Peterson's algorithm uses busy waiting (spinlock), which can waste CPU resources.
Step 2: Analyze other options
It cannot guarantee mutual exclusion under any circumstances is false because mutual exclusion is guaranteed. It depends on hardware atomic instructions to function correctly is incorrect since Peterson's algorithm was designed to avoid hardware atomic instructions. It allows unbounded waiting, leading to starvation is wrong because bounded waiting is guaranteed.
Final Answer:
Option B -> Option B
Quick Check:
Busy waiting is a known practical limitation of Peterson's algorithm.
Hint: Peterson's = busy waiting, no hardware locks, bounded waiting
3. Which of the following statements about deadlock prevention in the Dining Philosophers problem is INCORRECT?
medium
A. Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation
B. Allowing a philosopher to pick up both forks only if both are available prevents deadlock
C. Imposing a strict ordering on resource acquisition always prevents deadlock
D. Breaking one of the four Coffman conditions is sufficient to prevent deadlock
Solution
Step 1: Analyze each statement
Imposing a strict ordering on resource acquisition always prevents deadlock is correct: ordering resources prevents circular wait; Allowing a philosopher to pick up both forks only if both are available prevents deadlock is correct: atomic acquisition avoids partial hold; Breaking one of the four Coffman conditions is sufficient to prevent deadlock is correct: breaking any Coffman condition prevents deadlock.
Step 2: Evaluate Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation
Asymmetric fork picking prevents deadlock but does not guarantee no starvation, as some philosophers may be repeatedly delayed.
Final Answer:
Option A -> Option A
Quick Check:
Only Using asymmetric fork picking (odd philosophers pick left first, even pick right first) guarantees no starvation incorrectly claims starvation is guaranteed prevented.