Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling · Study Questions
MetricsFCFS and SJFSRTFPriority and agingRound RobinMLFQStride and lotteryMultiprocessorRM and EDFLinux CFS
Sercan KÜLCÜ

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.

P1P2P3P1P2P1024681011
Show answer
ProcessFinishTurnaroundWaitingResponse
P1111160
P210951
P36422
Avg.84.331

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 FCFSP1P2P3P40691719SJF (nonpreemptive)P1P4P2P30681119
P1P2P3P4Avg. waitingAvg. turnaround
FCFS waiting057136.2511
SJF waiting07924.59.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
4812t0 = 8t1 = 6t2 = 10τ0τ1τ2τ3

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?

0Ready (remaining):P1:7

At t = 0 only P1 is ready. SRTF compares remaining times at every arrival and every completion, and preempts the running process if necessary.

P102Ready (remaining):P2:4P1:5

P1 ran 0–2. At t = 2 P2 arrives: 4 < P1's remaining 5 → P1 is preempted, P2 runs.

P1P2024Ready (remaining):P3:1P2:2P1:5

P2 ran 2–4 (remaining 2). At t = 4 P3 arrives: 1 < 2 → P2 is preempted, P3 runs.

P1P2P30245Ready (remaining):P2:2P4:4P1:5

P3 finished at 4–5; P4 (4) arrived at the same time. Remaining: P1 5, P2 2, P4 4 → P2 continues.

P1P2P3P202457Ready (remaining):P4:4P1:5

P2 finished at 5–7. Remaining: P1 5, P4 4 → P4.

P1P2P3P2P40245711Ready (remaining):P1:5

P4 finished at 7–11. Only P1 is left; it has been waiting since ms 2.

P1P2P3P2P4P1024571116Ready (remaining):empty

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 P1P2P4P1P3P50269121415
P1P2P3P4P5
Finish12614915
Turnaround1241149
Waiting70918

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?

0Ready queue:P1

Only P1 is in the queue. Each process runs for at most q = 3 ms and goes back to the end of the queue.

P103Ready queue:P2P3P1

P1 ran 0–3 (remaining 2). P2 and P3, which arrived meanwhile, entered the queue before P1.

P1P2036Ready queue:P3P1P4

P2 finished at 3–6 just as its slice ended. P4, which arrived at t = 4, is at the end of the queue.

P1P2P30369Ready queue:P1P4P3

P3 ran 6–9 (remaining 3) and went to the back.

P1P2P3P1036911Ready queue:P4P3

P1 used its remaining 2 ms at 9–11 and finished: it gives up the CPU before its slice is used up.

P1P2P3P1P403691113Ready queue:P3

P4 finished at 11–13; it first got the CPU at 11 (response 11 − 4 = 7).

P1P2P3P1P4P30369111316Ready queue:empty

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 FCFSABCD0481216RR, q = 1ABCDABCDABCDABCD012345678910111213141516
  • 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?

0Q0 (q = 4)AQ1 (q = 8)—Q2 (FCFS)—

A arrives and enters Q0. Every new job is treated as short first and starts in the top queue.

A04Q0 (q = 4)CQ1 (q = 8)AQ2 (FCFS)—

A used up its 4 ms slice → drops to Q1. B and C were waiting in Q0; B runs.

AB047Q0 (q = 4)—Q1 (q = 8)AQ2 (FCFS)—

B finished in 3 ms, before its slice ended (response 4 − 1 = 3). Next is C in Q0.

ABC04711Q0 (q = 4)—Q1 (q = 8)CQ2 (FCFS)—

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.

ABCA0471116Q0 (q = 4)—Q1 (q = 8)ACQ2 (FCFS)—

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.

ABCAD047111618Q0 (q = 4)—Q1 (q = 8)CQ2 (FCFS)—

D finished at 16–18 (response 0). A uses the remaining 3 ms to complete its Q1 slice.

ABCADA04711161821Q0 (q = 4)—Q1 (q = 8)—Q2 (FCFS)A

A also used up its Q1 slice (4 + 8 = 12 ms) → Q2. C runs in Q1; the remaining 5 ms fit in its slice.

ABCADAC0471116182126Q0 (q = 4)—Q1 (q = 8)—Q2 (FCFS)—

C finished at 26. Q0 and Q1 are empty; A runs its remaining 2 ms in Q2 under FCFS.

ABCADACA047111618212628Q0 (q = 4)—Q1 (q = 8)—Q2 (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.

Slicepass Apass Bpass CRunning
1000A
220000B
32001000C
4200100400B
5200200400A
6400200400B
7400300400B

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.

RM (T1 always first)T1T2T10257t = 7: T2 has 1 ms left → deadline missedEDF (earliest deadline first)T1T2T1T2T102681214

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.

The End

Chapter 5: CPU Scheduling: Study Questions
Lecture slides: Chapter 5: CPU Scheduling · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.