Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Priority numbering in FreeRTOS - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. In FreeRTOS, what does a higher priority number mean for a task?
easy
A. The task runs before lower priority tasks
B. The task runs after lower priority tasks
C. The task runs at the same time as lower priority tasks
D. The task is ignored by the scheduler

Solution

  1. Step 1: Understand priority numbering

    In FreeRTOS, priority numbers start at 0 for the lowest priority and increase for higher priorities.
  2. Step 2: Relate priority to task execution

    Higher priority tasks are scheduled to run before lower priority tasks to ensure responsiveness.
  3. Final Answer:

    The task runs before lower priority tasks -> Option A
  4. Quick Check:

    Higher priority number = runs first [OK]
Hint: Higher number means higher priority and runs first [OK]
Common Mistakes:
  • Thinking higher number means lower priority
  • Assuming tasks run simultaneously regardless of priority
  • Confusing priority with task creation order
2. Which of the following is the correct way to define the highest priority in FreeRTOS if configMAX_PRIORITIES is 5?
easy
A. 5
B. 6
C. 4
D. 0

Solution

  1. Step 1: Understand priority range

    Priority numbers start at 0 and go up to configMAX_PRIORITIES - 1.
  2. Step 2: Calculate highest priority

    With configMAX_PRIORITIES = 5, highest priority is 5 - 1 = 4.
  3. Final Answer:

    4 -> Option C
  4. Quick Check:

    Highest priority = configMAX_PRIORITIES - 1 = 4 [OK]
Hint: Highest priority = configMAX_PRIORITIES minus one [OK]
Common Mistakes:
  • Using configMAX_PRIORITIES as highest priority directly
  • Using 0 as highest priority
  • Choosing a number outside the allowed range
3. Given three tasks with priorities 2, 4, and 1, which task will FreeRTOS schedule to run first?
medium
A. Task with priority 4
B. Task with priority 2
C. Task with priority 1
D. All run simultaneously

Solution

  1. Step 1: Identify task priorities

    Tasks have priorities 2, 4, and 1 respectively.
  2. Step 2: Determine highest priority

    Priority 4 is the highest number, so that task runs first.
  3. Final Answer:

    Task with priority 4 -> Option A
  4. Quick Check:

    Highest priority runs first = 4 [OK]
Hint: Task with highest priority number runs first [OK]
Common Mistakes:
  • Choosing the lowest priority task
  • Assuming tasks run in creation order
  • Thinking tasks run simultaneously
4. What is wrong with this priority assignment if configMAX_PRIORITIES is 3?
vTaskPrioritySet(taskHandle, 3);
medium
A. Priority 3 is valid and highest
B. Priority 3 is out of range; max is 2
C. Priority must be zero
D. Priority must be negative

Solution

  1. Step 1: Check allowed priority range

    Priorities go from 0 to configMAX_PRIORITIES - 1, so 0 to 2 here.
  2. Step 2: Validate assigned priority

    Priority 3 is outside this range and invalid.
  3. Final Answer:

    Priority 3 is out of range; max is 2 -> Option B
  4. Quick Check:

    Max priority = 2, 3 is invalid [OK]
Hint: Priority must be less than configMAX_PRIORITIES [OK]
Common Mistakes:
  • Using configMAX_PRIORITIES as a valid priority
  • Assigning negative priorities
  • Assuming priority zero is highest
5. You have tasks with priorities 0, 1, and 2. You want the system to always run the task with priority 1 before the others, but currently priority 2 runs first. How can you fix this using FreeRTOS priority numbering?
hard
A. Set all tasks to priority 0
B. Set the task currently at 1 to priority 0
C. Set the task currently at 2 to priority 0
D. Set the priority of the task currently at 1 to 3 and others lower

Solution

  1. Step 1: Understand current priorities

    Task with priority 2 runs first because it has highest priority.
  2. Step 2: Adjust priorities to reorder execution

    Increase the priority of the task currently at 1 to a number higher than 2 (e.g., 3) so it runs first.
  3. Step 3: Lower other tasks' priorities

    Set other tasks to lower priorities to ensure the desired task runs first.
  4. Final Answer:

    Set the priority of the task currently at 1 to 3 and others lower -> Option D
  5. Quick Check:

    Higher priority number runs first, so raise desired task's priority [OK]
Hint: Raise desired task priority above others to run first [OK]
Common Mistakes:
  • Lowering all priorities to zero
  • Not increasing the desired task's priority
  • Assuming priority 0 is highest
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard