Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Preemptive scheduling behavior in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What happens in FreeRTOS when a higher priority task becomes ready while a lower priority task is running under preemptive scheduling?
easy
A. The scheduler ignores the higher priority task until the lower priority task yields.
B. The lower priority task continues running until it finishes.
C. Both tasks run at the same time.
D. The higher priority task immediately preempts the lower priority task and starts running.

Solution

  1. Step 1: Understand preemptive scheduling behavior

    In preemptive scheduling, the system always runs the highest priority task that is ready.
  2. Step 2: Apply to the scenario

    When a higher priority task becomes ready, it interrupts the lower priority task immediately.
  3. Final Answer:

    The higher priority task immediately preempts the lower priority task and starts running. -> Option D
  4. Quick Check:

    Preemptive scheduling = immediate switch to higher priority task [OK]
Hint: Higher priority tasks always interrupt lower ones immediately [OK]
Common Mistakes:
  • Thinking tasks run simultaneously on a single core
  • Believing lower priority tasks finish before switching
  • Confusing preemptive with cooperative scheduling
2. Which of the following is the correct way to enable preemptive scheduling in FreeRTOS configuration?
easy
A. #define configUSE_PREEMPTION 1
B. #define configENABLE_PREEMPTION true
C. #define configPREEMPTIVE_SCHEDULING 1
D. #define configUSE_PREEMPTION 0

Solution

  1. Step 1: Recall FreeRTOS config option for preemption

    The correct macro to enable preemption is configUSE_PREEMPTION.
  2. Step 2: Identify correct value to enable preemption

    Setting configUSE_PREEMPTION to 1 enables preemptive scheduling.
  3. Final Answer:

    #define configUSE_PREEMPTION 1 -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 1 enables preemption [OK]
Hint: Enable preemption by setting configUSE_PREEMPTION to 1 [OK]
Common Mistakes:
  • Using 0 instead of 1 to enable preemption
  • Using wrong macro names
  • Using boolean true instead of 1
3. Consider two tasks in FreeRTOS: TaskA with priority 2 and TaskB with priority 3. TaskA is running when TaskB becomes ready. What will be the output if both tasks print their names when running?
medium
A. TaskA prints first, then TaskB prints after TaskA finishes.
B. TaskB prints immediately, preempting TaskA.
C. Both tasks print simultaneously.
D. No output because tasks block each other.

Solution

  1. Step 1: Identify task priorities and running state

    TaskA has priority 2 and is running; TaskB has higher priority 3 and becomes ready.
  2. Step 2: Apply preemptive scheduling rules

    TaskB will preempt TaskA immediately and run first, printing its name.
  3. Final Answer:

    TaskB prints immediately, preempting TaskA. -> Option B
  4. Quick Check:

    Higher priority task runs first = TaskB prints first [OK]
Hint: Higher priority task prints first by preempting lower priority [OK]
Common Mistakes:
  • Assuming lower priority task finishes before switching
  • Thinking tasks run at the same time
  • Ignoring task priorities
4. Given this FreeRTOS snippet, what is the error that prevents preemptive scheduling from working?
#define configUSE_PREEMPTION 0

void Task1(void *pvParameters) {
  while(1) {
    // Task code
  }
}

void Task2(void *pvParameters) {
  while(1) {
    // Task code
  }
}
medium
A. configUSE_PREEMPTION is set to 0, disabling preemption.
B. Tasks are missing priority assignments.
C. Task functions must return int, not void.
D. Tasks need to call vTaskDelay to allow switching.

Solution

  1. Step 1: Check configUSE_PREEMPTION value

    The macro is set to 0, which disables preemptive scheduling.
  2. Step 2: Understand effect on scheduling

    With preemption disabled, tasks will not be interrupted automatically by higher priority tasks.
  3. Final Answer:

    configUSE_PREEMPTION is set to 0, disabling preemption. -> Option A
  4. Quick Check:

    configUSE_PREEMPTION = 0 disables preemption [OK]
Hint: Preemption disabled if configUSE_PREEMPTION = 0 [OK]
Common Mistakes:
  • Thinking tasks must return int
  • Assuming missing priorities cause preemption failure
  • Believing vTaskDelay is required for preemption
5. You have three tasks: TaskX (priority 2), TaskY (priority 3), and TaskZ (priority 3). TaskX is running, then both TaskY and TaskZ become ready at the same time. Which task will FreeRTOS run first under preemptive scheduling, and why?
hard
A. TaskY runs first because it was created before TaskZ.
B. TaskZ runs first because it has the same priority as TaskY but higher than TaskX.
C. Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities.
D. TaskX continues running until it yields.

Solution

  1. Step 1: Identify task priorities and ready state

    TaskX priority 2 is running; TaskY and TaskZ both have priority 3 and become ready simultaneously.
  2. Step 2: Understand FreeRTOS behavior with equal priority tasks

    FreeRTOS uses time slicing (round-robin) between tasks of equal highest priority.
  3. Step 3: Determine which task runs first

    Either TaskY or TaskZ can run first; the scheduler switches between them fairly.
  4. Final Answer:

    Either TaskY or TaskZ runs first; FreeRTOS uses time slicing between equal priorities. -> Option C
  5. Quick Check:

    Equal priority tasks share CPU time via time slicing [OK]
Hint: Equal priority tasks share CPU time by time slicing [OK]
Common Mistakes:
  • Assuming creation order decides first run
  • Thinking lower priority task continues running
  • Believing one equal priority task always runs first
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard