Bird
Raised Fist0
FreeRTOSprogramming~20 mins

xTaskCreate() function in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS xTaskCreate 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 creation code?

Consider the following FreeRTOS code snippet that creates a task. What will be the output printed by the task?

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

void vTaskCode(void * pvParameters) {
    printf("Task running with parameter: %d\n", (int)(intptr_t)pvParameters);
    vTaskDelete(NULL);
}

int main() {
    BaseType_t xReturned;
    TaskHandle_t xHandle = NULL;

    xReturned = xTaskCreate(
                    vTaskCode,       /* Function that implements the task. */
                    "Task1",         /* Text name for the task. */
                    1000,             /* Stack size in words, not bytes. */
                    (void *)42,       /* Parameter passed into the task. */
                    1,                /* Priority at which the task is created. */
                    &xHandle);        /* Used to pass out the created task's handle. */

    if(xReturned == pdPASS) {
        vTaskStartScheduler();
    } else {
        printf("Task creation failed!\n");
    }
    return 0;
}
ATask running with parameter: 0
BTask running with parameter: 42
CTask creation failed!
DCompilation error due to cast
Attempts:
2 left
💡 Hint

Look at the parameter passed to the task and how it is printed.

🧠 Conceptual
intermediate
1:30remaining
What does the 'uxPriority' parameter in xTaskCreate() control?

In the xTaskCreate() function, what is the role of the 'uxPriority' parameter?

AIt specifies the maximum number of times the task can run.
BIt defines the stack size allocated to the task in bytes.
CIt sets the priority level of the created task, determining its scheduling order.
DIt is used to pass parameters to the task function.
Attempts:
2 left
💡 Hint

Think about how FreeRTOS decides which task to run first.

🔧 Debug
advanced
2:00remaining
Why does this xTaskCreate() call fail to create a task?

Examine the following code snippet. Why does the task creation fail?

FreeRTOS
void vTaskCode(void * pvParameters) {
    for(;;) {}
}

int main() {
    BaseType_t xReturned;
    TaskHandle_t xHandle = NULL;

    xReturned = xTaskCreate(
                    vTaskCode,
                    "Task2",
                    0,          /* Stack size is zero */
                    NULL,
                    1,
                    &xHandle);

    if(xReturned == pdPASS) {
        vTaskStartScheduler();
    } else {
        printf("Task creation failed due to stack size!\n");
    }
    return 0;
}
ABecause the stack size parameter is zero, which is invalid.
BBecause the task function does not call vTaskDelete().
CBecause the task priority is set to 1, which is too low.
DBecause the task handle pointer is NULL.
Attempts:
2 left
💡 Hint

Check the stack size argument passed to xTaskCreate().

📝 Syntax
advanced
1:30remaining
Which xTaskCreate() call is syntactically correct?

Identify the syntactically correct xTaskCreate() call from the options below.

FreeRTOS
void vTaskCode(void * pvParameters) { for(;;) {} }
AxTaskCreate(vTaskCode, "Task", 1000, NULL, 2, NULL);
BxTaskCreate(vTaskCode, Task, 1000, NULL, 2, NULL);
CxTaskCreate(vTaskCode, "Task", 1000, NULL, "2", NULL);
DxTaskCreate(vTaskCode, "Task", 1000, NULL, 2);
Attempts:
2 left
💡 Hint

Check the types of each argument required by xTaskCreate().

🚀 Application
expert
2:30remaining
How many tasks are created and running after this code executes?

Given the following code, how many tasks will be created and running after vTaskStartScheduler() is called?

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

void vTaskCode(void * pvParameters) {
    printf("Task %d started\n", (int)(intptr_t)pvParameters);
    vTaskDelete(NULL);
}

int main() {
    BaseType_t xReturned;
    TaskHandle_t xHandle1 = NULL, xHandle2 = NULL;

    xReturned = xTaskCreate(vTaskCode, "Task1", 1000, (void *)1, 1, &xHandle1);
    if(xReturned != pdPASS) {
        printf("Failed to create Task1\n");
    }

    xReturned = xTaskCreate(vTaskCode, "Task2", 1000, (void *)2, 1, &xHandle2);
    if(xReturned != pdPASS) {
        printf("Failed to create Task2\n");
    }

    vTaskStartScheduler();
    return 0;
}
ANo tasks run because the scheduler is never started.
BOnly one task runs because the second creation fails.
CTwo tasks are created but never run because of missing infinite loops.
DTwo tasks are created and run briefly before deleting themselves.
Attempts:
2 left
💡 Hint

Look at how tasks delete themselves after printing and the scheduler start.

Practice

(1/5)
1. What is the main purpose of the xTaskCreate() function in FreeRTOS?
easy
A. To start a new task that runs concurrently with others
B. To delete an existing task
C. To pause the scheduler temporarily
D. To allocate memory for variables

