Consider the following FreeRTOS code snippet that creates a task. What will be the output printed by the task?
#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; }
Look at the parameter passed to the task and how it is printed.
The task is created with parameter (void *)42, which is cast back to int inside the task and printed. So the output is "Task running with parameter: 42".
In the xTaskCreate() function, what is the role of the 'uxPriority' parameter?
Think about how FreeRTOS decides which task to run first.
The 'uxPriority' parameter sets the task's priority. Higher priority tasks run before lower priority ones.
Examine the following code snippet. Why does the task creation fail?
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;
}Check the stack size argument passed to xTaskCreate().
A stack size of zero is invalid and causes xTaskCreate() to fail.
Identify the syntactically correct xTaskCreate() call from the options below.
void vTaskCode(void * pvParameters) { for(;;) {} }Check the types of each argument required by xTaskCreate().
Option A uses correct types: function pointer, string, stack size (uint16_t), void pointer, priority (UBaseType_t), and task handle pointer.
Given the following code, how many tasks will be created and running after vTaskStartScheduler() is called?
#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; }
Look at how tasks delete themselves after printing and the scheduler start.
Both tasks are created successfully and run once, printing their message, then delete themselves.