Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Graceful shutdown sequence in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Shutdown Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS shutdown sequence?
Consider this FreeRTOS task code snippet that initiates a graceful shutdown. What will be printed to the console?
FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void vShutdownTask(void *pvParameters) {
    printf("Shutdown started\n");
    vTaskDelay(pdMS_TO_TICKS(100));
    printf("Closing resources\n");
    vTaskDelay(pdMS_TO_TICKS(100));
    printf("Shutdown complete\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(vShutdownTask, "ShutdownTask", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
A
Shutdown started
Closing resources
B
Shutdown started
Shutdown complete
Closing resources
C
Shutdown started
Closing resources
Shutdown complete
D
Shutdown complete
Closing resources
Shutdown started
Attempts:
2 left
💡 Hint
Think about the order of printf statements and the delays between them.
🧠 Conceptual
intermediate
1:30remaining
Which FreeRTOS function is best to notify tasks to start graceful shutdown?
In a FreeRTOS system, you want to signal multiple tasks to begin their shutdown routines. Which function is most appropriate for this notification?
AvTaskDelete()
BxTaskNotifyGive()
CvTaskDelay()
DxQueueSend()
Attempts:
2 left
💡 Hint
Consider a lightweight way to notify tasks without deleting them immediately.
🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS shutdown code cause a deadlock?
Analyze the following code snippet. Why does the system hang during shutdown?
FreeRTOS
SemaphoreHandle_t xResourceSemaphore;

void vShutdownTask(void *pvParameters) {
    xSemaphoreTake(xResourceSemaphore, portMAX_DELAY);
    // Clean up resource
    vTaskDelay(pdMS_TO_TICKS(200));
    xSemaphoreGive(xResourceSemaphore);
    vTaskDelete(NULL);
}

void vMainTask(void *pvParameters) {
    xSemaphoreTake(xResourceSemaphore, portMAX_DELAY);
    // Perform work
    vTaskDelay(pdMS_TO_TICKS(500));
    xSemaphoreGive(xResourceSemaphore);
    vTaskDelete(NULL);
}

int main() {
    xResourceSemaphore = xSemaphoreCreateMutex();
    xTaskCreate(vMainTask, "MainTask", 1000, NULL, 2, NULL);
    xTaskCreate(vShutdownTask, "ShutdownTask", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
AThe semaphore is never created properly, so both tasks block indefinitely.
BvTaskDelete(NULL) is called before releasing the semaphore causing resource leak.
CShutdownTask waits forever because MainTask holds the semaphore and delays for 500 ms, blocking ShutdownTask.
DDeadlock occurs because both tasks try to take the semaphore simultaneously causing priority inversion.
Attempts:
2 left
💡 Hint
Look at the semaphore creation in the main function.
📝 Syntax
advanced
2:00remaining
Which option correctly implements a graceful shutdown flag in FreeRTOS?
You want to use a global flag to signal tasks to shutdown gracefully. Which code snippet correctly declares and uses this flag?
FreeRTOS
volatile bool shutdownFlag = false;

void vTaskFunction(void *pvParameters) {
    while (!shutdownFlag) {
        // Task work
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    // Cleanup code
    vTaskDelete(NULL);
}
A
volatile int shutdownFlag = 0;

void vTaskFunction(void *pvParameters) {
    while (shutdownFlag != 1) {
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    vTaskDelete(NULL);
}
B
bool shutdownFlag = false;

void vTaskFunction(void *pvParameters) {
    while (!shutdownFlag) {
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    vTaskDelete(NULL);
}
C
volatile bool shutdownFlag;

void vTaskFunction(void *pvParameters) {
    while (!shutdownFlag) {
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    vTaskDelete(NULL);
}
D
volatile bool shutdownFlag = false;

void vTaskFunction(void *pvParameters) {
    while (shutdownFlag == false) {
        vTaskDelay(pdMS_TO_TICKS(100));
    }
    vTaskDelete(NULL);
}
Attempts:
2 left
💡 Hint
Check variable initialization and type correctness.
🚀 Application
expert
3:00remaining
How many tasks remain active after this shutdown sequence?
Given the following FreeRTOS code, how many tasks remain running after the shutdown sequence completes?
FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

volatile bool shutdownFlag = false;

void vWorkerTask(void *pvParameters) {
    while (!shutdownFlag) {
        vTaskDelay(pdMS_TO_TICKS(50));
    }
    printf("Worker task cleanup done\n");
    vTaskDelete(NULL);
}

void vMonitorTask(void *pvParameters) {
    vTaskDelay(pdMS_TO_TICKS(200));
    shutdownFlag = true;
    printf("Shutdown flag set\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(vWorkerTask, "Worker1", 1000, NULL, 2, NULL);
    xTaskCreate(vWorkerTask, "Worker2", 1000, NULL, 2, NULL);
    xTaskCreate(vMonitorTask, "Monitor", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
Consider when each task deletes itself after shutdownFlag is set.

Practice

(1/5)
1. What is the main purpose of a graceful shutdown sequence in FreeRTOS?
easy
A. To immediately stop all tasks without waiting
B. To restart the scheduler automatically
C. To allow tasks to finish their work safely before stopping
D. To delete all tasks without cleanup

Solution

  1. Step 1: Understand graceful shutdown concept

    Graceful shutdown means letting tasks complete their current work safely before stopping.
  2. 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.
  3. Final Answer:

    To allow tasks to finish their work safely before stopping -> Option C
  4. Quick Check:

    Graceful shutdown = safe task completion [OK]
Hint: Graceful shutdown means safe stop, not immediate kill [OK]
Common Mistakes:
  • Thinking shutdown means immediate task stop
  • Confusing restart with shutdown
  • Ignoring task cleanup before stopping
2. Which FreeRTOS function is commonly used to notify a task to start its shutdown process?
easy
A. vTaskDelete()
B. xTaskNotifyGive()
C. vTaskStartScheduler()
D. xQueueSend()

Solution

  1. Step 1: Identify notification function

    xTaskNotifyGive() is used to send a notification to a task, often to signal events like shutdown.
  2. Step 2: Eliminate other options

    vTaskDelete() deletes a task immediately, vTaskStartScheduler() starts the scheduler, xQueueSend() sends data to a queue, not a direct notification.
  3. Final Answer:

    xTaskNotifyGive() -> Option B
  4. Quick Check:

    Notify task to shutdown = xTaskNotifyGive() [OK]
Hint: Use xTaskNotifyGive() to signal tasks for shutdown [OK]
Common Mistakes:
  • Using vTaskDelete() to notify instead of delete
  • Confusing scheduler start with notification
  • Sending shutdown signal via queue instead of notification
3. Given the code below, what will be the output if the shutdown flag is set to true?
bool shutdownFlag = false;
void TaskFunction(void *pvParameters) {
  while (!shutdownFlag) {
    // Do work
  }
  printf("Task stopped safely\n");
}

int main() {
  shutdownFlag = true;
  TaskFunction(NULL);
  return 0;
}
medium
A. Task stopped safely
B. No output, infinite loop
C. Compilation error
D. Task continues running

Solution

  1. 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.
  2. Step 2: Check what happens after loop

    After the loop, the printf prints "Task stopped safely".
  3. Final Answer:

    Task stopped safely -> Option A
  4. Quick Check:

    shutdownFlag true skips loop, prints message [OK]
Hint: If shutdown flag true, loop exits and prints message [OK]
Common Mistakes:
  • Assuming loop runs infinitely
  • Confusing flag initial value
  • Expecting compilation error due to bool usage
4. Identify the error in this shutdown sequence code snippet:
void ShutdownTask(void *pvParameters) {
  vTaskDelete(NULL);
  // Cleanup code here
}
medium
A. Task should call vTaskStartScheduler() before deletion
B. vTaskDelete(NULL) deletes all tasks, causing crash
C. Missing call to vTaskSuspendAll() before deletion
D. Cleanup code runs after task deletion, so it never executes

Solution

  1. Step 1: Understand vTaskDelete behavior

    vTaskDelete(NULL) deletes the current task immediately, stopping execution at that point.
  2. Step 2: Check code after deletion

    Cleanup code after vTaskDelete(NULL) will never run because the task is deleted before it.
  3. Final Answer:

    Cleanup code runs after task deletion, so it never executes -> Option D
  4. Quick Check:

    vTaskDelete stops task immediately, no code after runs [OK]
Hint: Code after vTaskDelete(NULL) won't run [OK]
Common Mistakes:
  • Assuming cleanup runs after vTaskDelete
  • Thinking vTaskDelete(NULL) deletes all tasks
  • Confusing suspend with delete
5. You want to implement a graceful shutdown where multiple tasks stop safely before the scheduler stops. Which sequence is correct?
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
hard
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

  1. Step 1: Understand graceful shutdown steps

    First, signal tasks to stop by setting a shutdown flag or notification.
  2. Step 2: Wait for tasks to confirm they stopped

    Ensure tasks finish cleanup and confirm they stopped before stopping the scheduler.
  3. Step 3: Stop the scheduler safely

    Call vTaskEndScheduler() only after tasks have stopped to avoid abrupt termination.
  4. Final Answer:

    Set shutdown flag -> Wait for tasks to confirm stop -> Call vTaskEndScheduler() -> Option A
  5. Quick Check:

    Signal tasks -> wait -> stop scheduler = correct shutdown [OK]
Hint: Signal tasks first, wait, then stop scheduler [OK]
Common Mistakes:
  • Stopping scheduler before tasks finish
  • Deleting tasks immediately without cleanup
  • Setting flag after scheduler stops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard