0
0
FreeRTOSprogramming~10 mins

Task priority assignment in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task priority assignment
Create Task
Assign Priority
Add to Ready List
Scheduler Chooses Highest Priority Task
Task Runs
Task Completes or Yields
Scheduler Picks Next Highest Priority Task
When a task is created, it is given a priority. The scheduler always runs the highest priority task that is ready.
Execution Sample
FreeRTOS
xTaskCreate(task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(task2, "Task2", 1000, NULL, 3, NULL);
// Scheduler runs task2 first because priority 3 > 2
Creates two tasks with different priorities; the scheduler runs the higher priority task first.
Execution Table
StepActionTask1 PriorityTask2 PriorityScheduler DecisionRunning Task
1Create Task12N/ANo tasks ready yetNone
2Create Task223Task2 has higher priorityTask2
3Task2 runs23Task2 runningTask2
4Task2 yields/completes23Task1 is next highestTask1
5Task1 runs23Task1 runningTask1
6Task1 completes23No tasks readyNone
💡 All tasks completed or no ready tasks left
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
Task1 PriorityN/A2222
Task2 PriorityN/AN/A333
Running TaskNoneNoneTask2Task1None
Key Moments - 2 Insights
Why does Task2 run before Task1 even though Task1 was created first?
Because Task2 has a higher priority (3) than Task1 (2), the scheduler always runs the highest priority ready task first as shown in step 2 of the execution_table.
What happens when the running task yields or completes?
The scheduler picks the next highest priority ready task to run, as seen in step 4 where Task2 yields and Task1 starts running.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which task is running at step 3?
ATask1
BTask2
CNone
DBoth
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table.
At which step does the scheduler switch from Task2 to Task1?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Scheduler Decision' and 'Running Task' columns around steps 3 to 5.
If Task1 had priority 4 instead of 2, which task would run first after creation?
ATask1
BTask2
CNone
DBoth at the same time
💡 Hint
Refer to the 'Task1 Priority' and 'Task2 Priority' columns in variable_tracker and execution_table.
Concept Snapshot
Task priority assignment in FreeRTOS:
- Assign priority when creating a task.
- Scheduler runs highest priority ready task.
- Higher number means higher priority.
- Tasks with same priority share CPU time.
- Yielding or completing switches to next highest priority task.
Full Transcript
In FreeRTOS, when you create a task, you assign it a priority number. The scheduler always runs the task with the highest priority that is ready to run. For example, if Task1 has priority 2 and Task2 has priority 3, Task2 will run first because 3 is higher than 2. When Task2 finishes or yields, the scheduler switches to Task1. This process continues until all tasks complete or no tasks are ready. The priority number controls which task runs first, not the order tasks are created.