C12 · Lesson 38 · Preemptive scheduling
Tasks, priorities, response times and priority inversion
A superloop gives every task its turn in order, so one slow task delays all the others. A real-time operating system (RTOS) instead runs the highest-priority task that's ready, interrupting a lower one the moment something more urgent can run. That makes response times independent of slow background work, but it brings two new problems: proving that every task meets its deadline when they all compete, and sharing data safely between tasks that can interrupt each other. Both have exact answers, and one of them nearly lost a spacecraft.
- Three tasks run under fixed priorities: task 1 (C₁ ms every 4 ms), task 2 (C₂ ms every 6 ms) and task 3 (C₃ ms, lowest priority).
- The staircase is the work that must be done before task 3 can finish if it starts with everyone: its own C₃ plus every release of the higher tasks so far. The diagonal is the time available.
- Task 3's response time is where the staircase first touches or dips below the diagonal. Raise C₃ or C₂ and watch that point move out, or vanish when the processor is overloaded.
Predict
A high-priority task needs a mutex that a low-priority task holds for 1 ms. A medium-priority task, which doesn't use the mutex, becomes ready and runs for 20 ms. How long can the high-priority task wait?
The medium task preempts the low one while it holds the mutex, so the high task waits for the medium task's whole 20 ms and then the low task's 1 ms: priority inversion. With priority inheritance, the low task runs at the high task's priority while it holds the mutex, so the medium task can't preempt it and the wait is 1 ms. The Mars Pathfinder lander kept resetting in 1997 from exactly this; turning on inheritance in its RTOS fixed it.
Name it
Preemption
Each task has a priority. The scheduler runs the highest-priority ready task; a task that waits (for a timer, a queue, a semaphore) stops being ready. Switching tasks saves one task's registers and restores another's: a context switch, typically a few hundred cycles. Each task needs its own stack.
Response-time analysis
With rate-monotonic priorities (shorter period, higher priority), task 's worst-case response time satisfies
Solve it by iteration, starting from , until it stops changing. The task meets its deadline if . The utilization bound from the real-time lesson is sufficient, not necessary: a set over the bound may still pass this exact test.
Sharing data
A mutex protects data shared between tasks; only its holder may touch the data. A task blocked on a mutex can suffer priority inversion, bounded by priority inheritance (the holder borrows the waiter's priority). Interrupt handlers can't block, so they hand work to tasks through queues and semaphores, which never make the ISR wait.
Worked example
Tasks of 1 ms every 4 ms, 2 ms every 6 ms and 3 ms every 13 ms run rate-monotonically. Do they meet their deadlines (deadline = period)?
Show the solution
- Task 1. It has the highest priority, so R = 1 ms.
- Task 2. Start at 3: so R = 3 ms.
- Task 3. Start at 6 and iterate until R stops changing: R = 10 ms, within 13.
- Compare with the utilization bound. All pass, although the utilization is over the three-task bound of 78.0%:
Answer yes: the response times are 1, 3 and 10 ms, all within their periods.
Worked example
A 1 kHz tick costs 50 cycles, and the system makes 2000 context switches a second at 200 cycles each. What share of the 10 MHz processor is overhead?
Show the solution
- Overhead cycles as a share of the clock.
- Where it goes. Most of it is switching; a design that wakes tasks only when there's work, rather than polling every tick, cuts it.
Answer 4.5% of the processor.
Worked example
Six tasks each get a 512-byte stack on a chip with 16 KB of RAM. What does that cost, and what does it mean for the stack-painting test?
Show the solution
- Total stack. 3 KB, about 19% of the RAM, before any data.
- What each stack must hold. Each task's stack must hold its own deepest call chain plus an interrupt frame.
- Paint each one. So each is painted and checked separately; most RTOSes can report every task's high-water mark.
Answer 3 KB, about 19% of the RAM, and each task's stack is painted and checked separately.
Check
Task 1 runs 2 ms every 5 ms at high priority; task 2 needs 7 ms at lower priority.
(a)
What is task 2's worst-case response time?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What share of the processor does task 1 take?
Check
Three tasks: 1 ms every 4 ms, 5 ms every 20 ms, 3 ms every 15 ms.
(a)
What is their total utilization?
(b)
Is it under the three-task bound of 78.0% (1 for yes)?
Check
A high-priority task waits for a mutex held for 1 ms by a low-priority task; a medium-priority task can run for 5 ms.
(a)
How long can it wait without priority inheritance?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
And with it?
Check
A 100 Hz tick costs 30 cycles, and 5000 context switches a second cost 400 cycles each, on the 10 MHz board.
(a)
What share of the processor is overhead?
(b)
How long is one context switch?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
6 tasks get 256-byte stacks on a chip with 32 KB of RAM.
(a)
How much RAM do the stacks take?
(b)
What share of RAM is that?
Check
Match each need to the RTOS tool for it.
- Aa mutex with priority inheritance
- Ba semaphore or queue the ISR gives without waiting
- Cdisabling the scheduler for the whole program
- Da periodic delay to an absolute wake-up time
- Ea queue
Check
Why must an interrupt handler never wait on a mutex?
Check
Write response_times(tasks): tasks is a list of (C, T) pairs in priority order, highest first. Return each task's worst-case response time by iterating over the higher tasks, or None for a task whose R exceeds its period T.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
How does priority inheritance prevent a medium-priority task from delaying a high-priority one indefinitely?
The trouble starts when a low-priority task holds a mutex that a high-priority task needs. The high task blocks, and any medium-priority task that becomes ready can preempt the low one, which then can't finish its critical section and release the mutex, so the high task waits for as long as the medium tasks keep running. With priority inheritance, the low task temporarily takes on the high task's priority while it holds the mutex. Medium tasks can no longer preempt it, so it finishes the critical section quickly and releases the mutex, and the high task's wait is bounded by the length of that critical section.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.