0
0
FreeRTOSprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Preemptive scheduling behavior
Task A running
Higher priority task ready?
NoContinue 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.