3B Infrastructure home

All subjects

Real-time Systems Engineering (Raspberry Pi)

Building systems where being late counts as being wrong: deadlines, scheduling, priority inversion, and real-time Linux on a Raspberry Pi. Press play on any sketch to watch it happen.

1 Real-time means deadlines

Real-time does not mean fast. It means predictable: a result must arrive by its deadline, every time. A correct answer delivered too late can be a failure.

  • Hard real-time: a missed deadline is a system failure, as in airbags and flight control.
  • Firm real-time: a late result is worthless, but the system survives.
  • Soft real-time: lateness reduces quality, as in video calls.
  • Jitter: variation in timing. Often this matters more than the average speed.
  • WCET: worst-case execution time, the number that really counts.

Timing diagram

CrashdetectedTaskrunsDeadline20 msFinished: ?

1/4 A crash is detected, and the clock starts.

TypeExampleA missed deadline means
HardAirbagPossible injury
FirmFactory camera checkThat reading is lost
SoftVideo callA brief glitch

Rule of thumb: Design for the worst case, not the average one.

2 Scheduling and priority inversion

When several tasks share a processor, the scheduler picks who runs. With rate monotonic scheduling, the most frequent task gets the highest priority. Done carefully, you can prove that all deadlines will be met.

  • Preemption: a high-priority task can interrupt a lower one.
  • Rate monotonic: a shorter period gets a higher priority.
  • Utilisation: U is the sum of C/T for each task. For two tasks, RM guarantees success up to about 83%.
  • Priority inversion: a low task holding a lock blocks a high task, while a medium task runs.
  • Priority inheritance: the lock holder briefly borrows the high priority.
Task A: C = 3 ms, T = 10 ms
Task B: C = 6 ms, T = 20 ms

U = 3/10 + 6/20 = 0.6
0.6 ≤ 0.83, so all deadlines are met

Scheduling diagram

HighreadyMediumidleLockLowholds lock

1/5 The Low task takes the lock.

Rule of thumb: Keep locks short. A famous priority inversion reset the Mars Pathfinder lander in 1997.

3 Real-time on a Raspberry Pi

A Raspberry Pi runs Linux, which is built for fairness and throughput, not deadlines. Normal Linux can delay a task unpredictably. You can improve this a lot, but for hard deadlines a microcontroller is still the safer choice.

  • SCHED_FIFO: a real-time policy. Your task runs until it blocks or something of higher priority needs the CPU.
  • PREEMPT_RT: makes the kernel far more preemptible. It became part of mainline Linux in version 6.12.
  • Measure it: cyclictest measures your real worst-case delay.
  • Lock memory: mlockall stops page faults causing surprise delays.
  • Hybrid: let a Pi Pico or Arduino handle the strict timing, and the Pi do the heavy thinking.
# run with a real-time priority
sudo chrt -f 80 ./control_loop

# measure worst-case latency
sudo cyclictest --mlockall --priority=90 --interval=1000

Timing diagram

Control loopevery 1 msLinuxnormalMotor

1/5 On a quiet Pi, the 1 ms loop looks perfect. This is misleading.

Rule of thumb: Never trust a real-time claim until you have measured it under full load.