Why scheduling determines real-time behavior in FreeRTOS - Performance Analysis
In FreeRTOS, scheduling decides which task runs and when. This affects how fast tasks respond in real time.
We want to see how scheduling steps grow as more tasks are added.
Analyze the time complexity of the FreeRTOS scheduler selecting the next task.
// Simplified scheduler loop
for (int priority = MAX_PRIORITY; priority >= 0; priority--) {
if (readyTasks[priority] != NULL) {
runTask(readyTasks[priority]);
break;
}
}
This code checks tasks from highest to lowest priority to pick the next one to run.
Look at what repeats when the scheduler runs.
- Primary operation: Loop through priorities to find a ready task.
- How many times: Up to the number of priority levels (MAX_PRIORITY + 1).
As the number of priority levels grows, the scheduler checks more steps.
| Input Size (priority levels) | Approx. Operations |
|---|---|
| 10 | Up to 11 checks |
| 100 | Up to 101 checks |
| 1000 | Up to 1001 checks |
Pattern observation: The number of checks grows directly with the number of priority levels.
Time Complexity: O(n)
This means the scheduler's work grows linearly with the number of priority levels it must check.
[X] Wrong: "Scheduler always picks the next task instantly, no matter how many tasks exist."
[OK] Correct: The scheduler must check priorities one by one, so more priorities mean more checks and longer decision time.
Understanding how scheduling time grows helps you explain real-time system behavior clearly and confidently.
"What if the scheduler used a data structure to find the highest priority ready task faster? How would the time complexity change?"