0
0
FreeRTOSprogramming~20 mins

Choosing priorities for real applications in FreeRTOS - Practice Problems & Coding Challenges

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

Consider two tasks created with different priorities. What will be the output order of the print statements?

FreeRTOS
void Task1(void *pvParameters) {
    printf("Task 1 running\n");
    printf("Task 1 finished\n");
    vTaskDelete(NULL);
}

void Task2(void *pvParameters) {
    printf("Task 2 running\n");
    printf("Task 2 finished\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask 1 running\nTask 2 running\nTask 2 finished\nTask 1 finished\n
BTask 1 running\nTask 1 finished\nTask 2 running\nTask 2 finished\n
CTask 2 running\nTask 2 finished\nTask 1 running\nTask 1 finished\n
DTask 2 running\nTask 1 running\nTask 1 finished\nTask 2 finished\n
Attempts:
2 left
💡 Hint

Remember, higher priority tasks preempt lower priority ones immediately.

🧠 Conceptual
intermediate
1:30remaining
Which priority assignment best avoids priority inversion?

You have three tasks: A (high priority), B (medium priority), and C (low priority). Task C holds a resource needed by Task A. Which priority assignment helps avoid priority inversion?

AAssign all tasks the same priority.
BKeep Task C at the lowest priority always.
CAssign Task B the highest priority to preempt Task C.
DAssign Task C the highest priority temporarily while it holds the resource.
Attempts:
2 left
💡 Hint

Think about priority inheritance to prevent blocking.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS system freeze with these priorities?

Given three tasks with priorities 1, 2, and 3, the system freezes after startup. What is the likely cause?

FreeRTOS
void TaskLow(void *pvParameters) {
    while(1) {
        // Does some work
    }
}

void TaskMedium(void *pvParameters) {
    while(1) {
        // Does some work
    }
}

void TaskHigh(void *pvParameters) {
    while(1) {
        // Does some work
    }
}

int main() {
    xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL);
    xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL);
    xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATaskHigh is starving lower priority tasks causing system freeze.
BTaskLow has a blocking call causing deadlock.
CTaskMedium has a syntax error preventing scheduler start.
DStack sizes are too small causing overflow.
Attempts:
2 left
💡 Hint

Consider how higher priority tasks affect lower priority ones.

📝 Syntax
advanced
1:30remaining
Which option correctly sets a task priority in FreeRTOS?

Choose the correct way to set a task's priority to 5 after creation.

FreeRTOS
TaskHandle_t xHandle = NULL;
xTaskCreate(TaskFunction, "Task", 1000, NULL, 3, &xHandle);
// Set priority here
AxTaskSetPriority(xHandle, 5);
BvTaskPrioritySet(xHandle, 5);
CvTaskSetPriority(xHandle, 5);
DxTaskPriorityChange(xHandle, 5);
Attempts:
2 left
💡 Hint

Check the FreeRTOS API naming conventions.

🚀 Application
expert
3:00remaining
How many tasks will run concurrently with these priorities and delays?

Three tasks are created with priorities 4, 3, and 2. Task with priority 4 calls vTaskDelay(100) in its loop. How many tasks run concurrently during the delay?

FreeRTOS
void TaskHigh(void *pvParameters) {
    while(1) {
        // Do work
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

void TaskMid(void *pvParameters) {
    while(1) {
        // Do work
    }
}

void TaskLow(void *pvParameters) {
    while(1) {
        // Do work
    }
}

int main() {
    xTaskCreate(TaskHigh, "High", 1000, NULL, 4, NULL);
    xTaskCreate(TaskMid, "Mid", 1000, NULL, 3, NULL);
    xTaskCreate(TaskLow, "Low", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
AOnly one task runs during the delay: Mid.
BNo tasks run during the delay.
CAll three tasks run concurrently during the delay.
DTwo tasks run concurrently during the delay: Mid and Low.
Attempts:
2 left
💡 Hint

Remember that vTaskDelay makes the task blocked, allowing lower priority tasks to run.