Priority-based scheduling in FreeRTOS - Time & Space Complexity
When using priority-based scheduling in FreeRTOS, tasks are chosen based on their priority levels.
We want to understand how the time to pick the next task changes as the number of tasks grows.
Analyze the time complexity of the following code snippet.
// Simplified task selection in FreeRTOS
TaskHandle_t xTaskToRun = NULL;
for (int priority = configMAX_PRIORITIES - 1; priority >= 0; priority--) {
if (listLIST_IS_EMPTY(&pxReadyTasksLists[priority]) == pdFALSE) {
xTaskToRun = listGET_OWNER_OF_HEAD_ENTRY(&pxReadyTasksLists[priority]);
break;
}
}
// xTaskToRun now points to the highest priority ready task
This code checks task lists from highest to lowest priority to find the next task to run.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over priority levels from highest to lowest.
- How many times: Up to
configMAX_PRIORITIEStimes, which is a fixed number.
The loop runs once for each priority level until it finds a ready task.
| Input Size (Number of Priorities) | Approx. Operations |
|---|---|
| 5 | Up to 5 checks |
| 10 | Up to 10 checks |
| 32 | Up to 32 checks |
Pattern observation: The number of checks grows linearly with the number of priority levels, but this number is usually fixed and small.
Time Complexity: O(1)
This means the time to select the next task does not grow with the number of tasks, only with the fixed number of priority levels.
[X] Wrong: "The scheduler checks every task every time, so time grows with total tasks."
[OK] Correct: The scheduler only checks priority lists, not every task individually, so time depends on priority count, not total tasks.
Understanding how priority-based scheduling scales helps you explain real-time system behavior clearly and confidently.
"What if the number of priority levels was not fixed but grew with the number of tasks? How would the time complexity change?"
Practice
What does priority-based scheduling in FreeRTOS ensure?
Solution
Step 1: Understand priority-based scheduling
Priority-based scheduling means tasks with higher priority get CPU time before lower priority tasks.Step 2: Apply this to FreeRTOS task execution
FreeRTOS runs the highest priority task that is ready to run first, preempting lower priority tasks.Final Answer:
The highest priority task runs first -> Option CQuick Check:
Priority-based scheduling = highest priority runs first [OK]
- Thinking tasks run in creation order
- Assuming equal CPU time for all tasks
- Believing tasks run randomly
Which of the following is the correct way to set a task priority when creating a task in FreeRTOS?
xTaskCreate(TaskFunction, "TaskName", 1000, NULL, ___, &TaskHandle);Solution
Step 1: Identify parameters of xTaskCreate
The fifth parameter in xTaskCreate is the task priority.Step 2: Match parameter to correct argument
Passing 'priority' sets the task's priority level correctly.Final Answer:
priority -> Option DQuick Check:
Fifth parameter in xTaskCreate = priority [OK]
- Confusing stack size with priority
- Using task handle as priority
- Passing task name instead of priority
Given the following FreeRTOS tasks with priorities, which task will run first?
Task A priority = 2
Task B priority = 3
Task C priority = 1
Solution
Step 1: Compare task priorities
Task B has priority 3, which is higher than Task A (2) and Task C (1).Step 2: Determine which task runs first
FreeRTOS runs the highest priority task first, so Task B runs before others.Final Answer:
Task B -> Option BQuick Check:
Highest priority (3) = Task B [OK]
- Choosing task with lowest priority
- Assuming tasks run simultaneously
- Ignoring priority values
Identify the error in this FreeRTOS task creation code snippet:
BaseType_t result = xTaskCreate(TaskFunc, "Task1", 500, NULL, 0, NULL);
if(result != pdPASS) {
// Handle error
}What is wrong with the priority value?
Solution
Step 1: Understand priority 0 in FreeRTOS
Priority 0 is the lowest priority and is valid in FreeRTOS.Step 2: Check if priority 0 causes error
Priority 0 does not cause an error; task creation can succeed with it.Final Answer:
Priority 0 means lowest priority but is valid -> Option AQuick Check:
Priority 0 is valid lowest priority [OK]
- Thinking priority 0 is invalid
- Assuming priority must be > 5
- Using negative priority values
You have three tasks with priorities 2, 4, and 3. Task with priority 4 is blocked waiting for an event. Which task will FreeRTOS schedule next?
Solution
Step 1: Understand task states and scheduling
FreeRTOS schedules the highest priority task that is ready (not blocked).Step 2: Identify ready tasks
Task with priority 4 is blocked, so it cannot run now. Tasks with priority 3 and 2 are ready.Step 3: Choose highest priority ready task
Between priority 3 and 2, priority 3 is higher, so it runs next.Final Answer:
Task with priority 3 -> Option AQuick Check:
Highest ready priority = 3 [OK]
- Trying to run blocked task
- Ignoring task states
- Assuming no task runs if highest priority blocked
Ready to prove it?
Pass the test and this pattern moves unknown
