Ground Embedded KO Subscribe

Notes / RTOS

Priority and preemption: urgent work first

A preemptive scheduler hands over the CPU the moment a higher-priority task becomes ready. How to decide priorities.

GROUND EMBEDDED · NOTERTOS 03Priority and preemptionBUFFERhead →tail →write · read

Preemptive scheduling

High taskLow taskwakesdone
Preemption. The moment the high task becomes ready the low task stops, and it continues when the high task finishes.

FreeRTOS by default runs "the highest-priority task that is Ready". Even while a lower task is running, the switch happens immediately when a higher task wakes. Tasks of equal priority take turns every tick (round robin).

Criteria for setting priorities

Give higher priority to shorter and stricter deadlines (the intuition behind rate-monotonic scheduling).

Task Period / deadline Priority
Motor speed control 1 ms High
IMU read 5 ms Upper middle
CAN communication 10 ms Middle
Display update 50 ms Low
Log storage Whenever Lowest

High priority goes to what "must not be late", not to what is "important". The display may look important, but nothing breaks if it is 50 ms late.

3D labRun what you have read so far→

Relation to interrupts

Interrupts run before any task. Loops at 10 kHz or more, such as current control, run in an interrupt and not in a task; tasks take the slower loops above them.

Starvation

If a high-priority task keeps running without ever going Blocked, the tasks below never run. Every task needs a point where it waits (a delay, a queue wait).

3D LABRTOS Scheduler SimulationRun what this note explains and watch it respond.Open the lab →FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →