A graceful shutdown sequence helps your FreeRTOS system stop tasks and clean up resources safely before turning off or restarting.
Graceful shutdown sequence in FreeRTOS
void graceful_shutdown(void) {
// Notify tasks to stop
// Wait for tasks to finish
// Close peripherals
// Save data if needed
// Stop scheduler or enter low power mode
}This is a general structure; actual implementation depends on your tasks and hardware.
Use task notifications or flags to tell tasks to stop work.
void graceful_shutdown(void) {
// Signal tasks to stop
xTaskNotifyGive(taskHandle1);
xTaskNotifyGive(taskHandle2);
// Wait for tasks to confirm they stopped
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// Close peripherals
close_uart();
close_spi();
// Save data
save_settings();
// Stop scheduler
vTaskEndScheduler();
}void graceful_shutdown(void) {
// Set a global flag
shutdown_requested = true;
// Tasks check this flag and stop themselves
// Wait some time for tasks to finish
vTaskDelay(pdMS_TO_TICKS(500));
// Power down hardware
power_down_peripherals();
// Enter low power mode
enter_sleep_mode();
}This program creates a worker task that runs and prints a message repeatedly. A shutdown task delays 500ms then calls graceful_shutdown(), which sets a flag to stop the worker task, waits for it to finish, then ends the scheduler.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> static TaskHandle_t taskHandle; static volatile int shutdown_requested = 0; void vTaskFunction(void *pvParameters) { while (!shutdown_requested) { // Do some work printf("Task running...\n"); vTaskDelay(pdMS_TO_TICKS(200)); } printf("Task stopping gracefully.\n"); vTaskDelete(NULL); } void vShutdownTask(void *pvParameters) { vTaskDelay(pdMS_TO_TICKS(500)); graceful_shutdown(); } void graceful_shutdown(void) { shutdown_requested = 1; // Wait for task to stop vTaskDelay(pdMS_TO_TICKS(500)); printf("Shutdown complete.\n"); vTaskEndScheduler(); } int main(void) { xTaskCreate(vTaskFunction, "Worker", 1000, NULL, 1, &taskHandle); xTaskCreate(vShutdownTask, "Shutdown", 1000, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Always ensure tasks check for shutdown signals regularly to stop quickly.
Use vTaskEndScheduler() only if your port supports it; some FreeRTOS ports do not.
FreeRTOS does not provide a built-in shutdown function; you must design your own sequence.
Graceful shutdown lets tasks stop safely and hardware close properly.
Use flags or notifications to tell tasks to stop work.
Wait for tasks to finish before powering down or stopping the scheduler.
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
