Quiz 2
Registry Synced

CPU Scheduling — Round Robin, Priority, MLFQ

872 words
4 min read

Reading compass

Now · 🎯 Learning Objectives

CPU Scheduling — Round Robin, Priority, MLFQ

🎯 Learning Objectives

  • Trace Round Robin scheduling with different time quanta
  • Analyze priority scheduling and the starvation problem
  • Explain the design and behavior of Multilevel Feedback Queue
  • Compare scheduling algorithms across multiple criteria

1. Round Robin (RR)

1.1 Intuition

Round Robin is like dealing cards: each process gets a small slice of CPU time (time quantum), then goes to the back of the queue. This ensures fairness and responsiveness — no process waits too long.

1.2 Algorithm

(Diagram)

1.3 Worked Example: q = 4

ProcessArrivalBurst
P1010
P205
P302
Gantt Chart (q=4):
pseudo
| P1(4) | P2(4) | P3(2) | P1(4) | P2(1) | P1(2) |
0       4       8       10      14     15      17
Tracing Table:
TimeEventQueueRunning
0StartP1(10), P2(5), P3(2)P1
4P1 preempted (6 remaining)P2(5), P3(2), P1(6)P2
8P2 preempted (1 remaining)P3(2), P1(6), P2(1)P3
10P3 done (burst=2)P1(6), P2(1)P1
14P1 preempted (2 remaining)P2(1), P1(2)P2
15P2 doneP1(2)P1
17P1 done----
Calculations:
ProcessTATWT
P1177
P21510
P3108
Average TAT = 14, Average WT = 8.33

1.4 Time Quantum Selection

(Diagram)
QuantumProsCons
Small (< 10ms)Good response timeHigh context switch overhead (>50% CPU wasted)
Large (> 100ms)Low overheadPoor response time, approaches FCFS
Typical (10-50ms)Balanced1-10% overhead, good interactive performance
Context switch overhead formula:
CPU Utilization=qq+overhead\text{CPU Utilization} = \frac{q}{q + \text{overhead}}
For q = 10ms and overhead = 1ms:
Utilization=1010+1=90.9%\text{Utilization} = \frac{10}{10 + 1} = 90.9\%

2. Priority Scheduling

2.1 Intuition

Each process has a priority (lower number = higher priority in Unix/Linux). The highest-priority ready process runs. Can be preemptive (new higher priority preempts) or non-preemptive.

2.2 Worked Example (Preemptive Priority)

ProcessArrivalBurstPriority
P1083 (low)
P2141 (high)
P3222 (mid)
P4311 (high)
Gantt Chart:
pseudo
| P1(1) | P2(1) | P4(1) | P2(3) | P3(2) | P1(7) |
0       1       2       3       6       8      15
Tracing (preemptive priority):
TimeRunningEventReady Queue
0P1(8)P1 starts--
1P2(4)P2 (pri=1) preempts P1 (pri=3)P1(7)
2P4(1)P4 (pri=1) preempts P2? P2(3) pri=1, P4 pri=1 — tie, FCFS? Actually P4 arrives, priority=1 same as P2's. Usually same priority uses RRP1(7), P2(3)
3P4 done--P1(7), P2(3)
6P2 done--P1(7), P3(2)
8P3 done--P1(7)
15P1 done----
Problem: Starvation. Low-priority processes may never run if high-priority processes keep arriving.

2.3 Aging Solution

c
while (true) {
    for each process p in ready_queue:
        p.priority++;  // Increase priority of waiting processes
    schedule();
}
Aging gradually increases the priority of waiting processes, ensuring they eventually get the CPU.

3. Multilevel Queue (MLQ)

3.1 Intuition

Partition the ready queue into multiple queues, each with its own scheduling algorithm. Processes are assigned permanently to a queue based on their type (foreground interactive, background batch). (Diagram)

3.2 Scheduling Between Queues

  • Strict priority: Serve all of Q1 before Q2, all of Q2 before Q3. Risk: Q2 and Q3 starve.
  • Time slice: 80% CPU to Q1, 20% to Q2, etc.

4. Multilevel Feedback Queue (MLFQ)

4.1 Intuition

MLFQ is the most general scheduling algorithm. Like MLQ but processes move between queues based on their behavior. Short CPU bursts → high priority (interactive). Long CPU bursts → low priority (batch).

4.2 MLFQ Rules

(Diagram) Classic 3-level MLFQ:
QueueQuantumSchedulingBehavior
Q0 (highest)5msRRInteractive processes
Q110msRRShort jobs
Q2 (lowest)FCFSFCFSLong-running batch

4.3 Worked Example

ProcessArrivalBurst
P1015
P203
P316
Queues: Q0 (q=5ms, RR), Q1 (q=10ms, RR), Q2 (FCFS) Tracing:
TimeEventQ0Q1Q2Running
0P1, P2 arriveP1(15), P2(3)----P1 starts (Q0)
5P1 quantum expired, moved to Q1P2(3)P1(10)--P2 starts (Q0)
8P2 done (ran 3ms)--P1(10)--P1 starts (Q1)
9P3 arrivesP3(6)P1(10)--P3 preempts? No, Q0 > Q1
9--P3(6)P1(10)--P3 starts (Q0)
14P3 quantum expired, moved to Q1--P1(10), P3(1)--P1 starts (Q1)
24P1 done (ran 10ms)--P3(1)--P3 starts (Q1)
25P3 done--------
TAT: P1=24, P2=8, P3=24. Avg TAT = 18.67

5. Algorithm Comparison

AlgorithmAvg WTResponseOverheadStarvationPreemptive
FCFSHighPoorLowNoNo
SJF (NP)LowPoorMediumYesNo
SRTFLowestBestHighYesYes
RRMediumGoodMediumNoYes
PriorityMediumGoodMediumYesOptional
MLFQLowGoodHighPossibleYes

6. 📝 Practice Questions

Q1: For RR with q=5 and processes P1(BT=10), P2(BT=5), P3(BT=2), compute average TAT.
Gantt: P1(5) | P2(5) | P3(2) | P1(5) → 0-5, 5-10, 10-12, 12-17
TAT: P1=17, P2=10, P3=12. Avg = 39/3 = 13 Q2: What happens to RR performance if the time quantum approaches 0? Approaches infinity?
Answer: q→0: High context switch overhead (CPU near 0% utilization). q→∞: Degrades to FCFS, response time becomes very poor. Q3: How does MLFQ prevent starvation of long-running processes?
Answer: MLFQ uses aging: processes that wait too long at lower-priority queues can be promoted back to higher queues. Some implementations also reserve a minimum CPU share for the lowest queue. Q4: Compare priority scheduling with SJF. Which is better for interactive systems?
Answer: Priority scheduling with external priorities (e.g., I/O-bound processes get higher priority) works better for interactive systems because it can favor processes that respond to user input. SJF minimizes average WT but doesn't consider interactivity. Q5: A system uses MLFQ with Q0(RR, q=4), Q1(FCFS). Draw the Gantt chart for P1(BT=10), P2(BT=3).
Tracing:
  • t=0: P1, P2 arrive. Q0: P1, P2. P1 runs.
  • t=4: P1 quantum expires → Q1. Q0: P2. P2 runs.
  • t=7: P2 done. Q1: P1. P1 runs.
  • t=17: P1 done.
Gantt: P1(0-4) | P2(4-7) | P1(7-17)

7. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.