C1238 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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 i's worst-case response time satisfies

Ri=Ci+∑jhigher⌈RiTj⌉Cj

Solve it by iteration, starting from R=Ci+∑Cj, until it stops changing. The task meets its deadline if Ri≤Di. The utilization bound n(21/n−1) 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
  1. Task 1. It has the highest priority, so R = 1 ms.
  2. Task 2. Start at 3: 2+⌈3/4⌉×1=3 so R = 3 ms.
  3. Task 3. Start at 6 and iterate until R stops changing: 3+2×1+1×2=73+2+2×2=93+3+4=103+⌈10/4⌉+2⌈10/6⌉=10 R = 10 ms, within 13.
  4. Compare with the utilization bound. All pass, although the utilization is over the three-task bound of 78.0%: 14+26+313=81.4%

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
  1. Overhead cycles as a share of the clock. 1000×50+2000×200107=4.5%
  2. 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
  1. Total stack. 6×512=3072bytes 3 KB, about 19% of the RAM, before any data.
  2. What each stack must hold. Each task's stack must hold its own deepest call chain plus an interrupt frame.
  3. 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.

  1. Aa mutex with priority inheritance
  2. Ba semaphore or queue the ISR gives without waiting
  3. Cdisabling the scheduler for the whole program
  4. Da periodic delay to an absolute wake-up time
  5. 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 R=Ci+∑j⌈R/Tj⌉Cj 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?

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.