0
0
FreeRTOSprogramming~20 mins

Task handle usage in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Task 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 task handle code?

Consider the following FreeRTOS code snippet. What will be printed to the console?

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

TaskHandle_t xTaskHandle = NULL;

void vTaskFunction(void *pvParameters) {
    printf("Task running\n");
    vTaskDelete(NULL);
}

int main(void) {
    BaseType_t xReturned;
    xReturned = xTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, &xTaskHandle);
    if (xReturned == pdPASS && xTaskHandle != NULL) {
        printf("Task created successfully\n");
    } else {
        printf("Task creation failed\n");
    }
    return 0;
}
ANo output
BTask creation failed
CTask created successfully
DTask running
Attempts:
2 left
💡 Hint

Remember that xTaskCreate returns pdPASS if the task is created and the handle is set.

🧠 Conceptual
intermediate
1:30remaining
What does a TaskHandle_t represent in FreeRTOS?

In FreeRTOS, what is the purpose of a TaskHandle_t variable?

AIt is used to store task execution time.
BIt stores the priority level of a task.
CIt contains the stack size of a task.
DIt holds a reference to a created task for management.
Attempts:
2 left
💡 Hint

Think about how you can control or delete a specific task after creating it.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS code cause a runtime error?

Examine the code below. It tries to delete a task using its handle but causes a runtime error. What is the likely cause?

FreeRTOS
TaskHandle_t xTaskHandle = NULL;

void vTaskFunction(void *pvParameters) {
    for (;;) {
        // Task code
    }
}

int main(void) {
    xTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, &xTaskHandle);
    vTaskDelete(xTaskHandle);
    return 0;
}
AThe task handle is NULL because the task was not created successfully.
BDeleting the task immediately after creation before the scheduler runs causes an error.
CThe task function does not call vTaskDelete, causing a leak.
DThe stack size of 1000 is too small, causing overflow.
Attempts:
2 left
💡 Hint

Consider when tasks actually start running and when handles become valid for deletion.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a FreeRTOS task and stores its handle?

Choose the correct code snippet that creates a task and stores its handle in xTaskHandle.

AxTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, &xTaskHandle);
BxTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, *xTaskHandle);
CxTaskCreate(&vTaskFunction, "Task1", 1000, NULL, 1, &xTaskHandle);
DxTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, xTaskHandle);
Attempts:
2 left
💡 Hint

Remember the type of the last parameter and how to pass the address of a variable in C.

🚀 Application
expert
3:00remaining
How many tasks are running after this FreeRTOS code executes?

Given the code below, how many tasks are actively running after vTaskStartScheduler() is called?

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"

TaskHandle_t xTask1Handle = NULL;
TaskHandle_t xTask2Handle = NULL;

void vTask1(void *pvParameters) {
    for (;;) {}
}

void vTask2(void *pvParameters) {
    for (;;) {}
}

int main(void) {
    xTaskCreate(vTask1, "Task1", 1000, NULL, 2, &xTask1Handle);
    xTaskCreate(vTask2, "Task2", 1000, NULL, 3, &xTask2Handle);
    vTaskDelete(xTask1Handle);
    vTaskStartScheduler();
    return 0;
}
A1 task
B0 tasks
C2 tasks
D3 tasks
Attempts:
2 left
💡 Hint

Consider what happens when vTaskDelete is called before the scheduler starts.