0
0
FreeRTOSprogramming~20 mins

Preemptive scheduling behavior in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Preemptive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Preemptive Scheduling: Task Priority Impact

Consider two FreeRTOS tasks: TaskA with priority 2 and TaskB with priority 3. Both tasks are ready to run at the same time. TaskA starts running first. What happens next?

FreeRTOS
void TaskA(void *pvParameters) {
    for (;;) {
        // TaskA code
    }
}

void TaskB(void *pvParameters) {
    for (;;) {
        // TaskB code
    }
}

int main() {
    xTaskCreate(TaskA, "TaskA", 1000, NULL, 2, NULL);
    xTaskCreate(TaskB, "TaskB", 1000, NULL, 3, NULL);
    vTaskStartScheduler();
    return 0;
}
ATaskB immediately preempts TaskA and starts running because it has higher priority.
BTaskA and TaskB run in a round-robin fashion regardless of priority.
CTaskA continues running until it blocks or yields, then TaskB runs.
DBoth tasks run simultaneously on separate cores.
Attempts:
2 left
💡 Hint

Remember, in preemptive scheduling, the highest priority ready task runs immediately.

Predict Output
intermediate
2:00remaining
Effect of vTaskDelay on Preemptive Scheduling

In FreeRTOS, if a running task calls vTaskDelay(), what happens to the CPU scheduling?

FreeRTOS
void TaskExample(void *pvParameters) {
    for (;;) {
        // Do some work
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}
AThe scheduler stops all tasks until the delay expires.
BThe task yields but remains ready to run immediately after delay.
CThe task continues running without interruption.
DThe task blocks and the scheduler runs the highest priority ready task.
Attempts:
2 left
💡 Hint

Think about what happens when a task delays itself in FreeRTOS.

Predict Output
advanced
2:00remaining
Preemptive Scheduling with Equal Priority Tasks

Two tasks, Task1 and Task2, have the same priority in FreeRTOS. Both are ready to run. What is the expected behavior?

FreeRTOS
void Task1(void *pvParameters) {
    for (;;) {
        // Task1 code
    }
}

void Task2(void *pvParameters) {
    for (;;) {
        // Task2 code
    }
}

int main() {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 2, NULL);
    vTaskStartScheduler();
    return 0;
}
AOnly Task1 runs because it was created first.
BThe scheduler randomly picks one task to run forever.
CTasks run in strict alternation using time slicing if enabled.
DBoth tasks run simultaneously on different cores.
Attempts:
2 left
💡 Hint

Consider how FreeRTOS handles tasks with the same priority.

Predict Output
advanced
2:00remaining
Preemption Behavior with Interrupts and Task Priorities

In FreeRTOS, an interrupt service routine (ISR) makes a higher priority task ready to run. What happens immediately after the ISR completes?

FreeRTOS
void vAnISR(void) {
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    xTaskNotifyFromISR(xHighPriorityTaskHandle, 0, eNoAction, &xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
AThe higher priority task preempts the currently running task immediately after ISR.
BThe scheduler ignores the higher priority task until the next tick.
CThe ISR blocks the higher priority task from running until manually resumed.
DThe higher priority task runs only if the current task yields voluntarily.
Attempts:
2 left
💡 Hint

Think about how FreeRTOS handles context switches triggered by ISRs.

🧠 Conceptual
expert
3:00remaining
Preemptive Scheduling and Priority Inversion Scenario

In a FreeRTOS system, TaskLow (priority 1) holds a mutex needed by TaskHigh (priority 3). TaskMedium (priority 2) is ready to run. What problem can occur and how does FreeRTOS help prevent it?

ANo problem occurs; TaskHigh preempts all tasks immediately.
BPriority inversion occurs; FreeRTOS uses priority inheritance to temporarily raise TaskLow's priority.
CTaskMedium blocks TaskHigh indefinitely; FreeRTOS disables preemption to fix this.
DTaskLow is forced to release the mutex immediately; FreeRTOS kills TaskMedium.
Attempts:
2 left
💡 Hint

Think about what happens when a low priority task blocks a high priority task and a medium priority task runs.