0
0
FreeRTOSprogramming~10 mins

Time-slicing for equal priority tasks in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Time-slicing for equal priority tasks
Start Scheduler
Ready Tasks with Equal Priority
Select Task 1
Run Task 1 for Time Slice
Time Slice Ends
Select Task 2
Run Task 2 for Time Slice
Time Slice Ends
... (repeat for all equal priority tasks)
Cycle repeats until tasks block or finish
... (repeat for all equal priority tasks)
The scheduler cycles through tasks of equal priority, giving each a fixed time slice to run before switching to the next, repeating this round-robin until tasks block or complete.
Execution Sample
FreeRTOS
void Task1(void *pvParameters) {
  for(;;) {
    // Task1 work
    vTaskDelay(10);
  }
}

void Task2(void *pvParameters) {
  for(;;) {
    // Task2 work
    vTaskDelay(10);
  }
}
Two tasks with equal priority run in turn, each getting a time slice before the scheduler switches to the other.
Execution Table
StepCurrent TaskTime Slice RemainingActionNext Task
1Task1TickCountToTimeSliceTask1 runsTask1
2Task10Time slice ends, switchTask2
3Task2TickCountToTimeSliceTask2 runsTask2
4Task20Time slice ends, switchTask1
5Task1TickCountToTimeSliceTask1 runsTask1
6Task10Time slice ends, switchTask2
7Task2TickCountToTimeSliceTask2 runsTask2
8Task20Time slice ends, switchTask1
9Task1TickCountToTimeSliceTask1 runsTask1
10Task10Time slice ends, switchTask2
11--Tasks block or finish, scheduler idles or switches-
💡 Tasks block or finish, so scheduler stops time-slicing and switches context accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
Current TaskTask1Task2Task1Task2Task1-
Time Slice RemainingTickCountToTimeSlice0TickCountToTimeSlice0TickCountToTimeSlice-
Key Moments - 2 Insights
Why does the scheduler switch tasks even if the current task is not blocked?
Because tasks have equal priority, the scheduler uses time-slicing to give each task a fixed time slice, then switches to the next task as shown in steps 2, 4, 6, etc.
What happens if a task blocks before its time slice ends?
The scheduler immediately switches to the next ready task, skipping the remaining time slice, which is why the exit note mentions tasks blocking or finishing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current task at step 4?
ATask1
BTask2
CIdle Task
DScheduler
💡 Hint
Check the 'Current Task' column at step 4 in the execution_table.
At which step does the time slice for Task1 end for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for when 'Time Slice Remaining' becomes 0 for Task1 in the execution_table.
If Task1 blocks early, how would the execution table change?
AScheduler would stop all tasks.
BScheduler would continue running Task1 until time slice ends.
CScheduler would immediately switch to Task2, skipping Task1's remaining time slice.
DScheduler would run Task1 and Task2 simultaneously.
💡 Hint
Refer to the key moment about task blocking and the exit note in the execution_table.
Concept Snapshot
Time-slicing lets FreeRTOS share CPU time fairly among equal priority tasks.
Each task runs for a fixed time slice.
When time slice ends, scheduler switches to next equal priority task.
If a task blocks, scheduler switches immediately.
This creates a round-robin execution pattern.
Helps prevent one task from starving others.
Full Transcript
In FreeRTOS, when multiple tasks have the same priority, the scheduler uses time-slicing to share CPU time fairly. It runs each task for a fixed time slice, then switches to the next task of equal priority. This cycle repeats, creating a round-robin pattern. If a task blocks or finishes early, the scheduler immediately switches to the next ready task, skipping the remaining time slice. This ensures all equal priority tasks get a chance to run without one blocking the others. The execution table shows how the scheduler switches tasks after each time slice ends, alternating between Task1 and Task2. Variables track which task is running and how much time slice remains. This method keeps the system responsive and fair.