Priority design helps decide which task runs first in a program. It keeps important jobs fast and smooth.
Why priority design matters in FreeRTOS
In FreeRTOS, you set priority when creating a task: xTaskCreate( TaskFunction, // Function that implements the task "TaskName", // Name of the task StackDepth, // Stack size in words, not bytes Parameters, // Parameters to the task Priority, // Priority number (higher means more important) &TaskHandle // Task handle );
Priority is a number; higher means the task runs before lower priority tasks.
Tasks with the same priority share CPU time fairly.
xTaskCreate(Task1, "SensorTask", 1000, NULL, 3, NULL);
xTaskCreate(Task2, "LoggerTask", 1000, NULL, 1, NULL);
This program creates two tasks: one high priority and one low priority. The high priority task runs first and prints its message every second. The low priority task runs only when the high priority task is waiting.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void HighPriorityTask(void *pvParameters) { for (;;) { printf("High priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } void LowPriorityTask(void *pvParameters) { for (;;) { printf("Low priority task running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } int main(void) { xTaskCreate(HighPriorityTask, "HighTask", 1000, NULL, 3, NULL); xTaskCreate(LowPriorityTask, "LowTask", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
If a high priority task never waits, lower priority tasks may never run.
Design priorities carefully to avoid blocking important tasks.
Priority design controls which tasks run first.
Higher priority tasks get CPU time before lower ones.
Good priority design keeps your program fast and responsive.
Practice
Why is task priority important in FreeRTOS?
Solution
Step 1: Understand task scheduling in FreeRTOS
FreeRTOS uses task priorities to decide which task runs when multiple tasks are ready.Step 2: Identify the role of priority
The highest priority ready task gets CPU time first, ensuring important tasks run promptly.Final Answer:
It determines which task runs first when multiple are ready. -> Option AQuick Check:
Priority controls task execution order = A [OK]
- Confusing priority with memory size
- Thinking priority limits execution time
- Assuming priority controls stack protection
Which of the following is the correct way to set a task priority in FreeRTOS?
vTaskPrioritySet(Solution
Step 1: Recall FreeRTOS API for setting priority
The function vTaskPrioritySet requires the task handle and the new priority as parameters.Step 2: Check parameter order
The correct order is first the task handle, then the new priority value.Final Answer:
vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority); -> Option BQuick Check:
Correct parameter order = C [OK]
- Swapping parameter order
- Missing one parameter
- Using wrong parameter types
Given two tasks, TaskA with priority 3 and TaskB with priority 2, both ready to run, which task will FreeRTOS run first?
Solution
Step 1: Compare task priorities
TaskA has priority 3, TaskB has priority 2. Higher number means higher priority.Step 2: Determine which task runs first
FreeRTOS runs the highest priority ready task first, so TaskA runs before TaskB.Final Answer:
TaskA -> Option DQuick Check:
Higher priority runs first = B [OK]
- Thinking lower number means higher priority
- Assuming tasks run simultaneously
- Believing creation order affects scheduling
What is wrong with this code snippet?
vTaskPrioritySet(3, TaskHandle);Solution
Step 1: Check function parameter order
The correct order is (TaskHandle_t xTask, UBaseType_t uxNewPriority), but here it is reversed.Step 2: Validate parameter types
TaskHandle should be first, priority second. The code swaps them causing error.Final Answer:
Parameters are in wrong order. -> Option AQuick Check:
Correct parameter order matters = A [OK]
- Swapping parameters
- Using wrong data types
- Misnaming the function
You have three tasks: Task1 (priority 2), Task2 (priority 2), and Task3 (priority 3). Task1 and Task2 share a resource and must not run simultaneously. How should you design priorities to keep the system responsive and safe?
Solution
Step 1: Assign highest priority to critical task
Task3 has highest priority (3) to keep system responsive.Step 2: Use equal lower priorities and mutex for shared resource
Task1 and Task2 have same priority (2) and use a mutex to prevent simultaneous access.Step 3: Avoid equal priorities without synchronization
Equal priorities without mutex risk resource conflicts; delays reduce responsiveness.Final Answer:
Set Task3 highest priority, Task1 and Task2 equal lower priorities, use mutex for resource. -> Option CQuick Check:
Priority + mutex ensures safety and responsiveness = D [OK]
- Ignoring resource protection
- Setting all tasks same priority without sync
- Using delays instead of priorities
Ready to prove it?
Pass the test and this pattern moves unknown
