Consider two tasks in FreeRTOS: TaskA with priority 2 and TaskB with priority 1. Both print a message in an infinite loop with a delay. What will be the order of printed messages?
void TaskA(void *pvParameters) {
for (;;) {
printf("TaskA running\n");
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void TaskB(void *pvParameters) {
for (;;) {
printf("TaskB running\n");
vTaskDelay(pdMS_TO_TICKS(100));
}
}
int main() {
xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
xTaskCreate(TaskB, "TaskB", 1000, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}Remember that FreeRTOS always runs the highest priority task that is ready.
TaskA has higher priority (2 > 1), so it runs first, prints 'TaskA running', then delays for 100ms (blocks). During TaskA's delay, TaskB (only ready task) runs, prints 'TaskB running', and delays for 100ms. When TaskA wakes, it has higher priority so runs next (preempting if necessary), prints again, and delays. Since delays are equal and print times are negligible, the output alternates: TaskA, TaskB, TaskA, TaskB, ...
In FreeRTOS, what problem occurs when a low priority task holds a resource needed by a high priority task, but a medium priority task preempts the low priority task?
Think about what happens when a medium priority task runs instead of the low priority task holding the resource.
Priority inversion happens when a low priority task holds a resource needed by a high priority task, but a medium priority task preempts the low priority task. This causes the high priority task to wait longer than expected because the low priority task cannot run to release the resource.
Given three tasks with priorities 3, 2, and 1, the lowest priority task never seems to run. What is the most likely cause?
xTaskCreate(TaskHigh, "High", 1000, NULL, 3, NULL); xTaskCreate(TaskMedium, "Medium", 1000, NULL, 2, NULL); xTaskCreate(TaskLow, "Low", 1000, NULL, 1, NULL); // All tasks run infinite loops without blocking or delaying
Consider how FreeRTOS schedules tasks based on priority and task states.
If higher priority tasks never block or delay, they will always run, preventing lower priority tasks from running. This causes starvation of the low priority task.
Which of the following calls correctly changes the priority of a task handle xTaskHandle to 5?
Check the FreeRTOS API naming conventions for setting task priority.
The correct API to set a task's priority is vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority). Option B matches this exactly.
You have a high priority task and a low priority task sharing a mutex. A medium priority task often preempts the low priority task, causing priority inversion. Which FreeRTOS feature can you use to prevent this?
Think about how FreeRTOS mutexes handle priority inheritance.
FreeRTOS mutexes created with xSemaphoreCreateMutex() implement priority inheritance. This means the low priority task holding the mutex temporarily inherits the higher priority of the blocked task, preventing medium priority tasks from preempting and causing priority inversion.