Post

Schedulers in RTOS

Schedulers in RTOS

Schedulers in RTOS

Nice, let’s go deep into Schedulers in RTOS. Think of scheduler as the traffic police of the CPU – it decides which task runs and when.


What is a Scheduler?

  • In RTOS, multiple tasks exist, but only one runs on CPU at a time.
  • The scheduler decides which task gets CPU based on priority, state, and algorithm.
  • FreeRTOS uses a priority-based preemptive scheduler (but can be configured).

Types of Schedulers in RTOS

1. Cooperative Scheduling

  • A running task must yield (give up CPU) voluntarily.
  • No preemption – task runs until it finishes or calls taskYIELD().
  • Simple but risky: if a task never yields, others starve.

Example (FreeRTOS with configUSE_PREEMPTION=0):

1
2
3
4
5
for(;;)
{
    // Do work
    taskYIELD(); // must call this to let others run
}

2. Preemptive Scheduling

  • RTOS forces context switch if a higher-priority task becomes ready.
  • Ensures real-time responsiveness.
  • Default in FreeRTOS (configUSE_PREEMPTION=1).

3. Round Robin Scheduling (within same priority)

  • If multiple tasks share same priority, FreeRTOS gives CPU in time slices.
  • Controlled by configUSE_TIME_SLICING.

Example:

  • Task A and Task B both priority 2.
  • They take turns in running when time slice expires.

4. Priority-Based Scheduling (Main type in FreeRTOS)

  • Always runs the highest priority ready task.
  • If multiple with same priority → round robin.

5. Rate Monotonic Scheduling (RMS)

  • A fixed-priority algorithm:

    • Shorter period task → higher priority.
  • Common in hard real-time systems.
  • FreeRTOS doesn’t implement RMS directly, but you can assign priorities that way.

6. Earliest Deadline First (EDF)

  • Dynamic priority: task with the earliest deadline runs first.
  • Not built into FreeRTOS, but possible with custom scheduler.
  • Used in hard real-time apps like medical devices, automotive.

Summary Table

Scheduler TypePreemptionWho decides CPU use?Use Case
CooperativeNoTask itselfSimpler, non-critical apps
Preemptive (FreeRTOS)YesScheduler (priority)Most RTOS apps
Round RobinYes (same priority)Scheduler (time slice)Fairness among same-level tasks
Rate Monotonic (RMS)YesFixed priority (period-based)Hard real-time
Earliest Deadline First (EDF)YesDynamic deadline-basedHard real-time with varying deadlines

In FreeRTOS interviews, they usually expect you to know:

  • Preemptive vs Cooperative
  • Round robin for same priority tasks
  • How time slicing works

This post is licensed under CC BY 4.0 by the author.