0
0
FreeRTOSprogramming~10 mins

Priority numbering in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Priority numbering in FreeRTOS
Define task priorities
Assign priority numbers
Scheduler compares priorities
Highest priority task runs
Lower priority tasks wait
If higher priority task blocks
Next highest priority task runs
Tasks are given priority numbers; the scheduler runs the highest priority task ready to run, lower priority tasks wait unless higher priority tasks block.
Execution Sample
FreeRTOS
xTaskCreate(task1, "Task1", 1000, NULL, 3, NULL);
xTaskCreate(task2, "Task2", 1000, NULL, 1, NULL);
xTaskCreate(task3, "Task3", 1000, NULL, 5, NULL);
Creates three tasks with priorities 3, 1, and 5; the scheduler runs the highest priority task first.
Execution Table
StepTask CreatedPriority AssignedScheduler DecisionRunning Task
1Task13Task1 is highest priority so farTask1
2Task21Task1 (3) > Task2 (1), Task1 runsTask1
3Task35Task3 (5) > Task1 (3), Task3 runsTask3
4Task3 blocks or delaysN/ANext highest priority ready task is Task1 (3)Task1
5Task1 blocks or delaysN/ANext highest priority ready task is Task2 (1)Task2
6All tasks blockedN/AIdle task runsIdle Task
💡 Execution stops when all tasks are blocked or system is idle
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Task1 PriorityN/A333333
Task2 PriorityN/AN/A11111
Task3 PriorityN/AN/AN/A5555
Running TaskIdle TaskTask1Task1Task3Task1Task2Idle Task
Key Moments - 3 Insights
Why does Task3 run instead of Task1 after it is created?
Because Task3 has a higher priority number (5) than Task1 (3), the scheduler runs the highest priority ready task as shown in step 3 of the execution_table.
What happens if the highest priority task blocks or delays?
The scheduler runs the next highest priority task that is ready, as shown in steps 4 and 5 where Task3 or Task1 blocks and the scheduler switches to the next ready task.
Is a higher priority number better or worse in FreeRTOS?
A higher priority number means higher priority and the task will run before lower priority tasks, as demonstrated by Task3 with priority 5 running before Task1 with priority 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which task runs?
ATask2
BTask1
CTask3
DIdle Task
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table
At which step does the scheduler run Task2?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Running Task' column in the execution_table for when Task2 runs
If Task3 had priority 2 instead of 5, which task would run at step 3?
ATask1
BTask3
CTask2
DIdle Task
💡 Hint
Compare priorities in variable_tracker and see which is highest at step 3
Concept Snapshot
FreeRTOS uses numeric priorities for tasks.
Higher number means higher priority.
Scheduler runs highest priority ready task.
If highest priority blocks, next highest runs.
Idle task runs if no tasks ready.
Full Transcript
In FreeRTOS, each task is assigned a numeric priority. The scheduler always runs the task with the highest priority number that is ready to run. When a new task is created with a higher priority than the currently running task, the scheduler switches to the new higher priority task immediately. If the highest priority task blocks or delays, the scheduler runs the next highest priority task that is ready. If no tasks are ready, the idle task runs. This priority numbering system ensures that important tasks get CPU time before less important ones.