Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Task handle usage in FreeRTOS - Practice Problems & Coding Challenges

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

Practice

(1/5)
1.

What is the main purpose of a task handle in FreeRTOS?

easy
A. To allocate memory for tasks
B. To store task priority values
C. To identify and control a specific task
D. To define the task's stack size

Solution

  1. Step 1: Understand what a task handle represents

    A task handle is a reference used to identify a task uniquely in FreeRTOS.
  2. Step 2: Identify the purpose of the handle

    The handle allows you to control the task, such as suspending or deleting it.
  3. Final Answer:

    To identify and control a specific task -> Option C
  4. Quick Check:

    Task handle = task control reference [OK]
Hint: Task handles let you control tasks by their ID [OK]
Common Mistakes:
  • Confusing handle with task priority
  • Thinking handle allocates memory
  • Mixing handle with stack size
2.

Which of the following is the correct way to declare a task handle variable in FreeRTOS?

?
easy
A. TaskHandle_t xTaskHandle;
B. TaskHandle xTaskHandle;
C. HandleTask_t xTaskHandle;
D. Task_Handle xTaskHandle;

Solution

  1. Step 1: Recall the FreeRTOS type for task handles

    The correct type defined by FreeRTOS for task handles is TaskHandle_t.
  2. Step 2: Check the syntax of declaration

    Declaring a variable as TaskHandle_t xTaskHandle; is the correct syntax.
  3. Final Answer:

    TaskHandle_t xTaskHandle; -> Option A
  4. Quick Check:

    Task handle type = TaskHandle_t [OK]
Hint: Use TaskHandle_t to declare task handles [OK]
Common Mistakes:
  • Using incorrect type names
  • Adding underscores wrongly
  • Confusing with other handle types
3.

What will be the output of the following code snippet?

TaskHandle_t xHandle = NULL;

void vTaskFunction(void *pvParameters) {
    for (;;) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

int main() {
    xTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, &xHandle);
    if (xHandle != NULL) {
        printf("Task created successfully\n");
    } else {
        printf("Task creation failed\n");
    }
    return 0;
}
medium
A. Task creation failed
B. Task created successfully
C. Compilation error due to missing includes
D. No output, program hangs

Solution

  1. Step 1: Analyze task creation and handle assignment

    The function xTaskCreate assigns the created task's handle to xHandle. If successful, xHandle is not NULL.
  2. Step 2: Check the condition and output

    The code checks if xHandle != NULL and prints "Task created successfully" if true.
  3. Final Answer:

    Task created successfully -> Option B
  4. Quick Check:

    xHandle != NULL means success [OK]
Hint: Check if handle is NULL to confirm task creation [OK]
Common Mistakes:
  • Assuming handle is NULL on success
  • Ignoring handle assignment
  • Confusing task creation return value
4.

Identify the error in the following code snippet related to task handle usage:

TaskHandle_t xTaskHandle;

void vTaskFunction(void *pvParameters) {
    for (;;) {
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

int main() {
    xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, xTaskHandle);
    vTaskSuspend(xTaskHandle);
    return 0;
}
medium
A. xTaskCreate should pass &xTaskHandle, not xTaskHandle
B. vTaskSuspend cannot suspend a task
C. Task handle must be initialized to NULL before use
D. Task priority is missing in xTaskCreate

Solution

  1. Step 1: Check how task handle is passed to xTaskCreate

    The last parameter of xTaskCreate expects a pointer to the task handle variable, so it should be &xTaskHandle.
  2. Step 2: Identify the error in the code

    The code passes xTaskHandle instead of its address, causing incorrect handle assignment.
  3. Final Answer:

    xTaskCreate should pass &xTaskHandle, not xTaskHandle -> Option A
  4. Quick Check:

    Pass handle address to xTaskCreate [OK]
Hint: Always pass &handle to xTaskCreate [OK]
Common Mistakes:
  • Passing handle instead of its address
  • Assuming vTaskSuspend needs no handle
  • Ignoring task priority parameter
5.

You want to create two tasks and suspend the first one after creation using task handles. Which code snippet correctly achieves this?

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

int main() {
    TaskHandle_t xHandle1 = NULL, xHandle2 = NULL;
    // Create tasks here
    // Suspend first task here
    return 0;
}
hard
A. xTaskCreate(vTask1, "Task1", 1000, NULL, 1, &xHandle1); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, &xHandle2); vTaskSuspend(NULL);
B. xTaskCreate(vTask1, "Task1", 1000, NULL, 1, xHandle1); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, xHandle2); vTaskSuspend(xHandle1);
C. xTaskCreate(vTask1, "Task1", 1000, NULL, 1, &xHandle1); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, &xHandle2); vTaskSuspend(&xHandle1);
D. xTaskCreate(vTask1, "Task1", 1000, NULL, 1, &xHandle1); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, &xHandle2); vTaskSuspend(xHandle1);

Solution

  1. Step 1: Correctly create tasks with handles

    Use &xHandle1 and &xHandle2 to store task handles during creation.
  2. Step 2: Suspend the first task using its handle

    Call vTaskSuspend(xHandle1); to suspend the first task by passing the handle itself, not its address or NULL.
  3. Final Answer:

    Correct creation with &xHandle and suspend with xHandle1 -> Option D
  4. Quick Check:

    Create with &handle, suspend with handle [OK]
Hint: Create tasks with &handle, suspend using handle [OK]
Common Mistakes:
  • Passing handle instead of &handle to xTaskCreate
  • Passing &handle to vTaskSuspend instead of handle
  • Suspending with NULL handle
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