Real-time Linux: PREEMPT_RT and dividing the roles
Linux can get close to real time, but it is not the same as an MCU. How to measure latency and how to decide what to leave to the MCU.
What real time means
Not "fast" but "responds within the deadline even in the worst case". An average of 10 µs is not real time if it occasionally takes 5 ms.
PREEMPT_RT
It makes most of the kernel preemptible, so a high-priority real-time thread is not blocked for long by kernel work. It has been part of the mainline kernel since 6.12.
Measuring latency
cyclictest -p 90 -t1 -i 1000 -l 100000 -m
The measure is the maximum latency under load (stress, network traffic). An ordinary kernel shows several ms; PREEMPT_RT brings it down to tens of µs, depending on the hardware.
RoadmapSee where this note sits in Embedded Linux→Writing a real-time application
- Set a
SCHED_FIFOpriority - Lock memory with
mlockall()(prevents page faults) - No dynamic allocation or file I/O inside the real-time loop
- Isolate one CPU core for the real-time work
Dividing the roles
| Leave to the MCU | Leave to Linux |
|---|---|
| Current control at 10 kHz and above, PWM generation | Higher-level path planning, image processing |
| µs-level protection such as overcurrent | UI, network, log storage |
| Safety functions | Updates, cloud connection |
Robots and vehicles commonly use a Linux SoC and an MCU together, connected by UART, SPI, CAN or shared memory (RPMsg on combined Cortex-A + M chips).