0
0
FreeRTOSprogramming~20 mins

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

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