Priority
The number that tells the scheduler how urgent a task is, and why FreeRTOS and Zephyr count it in opposite directions.
A task’s priority tells the scheduler how urgent it is. Whenever the scheduler chooses, it runs the most urgent task that is ready; that is the whole rule behind preemption. Tasks of equal priority share the CPU by time slicing.
The two kernels count in opposite directions:
- FreeRTOS: a higher number is more urgent. The idle task has priority 0
(
tskIDLE_PRIORITY), and application tasks use 1 up toconfigMAX_PRIORITIES− 1. The simulator setsconfigMAX_PRIORITIESto 5. - Zephyr: a lower number is more urgent. Priorities from 0 upwards are preemptible; negative priorities are cooperative, and a cooperative thread is never preempted by another thread. The idle thread has the least urgent priority, 15 in the simulated configuration.
The simulator’s scenarios give each task an urgency (1 = least urgent) and translate it, so one scenario runs on both kernels. The task cards show each kernel’s own number.
Two details that surprise people:
- A task has two priorities. The one the application gave it (FreeRTOS
uxBasePriority) and the one the scheduler uses (uxPriority). They differ while the task holds a mutex that a more urgent task waits for: that is priority inheritance. Zephyr keeps the thread’s own priority inorig_priowhile it holds a mutex, and works outbase.prioagain on every unlock. - Priority is not importance. It decides who runs first, nothing else. A low-priority task can wait forever if more urgent tasks never block, and a high-priority task can still wait for a low-priority one through a shared lock: priority inversion.