Bare-metal vs RTOS execution model in Embedded C - Performance Comparison
We want to understand how the execution time changes when using bare-metal code versus an RTOS in embedded C.
How does the program's running time grow as tasks or inputs increase?
Analyze the time complexity of these two simple execution models.
// Bare-metal loop
while(1) {
task1();
task2();
task3();
}
// RTOS task scheduler
void scheduler() {
while(1) {
if(task1_ready) run_task1();
if(task2_ready) run_task2();
if(task3_ready) run_task3();
}
}
This code shows a bare-metal infinite loop running tasks one after another, and an RTOS scheduler checking and running ready tasks.
Look at what repeats in each model.
- Primary operation: The infinite loop running tasks repeatedly.
- How many times: The loop runs forever, repeating all tasks each cycle.
As the number of tasks grows, the time to complete one loop cycle grows too.
| Number of Tasks (n) | Approx. Operations per Loop |
|---|---|
| 3 | 3 task calls |
| 10 | 10 task calls |
| 100 | 100 task calls |
Pattern observation: The time to finish one loop grows roughly in direct proportion to the number of tasks.
Time Complexity: O(n)
This means the time to run all tasks once grows linearly as you add more tasks.
[X] Wrong: "Adding an RTOS scheduler makes the execution time constant regardless of tasks."
[OK] Correct: The scheduler still checks each task, so time grows with the number of tasks, just like bare-metal.
Understanding how task execution time grows helps you explain embedded system design choices clearly and confidently.
What if the RTOS used priority-based preemption instead of checking all tasks each loop? How would the time complexity change?
Practice
bare-metal execution model in embedded C?Solution
Step 1: Understand bare-metal execution
Bare-metal means the program runs directly on the hardware without an operating system.Step 2: Identify the execution style
Bare-metal typically uses a single main loop to control the program flow.Final Answer:
Code runs directly on hardware with a single main loop. -> Option CQuick Check:
Bare-metal = single main loop [OK]
- Confusing bare-metal with RTOS multitasking
- Thinking bare-metal requires an OS
- Assuming bare-metal uses task scheduling
Solution
Step 1: Recognize RTOS task creation syntax
In embedded C with RTOS like FreeRTOS, tasks are created usingxTaskCreate().Step 2: Match the correct function call
xTaskCreate(taskFunction, "Task", 1000, NULL, 1, NULL); usesxTaskCreate()with proper parameters to start a task.Final Answer:
xTaskCreate(taskFunction, "Task", 1000, NULL, 1, NULL); -> Option BQuick Check:
RTOS task start = xTaskCreate() [OK]
- Using infinite loops without RTOS task creation
- Calling non-existent functions like createThread()
- Starting tasks directly in main() without scheduler
int main() {
while(1) {
toggleLED();
}
return 0;
}What will happen when this runs?
Solution
Step 1: Analyze the infinite loop
Thewhile(1)loop runs forever, repeatedly callingtoggleLED().Step 2: Understand bare-metal behavior
Since there is no OS, the program stays in this loop, toggling the LED continuously.Final Answer:
The LED toggles continuously without stopping. -> Option AQuick Check:
Infinite loop toggles LED forever [OK]
- Thinking tasks are created automatically
- Assuming program ends after one toggle
- Expecting runtime errors without cause
void taskLED(void *params) {
toggleLED();
}
int main() {
xTaskCreate(taskLED, "LED", 1000, NULL, 1, NULL);
vTaskStartScheduler();
while(1) {}
}What is the main problem?
Solution
Step 1: Check the task function structure
The task function runstoggleLED()once and then returns, ending the task immediately.Step 2: Understand RTOS task lifetime
RTOS tasks must have an infinite loop to keep running and performing actions repeatedly.Final Answer:
The task function lacks an infinite loop to keep running. -> Option AQuick Check:
RTOS tasks need infinite loops [OK]
- Forgetting infinite loop in task function
- Assuming scheduler auto-repeats tasks
- Blaming LED function without checking task code
Solution
Step 1: Analyze project requirements
The project needs to handle sensor reading, motor control, and UART communication at the same time.Step 2: Match execution model to multitasking needs
RTOS supports multiple tasks running concurrently with a scheduler, ideal for complex multitasking.Final Answer:
RTOS, because it can run multiple tasks concurrently with scheduling. -> Option DQuick Check:
Multitasking needs = RTOS [OK]
- Choosing bare-metal for complex multitasking
- Thinking RTOS can't multitask
- Confusing virtual machines with RTOS requirements
Ready to prove it?
Pass the test and this pattern moves unknown
