Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Preemptive scheduling behavior in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Preemptive scheduling behavior
Task A running
↓
Higher priority task ready?
No→Continue Task A
Yes↓
Preempt Task A
↓
Switch to Higher Priority Task
↓
Higher Priority Task runs
↓
Higher Priority Task completes or blocks
↓
Resume Task A or next highest priority task
The scheduler checks if a higher priority task is ready. If yes, it stops the current task and switches to the higher priority one immediately.
Execution Sample
FreeRTOS
void vTaskA(void *pvParameters) {
  while(1) {
    // Task A work
  }
}

void vTaskB(void *pvParameters) {
  while(1) {
    // Higher priority task work
  }
}
Two tasks run: Task A (low priority) and Task B (high priority). When Task B becomes ready, it preempts Task A immediately.
Execution Table
StepCurrent TaskEventScheduler ActionRunning Task
1Task AStart systemRun Task ATask A
2Task ATask B becomes readyPreempt Task ATask B
3Task BTask B runsContinue Task BTask B
4Task BTask B blocks or finishesResume Task ATask A
5Task ANo higher priority readyContinue Task ATask A
💡 No higher priority tasks ready, Task A continues running
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Running TaskTask ATask BTask ATask A
Task B ReadyNoYesNoNo
Key Moments - 3 Insights
Why does Task A stop immediately when Task B becomes ready?
Because Task B has higher priority, the scheduler preempts Task A right away as shown in step 2 of the execution_table.
What happens if Task B blocks or finishes?
The scheduler resumes Task A or the next highest priority ready task, as shown in step 4.
Does Task A get to finish its current work before Task B runs?
No, preemptive scheduling interrupts Task A immediately when a higher priority task is ready, as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which task is running at step 3?
ATask A
BTask B
CNo task running
DBoth tasks running
💡 Hint
Check the 'Running Task' column at step 3 in the execution_table
At which step does the scheduler preempt Task A?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look for 'Preempt Task A' in the 'Scheduler Action' column
If Task B never becomes ready, what would the running task be after step 5?
ATask A
BNo task
CTask B
DBoth tasks
💡 Hint
Refer to the 'Running Task' variable in variable_tracker after final step
Concept Snapshot
Preemptive scheduling in FreeRTOS:
- Scheduler always runs highest priority ready task.
- If a higher priority task becomes ready, it preempts the current task immediately.
- The preempted task is paused and resumed later.
- Ensures critical tasks run without delay.
- Key for real-time responsiveness.
Full Transcript
In FreeRTOS preemptive scheduling, the system runs the highest priority task that is ready. When a higher priority task becomes ready while a lower priority task is running, the scheduler immediately stops the lower priority task and switches to the higher priority one. This is called preemption. The higher priority task runs until it blocks or finishes, then the scheduler resumes the lower priority task. This behavior ensures that important tasks get CPU time as soon as they need it. The execution table shows this step-by-step: Task A starts running, then Task B becomes ready and preempts Task A, runs, then Task A resumes after Task B blocks. Variables track which task is running and when Task B is ready. Common confusions include why Task A stops immediately and what happens when Task B finishes. The quiz questions help check understanding of these steps.

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