Ubuntu Linux for Robotics · Lesson 84 of 96
Real-time Linux
PREEMPT_RT, priorities, CPU isolation, memory locking and measuring latency.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain real-time linux in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Real-time means meeting a deadline every cycle, not just being fast on average. Ubuntu Pro offers a real-time kernel (PREEMPT_RT), which makes most of the kernel preemptible so a high-priority thread runs when it needs to. Check the running kernel with uname -a (look for PREEMPT_RT).
Run the control thread with a real-time policy (chrt -f 80 or SCHED_FIFO), lock memory (mlockall), pin it to an isolated CPU core (isolcpus, taskset) and keep other load off that core. Measure with cyclictest and your own jitter histogram; never claim hard real-time without measurements, and keep allocation, logging and blocking calls out of the loop.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the real-time linux commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
uname -a | grep -i preempt
# Check whether the running kernel is a real-time kernel.
sudo apt install rt-tests
# Install cyclictest and related tools.
sudo cyclictest -m -p80 -i1000 -l10000
# Measure wake-up latency at priority 80 with a 1 ms interval.
sudo chrt -f 80 taskset -c 3 ./control_loop
# Run the control program with a FIFO priority pinned to CPU 3.
cat /sys/devices/system/cpu/isolated
# Show which CPU cores are isolated from the scheduler.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · The robot's computer
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Real-time Linux. Expected result: The latency statistics of cyclictest in microseconds
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
# TODO: run cyclictest with memory locking (-m), priority 80 (-p80), a 1000 us interval (-i1000) and 10000 loops (-l10000)Expected output
The latency statistics of cyclictest in microsecondsStuck? Show the full solution (try it yourself first)
sudo cyclictest -m -p80 -i1000 -l10000Expected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Measure latency with cyclictest on the normal kernel and, if available, on PREEMPT_RT, while running a CPU stress test. Record the maximum and 99th percentile in each case.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.