0
0
FreeRTOSprogramming~20 mins

Graceful shutdown sequence in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 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.