0
0
FreeRTOSprogramming~20 mins

Task priority assignment in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Task Priority 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 priority code?

Consider the following FreeRTOS code snippet that creates two tasks with different priorities. What will be the output printed by the tasks?

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

void Task1(void *pvParameters) {
    printf("Task1 running with priority %lu\n", (unsigned long)uxTaskPriorityGet(NULL));
    vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
    printf("Task2 running with priority %lu\n", (unsigned long)uxTaskPriorityGet(NULL));
    vTaskDelete(NULL);
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask2 running with priority 2\nTask1 running with priority 3
BTask1 running with priority 2\nTask2 running with priority 3
CTask2 running with priority 3\nTask1 running with priority 2
DTask1 running with priority 3\nTask2 running with priority 2
Attempts:
2 left
💡 Hint

Remember that FreeRTOS runs the highest priority task that is ready first.

🧠 Conceptual
intermediate
1:30remaining
Which FreeRTOS API sets the priority of a running task?

In FreeRTOS, which function is used to change the priority of a task that is already running?

AvTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority)
BvTaskDelete(TaskHandle_t xTask)
CxTaskCreate(TaskFunction_t pxTaskCode, const char * const pcName, uint16_t usStackDepth, void *pvParameters, UBaseType_t uxPriority, TaskHandle_t *pxCreatedTask)
DuxTaskPriorityGet(TaskHandle_t xTask)
Attempts:
2 left
💡 Hint

Look for the function that explicitly mentions setting priority.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS task priority code cause starvation?

Examine the code below. Why does Task1 never get to run?

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

void Task1(void *pvParameters) {
    while(1) {
        printf("Task1 running\n");
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

void Task2(void *pvParameters) {
    while(1) {
        printf("Task2 running\n");
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask1 deletes itself immediately, so it never runs.
BTask1 has higher priority but calls vTaskDelay incorrectly.
CBoth tasks have the same priority causing a deadlock.
DTask2 has higher priority and never blocks, so Task1 starves.
Attempts:
2 left
💡 Hint

Think about task priorities and blocking behavior.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this FreeRTOS task creation code

Which option contains the correct syntax to create a task with priority 5?

AxTaskCreate(TaskFunction, Task, 1000, NULL, 5, NULL);
BxTaskCreate(TaskFunction, "Task", 1000, NULL, 5, NULL);
CxTaskCreate(TaskFunction, "Task", 1000, NULL, "5", NULL);
DxTaskCreate(TaskFunction, "Task", 1000, NULL, 5);
Attempts:
2 left
💡 Hint

Check the parameter types and count.

🚀 Application
expert
3:00remaining
How to dynamically change task priority to avoid starvation?

You have two tasks: TaskA with priority 3 and TaskB with priority 5. TaskB runs continuously and causes TaskA starvation. Which approach correctly changes TaskB's priority dynamically to allow TaskA to run?

AUse vTaskPrioritySet(NULL, 2) inside TaskB to lower its priority temporarily.
BUse vTaskPrioritySet(TaskAHandle, 6) to increase TaskA priority above TaskB.
CDelete TaskB and recreate it with lower priority.
DUse vTaskSuspend(TaskBHandle) to pause TaskB permanently.
Attempts:
2 left
💡 Hint

Think about temporarily lowering the running task's priority.