Solution

  1. Step 1: Understand the role of xTaskCreate()

    This function is designed to create and start a new task in FreeRTOS, allowing multitasking.
  2. Step 2: Compare with other options

    Deleting tasks, pausing the scheduler, or allocating memory are handled by different functions, not xTaskCreate().
  3. Final Answer:

    To start a new task that runs concurrently with others -> Option A
  4. Quick Check:

    Task creation = start new task [OK]
Hint: xTaskCreate always starts a new task [OK]
Common Mistakes:
  • Confusing task creation with task deletion
  • Thinking it pauses the scheduler
  • Assuming it allocates general memory
2. Which of the following is the correct syntax for xTaskCreate() in FreeRTOS?
easy
A. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority);
B. xTaskCreate(TaskFunction, Name, StackSize, Parameters, Priority, TaskHandle);
C. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, TaskHandle);
D. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle);

Solution

  1. Step 1: Recall the correct parameter list

    The correct syntax requires the task function, a string name in quotes, stack size, parameters, priority, and a pointer to the task handle.
  2. Step 2: Check each option carefully

    xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); matches the correct syntax exactly, including quotes around the name and the address-of operator for the task handle.
  3. Final Answer:

    xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); -> Option D
  4. Quick Check:

    Correct syntax includes &TaskHandle [OK]
Hint: Task name must be a string and handle passed by address [OK]
Common Mistakes:
  • Forgetting quotes around the task name
  • Passing task handle without & operator
  • Omitting the task handle parameter
3. Consider the following code snippet:
void vTaskCode(void * pvParameters) {
    for(;;) {
        // Task code here
    }
}

int main() {
    TaskHandle_t xHandle = NULL;
    BaseType_t result = xTaskCreate(vTaskCode, "Task1", 1000, NULL, 2, &xHandle);
    if(result == pdPASS) {
        // Task created successfully
    }
    return 0;
}

What is the value of result if the task is created successfully?
medium
A. pdPASS
B. pdFAIL
C. NULL
D. 1

Solution

  1. Step 1: Understand xTaskCreate() return values

    This function returns pdPASS if the task was created successfully, otherwise pdFAIL.
  2. Step 2: Analyze the code logic

    The code checks if result == pdPASS to confirm success, so result must be pdPASS on success.
  3. Final Answer:

    pdPASS -> Option A
  4. Quick Check:

    Success return = pdPASS [OK]
Hint: Success returns pdPASS, failure returns pdFAIL [OK]
Common Mistakes:
  • Confusing pdPASS with pdFAIL
  • Assuming NULL or 1 as success
  • Not checking the return value
4. Identify the error in this xTaskCreate() usage:
TaskHandle_t xHandle;
BaseType_t result = xTaskCreate(vTaskCode, "Task2", 500, NULL, 1, xHandle);
medium
A. Priority must be zero
B. Task name should not be in quotes
C. Missing & before xHandle in last parameter
D. Stack size is too large

Solution

  1. Step 1: Check the task handle parameter

    The last parameter must be a pointer to the task handle variable, so it requires the address operator &.
  2. Step 2: Verify other parameters

    The task name is correctly a string in quotes, stack size 500 is valid, and priority 1 is allowed.
  3. Final Answer:

    Missing & before xHandle in last parameter -> Option C
  4. Quick Check:

    Task handle needs & operator [OK]
Hint: Always pass &TaskHandle, not TaskHandle alone [OK]
Common Mistakes:
  • Passing task handle without &
  • Removing quotes from task name
  • Incorrect assumptions about stack size or priority
5. You want to create two tasks with different priorities using xTaskCreate(). Which code snippet correctly creates both tasks and stores their handles?
hard
A. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 5, handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 3, handle2);
B. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2);
C. TaskHandle_t handle1, handle2; xTaskCreate(Task1, Task1, 1000, NULL, 3, &handle1); xTaskCreate(Task2, Task2, 1000, NULL, 5, &handle2);
D. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle2); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle1);

Solution

  1. Step 1: Verify task handle usage

    Each task handle must be passed by address using &. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); correctly does this for both handles.
  2. Step 2: Check task names and priorities

    Task names are strings in quotes. Priorities differ (3 and 5) as required. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); matches this correctly.
  3. Step 3: Confirm no parameter mix-ups

    TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 5, handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 3, handle2); misses & operator. TaskHandle_t handle1, handle2; xTaskCreate(Task1, Task1, 1000, NULL, 3, &handle1); xTaskCreate(Task2, Task2, 1000, NULL, 5, &handle2); uses function names instead of strings for task names. TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle2); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle1); swaps handles incorrectly.
  4. Final Answer:

    TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); -> Option B
  5. Quick Check:

    Correct handles and priorities = TaskHandle_t handle1, handle2; xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1); xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); [OK]
Hint: Pass &handle for each task, use quoted names, set priorities properly [OK]
Common Mistakes:
  • Passing handles without &
  • Using function names instead of string names
  • Mixing up handles between tasks
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