Graceful shutdown sequence in FreeRTOS - Time & Space Complexity
When we stop a FreeRTOS system carefully, we want to know how long it takes as the number of tasks grows.
We ask: How does the shutdown time change when there are more tasks to stop?
Analyze the time complexity of the following code snippet.
void gracefulShutdown(void) {
for (int i = 0; i < numTasks; i++) {
vTaskDelete(taskHandles[i]);
}
// Additional cleanup here
}
This code stops each running task one by one to shut down the system safely.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all tasks to delete each one.
- How many times: Exactly once per task, so
numTaskstimes.
As the number of tasks grows, the time to stop them grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 task deletions |
| 100 | 100 task deletions |
| 1000 | 1000 task deletions |
Pattern observation: Doubling the tasks doubles the shutdown steps.
Time Complexity: O(n)
This means the shutdown time grows directly with the number of tasks to stop.
[X] Wrong: "Stopping all tasks happens instantly, no matter how many there are."
[OK] Correct: Each task must be stopped one by one, so more tasks mean more work and more time.
Understanding how shutdown time grows helps you design systems that stop safely and predictably, a skill valued in real projects.
"What if tasks could be stopped in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand graceful shutdown concept
Graceful shutdown means letting tasks complete their current work safely before stopping.Step 2: Compare options
Only To allow tasks to finish their work safely before stopping describes this safe stopping process; others describe abrupt or incorrect actions.Final Answer:
To allow tasks to finish their work safely before stopping -> Option CQuick Check:
Graceful shutdown = safe task completion [OK]
- Thinking shutdown means immediate task stop
- Confusing restart with shutdown
- Ignoring task cleanup before stopping
Solution
Step 1: Identify notification function
xTaskNotifyGive() is used to send a notification to a task, often to signal events like shutdown.Step 2: Eliminate other options
vTaskDelete() deletes a task immediately, vTaskStartScheduler() starts the scheduler, xQueueSend() sends data to a queue, not a direct notification.Final Answer:
xTaskNotifyGive() -> Option BQuick Check:
Notify task to shutdown = xTaskNotifyGive() [OK]
- Using vTaskDelete() to notify instead of delete
- Confusing scheduler start with notification
- Sending shutdown signal via queue instead of notification
bool shutdownFlag = false;
void TaskFunction(void *pvParameters) {
while (!shutdownFlag) {
// Do work
}
printf("Task stopped safely\n");
}
int main() {
shutdownFlag = true;
TaskFunction(NULL);
return 0;
}Solution
Step 1: Analyze the loop condition
The loop runs while shutdownFlag is false. Since shutdownFlag is set true before calling TaskFunction, the loop is skipped.Step 2: Check what happens after loop
After the loop, the printf prints "Task stopped safely".Final Answer:
Task stopped safely -> Option AQuick Check:
shutdownFlag true skips loop, prints message [OK]
- Assuming loop runs infinitely
- Confusing flag initial value
- Expecting compilation error due to bool usage
void ShutdownTask(void *pvParameters) {
vTaskDelete(NULL);
// Cleanup code here
}Solution
Step 1: Understand vTaskDelete behavior
vTaskDelete(NULL) deletes the current task immediately, stopping execution at that point.Step 2: Check code after deletion
Cleanup code after vTaskDelete(NULL) will never run because the task is deleted before it.Final Answer:
Cleanup code runs after task deletion, so it never executes -> Option DQuick Check:
vTaskDelete stops task immediately, no code after runs [OK]
- Assuming cleanup runs after vTaskDelete
- Thinking vTaskDelete(NULL) deletes all tasks
- Confusing suspend with delete
A) Set shutdown flag -> Wait for tasks to confirm stop -> Call vTaskEndScheduler() B) Call vTaskEndScheduler() -> Set shutdown flag -> Tasks stop C) Delete tasks immediately -> Set shutdown flag -> Stop scheduler D) Stop scheduler -> Set shutdown flag -> Tasks stop
Solution
Step 1: Understand graceful shutdown steps
First, signal tasks to stop by setting a shutdown flag or notification.Step 2: Wait for tasks to confirm they stopped
Ensure tasks finish cleanup and confirm they stopped before stopping the scheduler.Step 3: Stop the scheduler safely
Call vTaskEndScheduler() only after tasks have stopped to avoid abrupt termination.Final Answer:
Set shutdown flag -> Wait for tasks to confirm stop -> Call vTaskEndScheduler() -> Option AQuick Check:
Signal tasks -> wait -> stop scheduler = correct shutdown [OK]
- Stopping scheduler before tasks finish
- Deleting tasks immediately without cleanup
- Setting flag after scheduler stops
Ready to prove it?
Pass the test and this pattern moves unknown
