0
0
FreeRTOSprogramming~20 mins

Priority-based scheduling in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Priority Scheduling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of task execution order with different priorities

Consider three FreeRTOS tasks with priorities 2, 3, and 1 respectively. Each task prints its name once when it runs. What is the order of printed task names?

FreeRTOS
void Task1(void *pvParameters) {
    printf("Task1\n");
    vTaskDelete(NULL);
}
void Task2(void *pvParameters) {
    printf("Task2\n");
    vTaskDelete(NULL);
}
void Task3(void *pvParameters) {
    printf("Task3\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 3, NULL);
    xTaskCreate(Task3, "Task3", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
ATask1\nTask3\nTask2\n
BTask2\nTask1\nTask3\n
CTask3\nTask1\nTask2\n
DTask1\nTask2\nTask3\n
Attempts:
2 left
💡 Hint

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

Predict Output
intermediate
2:00remaining
Output when two tasks have the same priority

Two FreeRTOS tasks have the same priority and both are ready to run. Each prints its name once. What is the possible output order?

FreeRTOS
void TaskA(void *pvParameters) {
    printf("TaskA\n");
    vTaskDelete(NULL);
}
void TaskB(void *pvParameters) {
    printf("TaskB\n");
    vTaskDelete(NULL);
}

int main() {
    xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
    xTaskCreate(TaskB, "TaskB", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
ANo output, deadlock
BTaskA\nTaskB\n
CTaskB\nTaskA\n
DEither TaskA\nTaskB\n or TaskB\nTaskA\n
Attempts:
2 left
💡 Hint

Tasks with the same priority are scheduled round-robin.

🔧 Debug
advanced
2:00remaining
Identify the cause of priority inversion

Given three tasks with priorities 3 (high), 2 (medium), and 1 (low). The low priority task holds a mutex needed by the high priority task. The medium priority task runs continuously. What problem occurs and why?

FreeRTOS
/*
TaskLow (priority 1) locks mutex and starts long processing
TaskHigh (priority 3) tries to lock mutex and blocks
TaskMedium (priority 2) runs continuously
*/
ANo problem, TaskHigh preempts others immediately
BDeadlock because TaskHigh and TaskLow wait on each other
CPriority inversion because TaskMedium prevents TaskLow from running and releasing mutex
DStarvation of TaskMedium because TaskHigh blocks it
Attempts:
2 left
💡 Hint

Think about how medium priority tasks affect low priority tasks holding resources needed by high priority tasks.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in task creation

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

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

Check the parameter types and count for xTaskCreate.

🚀 Application
expert
2:00remaining
Determine the number of context switches in priority scheduling

In a FreeRTOS system, three tasks with priorities 3, 2, and 1 are ready. The highest priority task runs for 5 ticks, then blocks. The next highest runs for 3 ticks, then blocks. The lowest priority task runs for 4 ticks. How many context switches occur in total?

A4
B3
C5
D6
Attempts:
2 left
💡 Hint

Count switches when tasks block and scheduler switches to next ready task.