0
0
FreeRTOSprogramming~20 mins

Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION) in FreeRTOS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Static vs Dynamic 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 this FreeRTOS code snippet where configSUPPORT_STATIC_ALLOCATION is set to 1. What will be the output when the task is created?

FreeRTOS
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;
}
ATask created successfully
BTask creation failed
CCompilation error due to missing heap
DRuntime error due to NULL stack pointer
Attempts:
2 left
💡 Hint

Static allocation requires providing buffers explicitly. Check if buffers are correctly passed.

Predict Output
intermediate
2:00remaining
What happens if configSUPPORT_STATIC_ALLOCATION is 0 and xTaskCreateStatic is called?

Assuming configSUPPORT_STATIC_ALLOCATION is set to 0 in FreeRTOSConfig.h, what will happen if the following code is run?

FreeRTOS
StaticTask_t xTaskBuffer;
StackType_t xStack[100];

TaskHandle_t xHandle = xTaskCreateStatic(
    vTaskCode,
    "Task1",
    100,
    NULL,
    1,
    xStack,
    &xTaskBuffer
);
ACompilation error because xTaskCreateStatic is undefined
BTask is created successfully using static buffers
CRuntime error due to NULL heap
DTask is created dynamically ignoring static buffers
Attempts:
2 left
💡 Hint

Check if the static allocation API is available when the config option is disabled.

🔧 Debug
advanced
3:00remaining
Why does this statically allocated task crash at runtime?

Given this FreeRTOS task creation code with static allocation enabled, the system crashes at runtime. Identify the cause.

FreeRTOS
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;
}
ATask priority is invalid causing scheduler failure
BStack size is too small causing overflow
CpxTaskBuffer and pxStackBuffer are uninitialized pointers causing invalid memory access
DvTaskStartScheduler is called before task creation
Attempts:
2 left
💡 Hint

Check if the buffers passed to xTaskCreateStatic are valid memory locations.

🧠 Conceptual
advanced
2:00remaining
Which statement about static vs dynamic allocation in FreeRTOS is TRUE?

Choose the correct statement regarding static and dynamic allocation in FreeRTOS when configSUPPORT_STATIC_ALLOCATION is enabled.

ADynamic allocation is only possible if static allocation is disabled
BStatic allocation requires the user to provide memory buffers for task control blocks and stacks
CStatic allocation automatically allocates memory from the heap at runtime
DDynamic allocation requires the user to provide buffers explicitly
Attempts:
2 left
💡 Hint

Think about who manages memory in static vs dynamic allocation.

Predict Output
expert
3:00remaining
What is the number of items in the task list after these mixed allocations?

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?
A3
B1
C0
D2
Attempts:
2 left
💡 Hint

Both static and dynamic task creation APIs add tasks to the scheduler.