Sub topic 17
Multiprocessor and Real-Time Scheduling: A Short Overview
This chapter assumes you know the single-CPU algorithms from earlier chapters (FCFS, SJF, Round Robin, priority scheduling) and the idea of a ready queue. It also assumes you know what a context switch costs. Here we ask two questions. What changes when there are several CPUs? What changes when tasks have deadlines?
Scheduling with Several CPUs
Asymmetric and symmetric multiprocessing
There are two ways to organize scheduling on a machine with more than one processor:
- Asymmetric multiprocessing (AMP): one master processor runs all scheduling and kernel code. The other processors run only user code. It is simple, because only one CPU touches the scheduler data structures, but the master can become a bottleneck.
- Symmetric multiprocessing (SMP): every processor schedules itself. Each one picks a process from a ready queue. Modern general-purpose systems use SMP.
Ready queue organization
With SMP there are two ways to arrange the ready queue.
| Design | How it works | Strength | Weakness |
|---|---|---|---|
| Common ready queue | All CPUs take work from one shared queue | Automatic load balance | The queue needs locking, so it becomes a contention point as CPUs increase |
| Per-CPU ready queues | Each CPU has a private queue | No lock contention, good cache reuse | Queues can become uneven, so one CPU may idle while another has a backlog |
Most operating systems use per-CPU queues and add a balancing mechanism on top.
Processor affinity
When a process runs on a CPU, its data fills that CPU's cache. If the process moves to another CPU, the old cache contents are useless and the new cache must be refilled. For this reason schedulers try to keep a process on the same processor. This is processor affinity.
- Soft affinity: the OS tries to keep the process on one CPU but may move it.
- Hard affinity: the process may specify a set of CPUs it is allowed to run on. On Linux this is done with
sched_setaffinity.
On NUMA (non-uniform memory access) systems, affinity also matters for memory. A CPU reaches its own node's memory faster than a remote node's memory.
Load balancing
Load balancing keeps all CPUs busy. It is needed mainly with per-CPU queues. There are two approaches:
- Push migration: a periodic task checks each CPU's load. If it finds an imbalance, it moves processes from busy CPUs to idle or lightly loaded ones.
- Pull migration: an idle CPU takes a waiting process from a busy CPU's queue.
Both approaches can run together. Balancing works against affinity, because every migration throws away cached data. The scheduler must trade an even load against cache warmth.
Multicore and hardware threads
A multicore chip puts several CPU cores on one die. Some cores also run several hardware threads (for example, Intel Hyper-Threading). When one hardware thread stalls on a memory access, the core switches to another thread. To the OS each hardware thread looks like a logical CPU. Because those logical CPUs share one core's resources, a good scheduler prefers to place two busy processes on separate physical cores before it doubles up on one.
Real-Time Scheduling
A real-time system must produce correct results by a deadline.
- Hard real-time: missing a deadline is a failure, as with airbag control or flight control. The system must guarantee deadlines.
- Soft real-time: missing a deadline degrades quality but is tolerable, as with video playback. Real-time tasks get priority over ordinary tasks, but there is no guarantee.
Two kinds of delay affect response to an event. Interrupt latency is the time from the arrival of an interrupt to the start of its handler. Dispatch latency is the time the dispatcher needs to stop one process and start another. A real-time kernel must be preemptive and must keep both latencies small and bounded.
Periodic tasks
Many real-time tasks are periodic. A periodic task has:
- a processing time
t(calledcin some books), - a deadline
d, the time by which it must finish after being released, - a period
p, the interval between successive releases.
The usual assumption is that t ≤ d ≤ p, and often d = p. The CPU utilization of a task is t/p. The total utilization of a task set is the sum of these values.
Rate-Monotonic scheduling (RM)
RM is a static-priority preemptive algorithm. Each task gets a fixed priority that is inversely related to its period, so the task with the shortest period has the highest priority. The reasoning is that tasks that repeat often need the CPU more urgently.
For n periodic tasks with d = p, RM is guaranteed to meet all deadlines if
U ≤ n(2^(1/n) − 1)
This bound is 1.0 for n = 1, about 0.828 for n = 2, and about 0.780 for n = 3. It approaches ln 2 ≈ 0.693 as n grows. The bound is a sufficient condition only. A task set above the bound may still be schedulable, and you must then simulate it to find out.
Earliest-Deadline-First scheduling (EDF)
EDF is a dynamic-priority algorithm. At each scheduling point it runs the ready task whose absolute deadline is nearest. When a task becomes ready, it announces its deadline. For periodic tasks with d = p, EDF is feasible exactly when U ≤ 1 on one CPU. EDF is theoretically optimal on a single processor, but real systems often prefer RM because fixed priorities are easier to implement and analyze.
Worked example
Take P1 with p = 50, t = 25 and P2 with p = 80, t = 35. Deadlines equal periods.
U = 25/50 + 35/80 = 0.5 + 0.4375 = 0.9375
This is above the RM bound of 0.828 and below the EDF limit of 1.
Under RM: P1 has the shorter period, so it has higher priority.
- P1 runs from 0 to 25.
- P2 runs from 25 to 50 and has used 25 of its 35 units.
- P1 is released again at 50 and preempts P2. It runs from 50 to 75.
- P2 resumes at 75 and runs until its deadline at 80. It has now used 30 units and 5 remain. P2 misses its deadline.
Under EDF:
- P1 (deadline 50) runs from 0 to 25.
- P2 (deadline 80) runs from 25 to 50 and has 10 units left.
- At 50, P1's new deadline is 100 and P2's is 80, so P2 continues. It finishes at 60.
- P1 runs from 60 to 85. At 80, P2 is released with deadline 160, but P1 (deadline 100) is still earlier, so P1 continues.
- P2 runs from 85 to 100 (15 units). P1 is released again at 100 with deadline 150, so P1 runs from 100 to 125.
- P2 finishes its remaining 20 units from 125 to 145, before its deadline of 160.
All deadlines are met.
The following program checks the two utilization tests:
def rm_bound(n):
return n * (2 ** (1 / n) - 1)
tasks = [(50, 25), (80, 35)] # (period, processing time)
u = sum(t / p for p, t in tasks)
bound = rm_bound(len(tasks))
print(f"U = {u:.4f}")
print(f"RM bound = {bound:.4f}, RM guaranteed: {u <= bound}")
print(f"EDF feasible: {u <= 1}")It prints U = 0.9375, RM bound = 0.8284, RM guaranteed: False and EDF feasible: True.
Common errors
- Assigning RM priority by processing time or by deadline instead of by period.
- Using the RM bound for
ntasks as if it were1for the whole set. - Forgetting that a task released at time
k·ppreempts a lower-priority task under RM, but under EDF preempts only if its absolute deadline is earlier. - Treating EDF's
U ≤ 1test as valid when deadlines are shorter than periods.
What GATE Tends to Ask
GATE questions here are usually short. Typical items are the RM utilization bound for a given n, whether a task set is feasible under EDF, and which of the push, pull and affinity statements are true. Compute utilization first. Then apply the test the question asks for.
Exercise
Three periodic tasks have (period, processing time) = (4, 1), (5, 2), and (10, 1). Deadlines equal periods. Compute total utilization. Is the set guaranteed schedulable under RM by the utilization bound? Is it feasible under EDF?
Show answer
U = 1/4 + 2/5 + 1/10 = 0.25 + 0.40 + 0.10 = 0.75. The RM bound for n = 3 is 3(2^(1/3) − 1) ≈ 0.780. Since 0.75 ≤ 0.780, RM is guaranteed to meet all deadlines. EDF is feasible because 0.75 ≤ 1.
Exercise
A system has one ready queue per CPU. CPU 2 becomes idle while CPU 0 has five waiting processes. Name the load-balancing technique in which CPU 2 takes work from CPU 0, and state the cost this technique can impose.
Show answer
This is pull migration. The cost is lost processor affinity: the migrated process's data is not in CPU 2's cache, so it incurs cache misses until the cache is refilled.