Consider this FreeRTOS code snippet where configSUPPORT_STATIC_ALLOCATION is set to 1. What will be the output when the task is created?
StaticTask_t xTaskBuffer; StackType_t xStack[100]; void vTaskCode(void * pvParameters) { for(;;) {} } int main() { TaskHandle_t xHandle = xTaskCreateStatic( vTaskCode, // Task function "Task1", // Name 100, // Stack size NULL, // Parameters 1, // Priority xStack, // Stack buffer &xTaskBuffer // Task buffer ); if (xHandle != NULL) { printf("Task created successfully\n"); } else { printf("Task creation failed\n"); } return 0; }
Static allocation requires providing buffers explicitly. Check if buffers are correctly passed.
When configSUPPORT_STATIC_ALLOCATION is enabled, xTaskCreateStatic creates a task using the provided stack and task buffers. Since buffers are valid, the task is created successfully.
Assuming configSUPPORT_STATIC_ALLOCATION is set to 0 in FreeRTOSConfig.h, what will happen if the following code is run?
StaticTask_t xTaskBuffer; StackType_t xStack[100]; TaskHandle_t xHandle = xTaskCreateStatic( vTaskCode, "Task1", 100, NULL, 1, xStack, &xTaskBuffer );
Check if the static allocation API is available when the config option is disabled.
If configSUPPORT_STATIC_ALLOCATION is 0, the static allocation API xTaskCreateStatic is not compiled in, causing a compilation error.
Given this FreeRTOS task creation code with static allocation enabled, the system crashes at runtime. Identify the cause.
StaticTask_t *pxTaskBuffer;
StackType_t *pxStackBuffer;
void createTask() {
TaskHandle_t xHandle = xTaskCreateStatic(
vTaskCode,
"TaskCrash",
100,
NULL,
1,
pxStackBuffer,
pxTaskBuffer
);
if (xHandle == NULL) {
printf("Task creation failed\n");
}
}
int main() {
createTask();
vTaskStartScheduler();
return 0;
}Check if the buffers passed to xTaskCreateStatic are valid memory locations.
The pointers pxTaskBuffer and pxStackBuffer are declared but never assigned valid memory. Passing them to xTaskCreateStatic causes undefined behavior and crashes.
Choose the correct statement regarding static and dynamic allocation in FreeRTOS when configSUPPORT_STATIC_ALLOCATION is enabled.
Think about who manages memory in static vs dynamic allocation.
Static allocation means the user provides memory buffers for tasks, while dynamic allocation uses the heap managed by FreeRTOS. Both can be enabled independently.
Assuming configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is 1, consider this code:
TaskHandle_t h1, h2; StaticTask_t xTaskBuffer; StackType_t xStack[50]; h1 = xTaskCreateStatic(vTaskCode, "StaticTask", 50, NULL, 1, xStack, &xTaskBuffer); h2 = NULL; xTaskCreate(vTaskCode, "DynamicTask", 50, NULL, 1, &h2); // After both tasks are created, how many tasks exist in the scheduler?
Both static and dynamic task creation APIs add tasks to the scheduler.
Both xTaskCreateStatic and xTaskCreate create tasks that are added to the scheduler. So after both calls, there are 2 tasks.