Why RTOS over bare-metal in FreeRTOS - Performance Analysis
When choosing between RTOS and bare-metal programming, it's important to understand how task management affects execution time.
We want to see how the system's work grows as tasks increase or become more complex.
Analyze the time complexity of task switching in an RTOS compared to a bare-metal loop.
// Bare-metal main loop
while(1) {
task1();
task2();
task3();
}
// RTOS task scheduler snippet
void vTaskSwitchContext() {
// Select next ready task
// Save context of current task
// Load context of next task
}
The bare-metal code runs tasks one after another in a fixed loop. The RTOS scheduler switches between tasks based on priority and readiness.
Look at what repeats as the system runs.
- Primary operation: Task execution and context switching
- How many times: Bare-metal runs tasks sequentially every loop; RTOS switches tasks whenever needed, which can be many times per second
As the number of tasks increases, how does the work change?
| Number of Tasks (n) | Approx. Operations per Cycle |
|---|---|
| 3 | Bare-metal: 3 tasks; RTOS: 3 tasks + context switches |
| 10 | Bare-metal: 10 tasks; RTOS: 10 tasks + more context switches |
| 100 | Bare-metal: 100 tasks; RTOS: 100 tasks + many context switches |
Pattern observation: Bare-metal work grows linearly with tasks; RTOS adds overhead from switching, which grows with task count and frequency.
Time Complexity: O(n)
This means the total work grows roughly in direct proportion to the number of tasks running.
[X] Wrong: "RTOS always makes the system slower because of overhead."
[OK] Correct: RTOS overhead exists but helps manage many tasks efficiently, making complex systems easier and more reliable.
Understanding how task management affects time helps you explain design choices clearly and shows you grasp system behavior beyond code.
What if we added task priorities and interrupts? How would the time complexity change?