Priority numbering helps FreeRTOS decide which task should run first. It keeps your program organized and responsive.
Priority numbering in FreeRTOS
UBaseType_t uxPriority = 0; // Lowest priority uxPriority = configMAX_PRIORITIES - 1; // Highest priority
Priority numbers start at 0, which is the lowest priority.
The highest priority is one less than configMAX_PRIORITIES.
const UBaseType_t lowPriority = 0; const UBaseType_t highPriority = configMAX_PRIORITIES - 1;
xTaskCreate(taskFunction, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(taskFunction, "Task2", 1000, NULL, configMAX_PRIORITIES - 1, NULL);
This program creates two tasks: one with low priority and one with the highest priority. The high priority task runs more often because FreeRTOS always runs the highest priority task ready to run.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskLowPriority(void *pvParameters) { for (;;) { printf("Low priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } void vTaskHighPriority(void *pvParameters) { for (;;) { printf("High priority task running\n"); vTaskDelay(pdMS_TO_TICKS(500)); } } int main(void) { xTaskCreate(vTaskLowPriority, "LowTask", 1000, NULL, 1, NULL); xTaskCreate(vTaskHighPriority, "HighTask", 1000, NULL, configMAX_PRIORITIES - 1, NULL); vTaskStartScheduler(); for (;;) {} return 0; }
Tasks with higher priority always run before lower priority tasks.
If two tasks have the same priority, FreeRTOS switches between them to share CPU time.
Setting priorities carefully helps your system stay fast and responsive.
Priority numbers start at 0 (lowest) and go up to configMAX_PRIORITIES - 1 (highest).
Higher priority tasks run before lower priority ones.
Use priority numbering to control task order and system responsiveness.
Practice
Solution
Step 1: Understand priority numbering
In FreeRTOS, priority numbers start at 0 for the lowest priority and increase for higher priorities.Step 2: Relate priority to task execution
Higher priority tasks are scheduled to run before lower priority tasks to ensure responsiveness.Final Answer:
The task runs before lower priority tasks -> Option AQuick Check:
Higher priority number = runs first [OK]
- Thinking higher number means lower priority
- Assuming tasks run simultaneously regardless of priority
- Confusing priority with task creation order
Solution
Step 1: Understand priority range
Priority numbers start at 0 and go up to configMAX_PRIORITIES - 1.Step 2: Calculate highest priority
With configMAX_PRIORITIES = 5, highest priority is 5 - 1 = 4.Final Answer:
4 -> Option CQuick Check:
Highest priority = configMAX_PRIORITIES - 1 = 4 [OK]
- Using configMAX_PRIORITIES as highest priority directly
- Using 0 as highest priority
- Choosing a number outside the allowed range
Solution
Step 1: Identify task priorities
Tasks have priorities 2, 4, and 1 respectively.Step 2: Determine highest priority
Priority 4 is the highest number, so that task runs first.Final Answer:
Task with priority 4 -> Option AQuick Check:
Highest priority runs first = 4 [OK]
- Choosing the lowest priority task
- Assuming tasks run in creation order
- Thinking tasks run simultaneously
vTaskPrioritySet(taskHandle, 3);Solution
Step 1: Check allowed priority range
Priorities go from 0 to configMAX_PRIORITIES - 1, so 0 to 2 here.Step 2: Validate assigned priority
Priority 3 is outside this range and invalid.Final Answer:
Priority 3 is out of range; max is 2 -> Option BQuick Check:
Max priority = 2, 3 is invalid [OK]
- Using configMAX_PRIORITIES as a valid priority
- Assigning negative priorities
- Assuming priority zero is highest
Solution
Step 1: Understand current priorities
Task with priority 2 runs first because it has highest priority.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.Step 3: Lower other tasks' priorities
Set other tasks to lower priorities to ensure the desired task runs first.Final Answer:
Set the priority of the task currently at 1 to 3 and others lower -> Option DQuick Check:
Higher priority number runs first, so raise desired task's priority [OK]
- Lowering all priorities to zero
- Not increasing the desired task's priority
- Assuming priority 0 is highest
Ready to prove it?
Pass the test and this pattern moves unknown
