0
0
FreeRTOSprogramming~10 mins

Multiple tasks running concurrently in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple tasks running concurrently
Start Scheduler
Task 1 Ready
Task 1 Running
Task 1 Blocked
Task 2 Ready
Task 2 Running
Task 2 Blocked
Back to Scheduler
The scheduler starts and switches between tasks that are ready to run, running one task at a time and switching when tasks block or yield.
Execution Sample
FreeRTOS
void Task1(void *pvParameters) {
  for(;;) {
    // Do work
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}

void Task2(void *pvParameters) {
  for(;;) {
    // Do work
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}
Two tasks run concurrently, each delaying for different times to simulate work and allow switching.
Execution Table
StepRunning TaskTask1 StateTask2 StateScheduler ActionOutput
1Task1RunningReadyStart Task1Task1 runs first
2Task1Blocked (delay)ReadySwitch to Task2Task1 delays 1000ms
3Task2Blocked (delay)RunningRun Task2Task2 runs
4Task2Blocked (delay)Blocked (delay)Both tasks delayedTask2 delays 500ms
5IdleReadyReadyTasks ready, switch to Task1Task1 delay over
6Task1RunningReadyRun Task1Task1 runs again
7Task1Blocked (delay)ReadySwitch to Task2Task1 delays again
8Task2Blocked (delay)RunningRun Task2Task2 runs again
9Task2Blocked (delay)Blocked (delay)Both delayedTask2 delays again
10IdleReadyReadyTasks ready, switch to Task1Cycle repeats
Exit----Scheduler runs indefinitely, tasks keep switching
💡 Scheduler runs forever, switching tasks as they block and become ready again
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7After Step 9Final
Task1 StateReadyRunningBlocked (delay)ReadyBlocked (delay)ReadyBlocked (delay)
Task2 StateReadyReadyRunningBlocked (delay)ReadyRunningBlocked (delay)
Key Moments - 3 Insights
Why does Task1 become blocked after running?
Because Task1 calls vTaskDelay, it tells the scheduler to pause it for 1000ms, so it moves to Blocked state (see execution_table step 2).
How does the scheduler decide which task to run next?
The scheduler runs the highest priority task that is Ready. When Task1 blocks, Task2 is Ready and runs next (see steps 2 and 3).
What happens when both tasks are delayed at the same time?
Both tasks are Blocked, so the scheduler runs the Idle task until one task's delay expires (see step 4 and 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is Task2's state at Step 3?
AReady
BBlocked (delay)
CRunning
DSuspended
💡 Hint
Check the 'Task2 State' column at Step 3 in the execution_table.
At which step does Task1 become Ready again after its delay?
AStep 5
BStep 7
CStep 2
DStep 9
💡 Hint
Look for when Task1 state changes from Blocked to Ready in the execution_table.
If Task2's delay was increased to 2000ms, how would the scheduler behavior change?
ATask2 would run more often
BTask2 would block longer, Task1 runs more
CBoth tasks would block at the same time more often
DScheduler would stop switching tasks
💡 Hint
Consider how longer delay affects Task2's Blocked state duration in variable_tracker.
Concept Snapshot
FreeRTOS runs multiple tasks by switching between them.
Each task runs until it blocks or yields.
vTaskDelay pauses a task, letting others run.
Scheduler picks the highest priority Ready task.
When all tasks block, Idle task runs.
This creates the illusion of tasks running at the same time.
Full Transcript
In FreeRTOS, multiple tasks run concurrently by the scheduler switching between them. When the scheduler starts, it runs the first ready task. If a task calls vTaskDelay, it becomes blocked for that time, and the scheduler switches to another ready task. Tasks alternate running and blocking, allowing multitasking. When all tasks are blocked, the Idle task runs until a task becomes ready again. This process repeats indefinitely, creating concurrent task execution. The execution table shows step-by-step how Task1 and Task2 states change and how the scheduler switches between them. The variable tracker highlights the state changes of each task after each step. Key moments clarify why tasks block and how the scheduler chooses which task to run next. The visual quiz tests understanding of task states and scheduler behavior. The concept snapshot summarizes the multitasking mechanism in FreeRTOS.