Chapter 5: CPU Scheduling
Multiple Choice · Basic Concepts
Question 1: Scheduler and Dispatcher
Which statement correctly describes the division of work between the short-term scheduler and the dispatcher?
C: The decision (policy) belongs to the scheduler, the handover (mechanism) to the dispatcher. The dispatcher runs at every switch, so it must be fast; the time it takes is the dispatch latency. A reverses the roles; B confuses memory and I/O management with scheduling.
Multiple Choice · Basic Concepts
Question 2: Only in Preemptive Scheduling
In which of the following state transitions is the scheduling decision made only by a preemptive scheduler?
D: The woken process becomes ready, but the CPU is already held by another process; dropping the running process to pick the woken one is possible only with preemptive scheduling. The running → ready transition caused by a clock interrupt is the same. In A, B and C the CPU becomes free, so every scheduler must choose.
Output Question · Basic Concepts
Question 3: Metrics from a Gantt Chart
P1 (arrival 0, CPU 5), P2 (arrival 1, CPU 4) and P3 (arrival 2, CPU 2) were scheduled as shown in the figure. Find the turnaround, waiting and response times of the three processes.
Show answer
| Process | Finish | Turnaround | Waiting | Response |
|---|---|---|---|---|
| P1 | 11 | 11 | 6 | 0 |
| P2 | 10 | 9 | 5 | 1 |
| P3 | 6 | 4 | 2 | 2 |
| Avg. | 8 | 4.33 | 1 |
Turnaround = finish − arrival; waiting = turnaround − CPU time; response = first run − arrival.
P1 started immediately (response 0) but waited in the queue twice: 2–6 and 8–10 → 4 + 2 = 6. P2's waits are 1–2 and 4–8 → 5. Response time measures the first wait, waiting time measures all waits.
Output Question · Basic Algorithms
Question 4: FCFS and SJF
P1 (arrival 0, CPU 6), P2 (1, 3), P3 (2, 8), P4 (4, 2). Draw the Gantt chart for FCFS and nonpreemptive SJF and find the average waiting and turnaround times.
Show answer
| P1 | P2 | P3 | P4 | Avg. waiting | Avg. turnaround | |
|---|---|---|---|---|---|---|
| FCFS waiting | 0 | 5 | 7 | 13 | 6.25 | 11 |
| SJF waiting | 0 | 7 | 9 | 2 | 4.5 | 9.25 |
At t = 6, SJF picks the shortest of three candidates, P4 (2). The total finish time (19) does not change, only the order does; putting the short job first saves the short job more than it costs the long one.
Output Question · Basic Algorithms
Question 5: Exponential Averaging
The next CPU burst is predicted by τn+1 = α · tn + (1 − α) · τn; α = 0.5 and the initial estimate is τ0 = 12 ms. The first three measured bursts are 8, 6 and 10 ms. What are τ1, τ2 and τ3? What would τ3 be if α = 1?
Show answer
- τ1 = 0.5 · 8 + 0.5 · 12 = 10
- τ2 = 0.5 · 6 + 0.5 · 10 = 8
- τ3 = 0.5 · 10 + 0.5 · 8 = 9 ms
If α = 1 only the latest measurement counts: τ3 = t2 = 10. With α = 0 the estimate would never change and stay at 12. The weight of old measurements is multiplied by (1 − α) at every step, so it decays exponentially.
Step-by-Step Trace · Basic Algorithms
Question 6: SRTF Trace
P1 (arrival 0, CPU 7), P2 (2, 4), P3 (4, 1), P4 (5, 4) are scheduled with SRTF. What are the Gantt chart and the average waiting time?
At t = 0 only P1 is ready. SRTF compares remaining times at every arrival and every completion, and preempts the running process if necessary.
P1 ran 0–2. At t = 2 P2 arrives: 4 < P1's remaining 5 → P1 is preempted, P2 runs.
P2 ran 2–4 (remaining 2). At t = 4 P3 arrives: 1 < 2 → P2 is preempted, P3 runs.
P3 finished at 4–5; P4 (4) arrived at the same time. Remaining: P1 5, P2 2, P4 4 → P2 continues.
P2 finished at 5–7. Remaining: P1 5, P4 4 → P4.
P4 finished at 7–11. Only P1 is left; it has been waiting since ms 2.
P1 runs 11–16. Waiting: P1 16 − 0 − 7 = 9, P2 7 − 2 − 4 = 1, P3 0, P4 11 − 5 − 4 = 2 → average 12 / 4 = 3 ms.
t = 0; while (some process unfinished) { add arrivals to the ready set(t); p = ready process with smallest remaining time; run p for 1 ms; remaining[p]--; t++; if (remaining[p] == 0) finish[p] = t; } for (each i) waiting[i] = finish[i] - arrival[i] - cpu[i];
Output Question · Basic Algorithms
Question 7: Preemptive Priority
A smaller number means higher priority. P1 (arrival 0, CPU 5, priority 3), P2 (2, 4, 1), P3 (3, 2, 4), P4 (5, 3, 2), P5 (6, 1, 5). With preemptive priority scheduling, what are the Gantt chart, the average waiting time and the average turnaround time?
Show answer
| P1 | P2 | P3 | P4 | P5 | |
|---|---|---|---|---|---|
| Finish | 12 | 6 | 14 | 9 | 15 |
| Turnaround | 12 | 4 | 11 | 4 | 9 |
| Waiting | 7 | 0 | 9 | 1 | 8 |
Average waiting 25 / 5 = 5 ms, turnaround 40 / 5 = 8 ms. P2 (priority 1), arriving at t = 2, preempts P1; P3 and P4 cannot preempt P2. When P2 finishes, the order follows the priority value: P4, P1, P3, P5.
With nonpreemptive priority the average waiting time comes out at 4.8 ms: preemption does not always improve the average; it ensures that a high-priority job is not kept waiting.
Multiple Choice · Basic Algorithms
Question 8: Aging Time
Priorities range from 0 (highest) to 63 (lowest). Aging is applied: the priority number of every process waiting in the ready queue is decreased by 1 every 10 seconds. At the latest, after how many seconds does a process with priority 63 reach priority 0, even if it never runs?
B: There are 63 steps from 63 to 0: 63 × 10 s = 630 s (10.5 minutes). C counts the 64 values between 0 and 63 and takes one step too many. Aging does not remove long waits, but it puts a finite upper bound on waiting and so prevents starvation.
Step-by-Step Trace · Round Robin
Question 9: RR Trace: q = 3
P1 (arrival 0, CPU 5), P2 (1, 3), P3 (2, 6), P4 (4, 2); q = 3 ms. A new process arriving at the same moment enters the queue before the process whose slice has just ended. What are the Gantt chart, the average waiting time and the average response time?
Only P1 is in the queue. Each process runs for at most q = 3 ms and goes back to the end of the queue.
P1 ran 0–3 (remaining 2). P2 and P3, which arrived meanwhile, entered the queue before P1.
P2 finished at 3–6 just as its slice ended. P4, which arrived at t = 4, is at the end of the queue.
P3 ran 6–9 (remaining 3) and went to the back.
P1 used its remaining 2 ms at 9–11 and finished: it gives up the CPU before its slice is used up.
P4 finished at 11–13; it first got the CPU at 11 (response 11 − 4 = 7).
P3 runs 13–16. Average waiting 23 / 4 = 5.75, average response 13 / 4 = 3.25 ms; 5 context switches.
queue = [first arrival]; while (queue is not empty) { p = queue.dequeue(); run_time = min(q, remaining[p]); run p for run_time ms; t += run_time; add arrivals during this time to the queue; remaining[p] -= run_time; if (remaining[p] > 0) queue.enqueue(p); else finish[p] = t; }
Multiple Choice · Round Robin
Question 10: Context-Switch Overhead
In RR the time slice is q = 4 ms, each context switch takes s = 1 ms, and processes always use their slices to the end. What percentage of CPU time goes to context switching?
B: Each round has 4 ms of work and 1 ms of switching: s / (q + s) = 1 / 5 = 20%. C is the ratio s / q (it leaves the switch time out of the total); D is the share of useful work. The overhead grows as q shrinks; if q becomes very large, RR turns into FCFS.
Output Question · Round Robin
Question 11: RR with Equal Jobs
A, B, C, D arrive at t = 0 in this order; each needs 4 ms of CPU. Find the average turnaround and average response time for FCFS and for RR with q = 1 ms. Which is better on which metric?
Show answer
- FCFS: finishes at 4, 8, 12, 16 → turnaround 10; response (0 + 4 + 8 + 12) / 4 = 6 ms.
- RR: finishes at 13, 14, 15, 16 → turnaround 14.5; response (0 + 1 + 2 + 3) / 4 = 1.5 ms.
With equal-length jobs RR is among the best options for response and the worst for turnaround: everyone is kept waiting until the end. RR also makes 15 context switches, FCFS 3.
Step-by-Step Trace · Multilevel Queues
Question 12: MLFQ Trace
Three queues: Q0 (q = 4), Q1 (q = 8), Q2 (FCFS). Jobs: A (arrival 0, CPU 14), B (1, 3), C (3, 9), D (16, 2). A preempted job returns to the head of its queue and completes its slice. When do the jobs finish?
A arrives and enters Q0. Every new job is treated as short first and starts in the top queue.
A used up its 4 ms slice → drops to Q1. B and C were waiting in Q0; B runs.
B finished in 3 ms, before its slice ended (response 4 − 1 = 3). Next is C in Q0.
C also uses up its slice and drops to Q1. Q0 is empty → A at the head of Q1 runs with the 8 ms slice.
At t = 16 D arrives and enters Q0: A is preempted (it had used 5 ms of its slice) and returns to the head of Q1.
D finished at 16–18 (response 0). A uses the remaining 3 ms to complete its Q1 slice.
A also used up its Q1 slice (4 + 8 = 12 ms) → Q2. C runs in Q1; the remaining 5 ms fit in its slice.
C finished at 26. Q0 and Q1 are empty; A runs its remaining 2 ms in Q2 under FCFS.
Finishes: B 7, D 18, C 26, A 28. The short jobs B and D finish quickly in the top queue; the long job A sinks down.
new job → Q0; while (jobs exist) { p = head of the topmost nonempty queue; run p for at most its slice; if (a job arrived in a higher queue) p is preempted → head of its own queue; else if (p finished) finish[p] = t; else p drops one queue down; }
Multiple Choice · Multilevel Queues
Question 13: Gaming the MLFQ
A job always uses 9 ms of its 10 ms slice in Q0 and then does a short I/O; this way it never drops to a lower queue and gets most of the CPU. Which rule defeats this trick?
A: If the allotment is kept as total usage at a level, it no longer matters how often the job gives up the CPU; once 10 ms are used the job drops. B prevents starvation but does not stop the game. C would punish genuinely interactive jobs; D merely moves the threshold.
Output Question · Proportional Share
Question 14: Stride Order
In stride scheduling A has 50 tickets, B 100 and C 25; stride = 10,000 / tickets. At each slice the process with the smallest pass value runs and its stride is added to its pass; ties go in the order A, B, C. What is the run order for the first 7 slices? How many slices does each process get?
Show answer
Strides: A 200, B 100, C 400.
| Slice | pass A | pass B | pass C | Running |
|---|---|---|---|---|
| 1 | 0 | 0 | 0 | A |
| 2 | 200 | 0 | 0 | B |
| 3 | 200 | 100 | 0 | C |
| 4 | 200 | 100 | 400 | B |
| 5 | 200 | 200 | 400 | A |
| 6 | 400 | 200 | 400 | B |
| 7 | 400 | 300 | 400 | B |
The order is A, B, C, B, A, B, B; A gets 2, B 4, C 1 slice. In 7 slices the ratio is exactly 2 : 4 : 1 = 50 : 100 : 25. At the end all three pass values are 400. With a lottery the same ratio holds only in the long run and only approximately.
Multiple Choice · Proportional Share
Question 15: Lottery Share
In lottery scheduling A has 50 tickets, B 30 and C 20. If C is given 20 more tickets, what is C's expected number of slices out of 240?
C: The total becomes 120 tickets; C's share is 40 / 120 = 1/3 → 240 / 3 = 80. A computes with the old share (20%); D assumes the total is still 100 and takes 40%. A lottery is probabilistic: 80 is the expected value, and short-term deviation is normal.
Multiple Choice · Multiprocessor Scheduling
Question 16: The Idle CPU
On a system with a ready queue per core, CPU2's queue runs empty; CPU2 takes a job from the busiest queue and runs it. What is this mechanism called and what is its cost?
D: An idle CPU taking work is pull migration; the periodic balancer pushing work away from an overloaded CPU is push migration. Linux uses both. Migration restores balance but breaks processor affinity: cache and NUMA locality are lost.
Output Question · Real-Time Scheduling
Question 17: RM and EDF
Two periodic tasks: T1 (execution 2, period 5), T2 (execution 4, period 7); the deadline is the end of the period. What is the total utilization U? Are the first deadlines met with RM and with EDF?
Show answer
U = 2/5 + 4/7 = 34/35 ≈ 0.97. The RM bound for n = 2, ≈ 0.828, is exceeded: no guarantee. For EDF, U ≤ 1 is sufficient.
In RM, T1 arriving at t = 5 preempts T2 with its static priority. In EDF at t = 5 T1's deadline is 10 and T2's is 7: T2 continues and finishes at 6. Over the 35 ms hyperperiod EDF never misses a deadline.
Multiple Choice · Operating-System Examples
Question 18: Which Class First?
Three tasks are ready on the same CPU: one SCHED_DEADLINE, one SCHED_FIFO (priority 50), and one normal (SCHED_OTHER, nice −20). Which does the scheduler run first?
A: Scheduling classes are consulted in order: deadline > real-time (FIFO/RR) > fair (EEVDF) > idle. While a task in a higher class is ready, those below it wait. nice affects only the share within the fair class.
Multiple Choice · Operating-System Examples
Question 19: CPU Share in CFS
In Linux CFS there are two CPU-bound tasks: A with nice 0 (weight 1024) and B with nice 5 (weight 335). Roughly in what ratio do they share the CPU (A : B)?
A: The share is proportional to weight: 1024 / (1024 + 335) ≈ 75%, B ≈ 25%. Since vruntime += Δ × 1024 / weight, B's virtual time grows about 3 times faster and it is picked less often. D takes the nice difference to be the ratio directly; each nice step is a weight difference of about 1.25 times.
Multiple Choice · Evaluation
Question 20: Little's Law
A server receives an average of λ = 6 requests per second, and there are on average n = 18 requests in the system. According to Little's law, how long does a request stay in the system on average?
B: n = λ × W → W = 18 / 6 = 3 s. D is the product (λ × n); A and C are the difference and the sum. Little's law is independent of the arrival and service distributions; it is the basic tool of queueing models.