FreeRTOS Scheduler: Learn to Configure Scheduling Algorithm

In this tutorial, we will learn about the FreeRTOS scheduler. What type of scheduler FreeRTOS Kernel uses and how to configure it with different scheduling policy. First, we will define the term scheduler and how scheduling policy selects tasks from all available tasks which are in a ready state. 

Introduction to Scheduler 

All operating systems use schedulers to allocate CPU time to tasks or threads. Because only one process can run at a given time on the CPU in case of a single core processor. Low cost microcontroller comes with a single CPU only and FreeRTOS is specifically designed for resource and memory constrained microcontrollers. Hence, the scheduler provides the mechanism to decide which task should run on the CPU. 

A task can be in one of these four possible states such as running, ready, blocked, suspended. Only tasks that are in ready state can be picked by a scheduler depending on the scheduling policy. Furthermore, tasks that are in a blocked state ( waiting for events such as mutex or semaphores) or in suspended state can not be scheduled.  

Scheduling Policy 

A scheduling policy is an algorithm used by scheduler to decide which task to run on a processor. Different types of scheduling algorithms are used in real time operating systems. Mainly the scheduling algorithms are of two types time-slicing and preemptive. You can read more about scheduler here:

RTOS Scheduling Algorithms

FreeRTOS Scheduling Policy 

FreeRTOS kernel supports two types of scheduling policy: 

  1. Time Slicing Scheduling Policy: This is also known as a round-robin algorithm. In this algorithm, all equal priority tasks get CPU in equal portions of CPU time. 
  2. Fixed Priority Preemptive Scheduling: This scheduling algorithm selects tasks according to their priority. In other words, a high priority task always gets the CPU before a low priority task. A low priority task gets to execute only when there is no high priority task in the ready state. 

How to Configure FreeRTOS Scheduler

But in FreeRTOS, we usually use scheduling policy by mixing both the above-mentioned algorithms and it is known as Prioritized Preemptive Scheduling with Time Slicing.  

But you can select scheduling algorithms by setting corresponding configuration bits in an FreeRTOSConfig.h file. 

This  configUSE_PREEMPTION configuration constant is used to select fixed priority scheduling policy and configUSE_TIME_SLICING is used to select time slicing scheduler. But if you want to select both, you can set these configuration bit to one as shown in figure below: 

FreeRTOS scheduling algorithm selection

FreeRTOS Prioritized Preemptive Scheduling with Time Slicing

In this case, a scheduler can not change the priority of scheduled tasks. But tasks can change their own priority by using FreeRTOS priority changing API, you can read this article if you want to know how to change task priority during run time. 

Changing FreeRTOS tasks priority using Arduino

Because this scheduling algorithm also uses Preemptive scheduling, therefore, a high priority task can immediately preempt a low priority running task. Whenever a low priority task gets preempted by a high priority task, a low priority task enters the ready state and allows a high priority task to enter the running state. 

The last part of this scheduling algorithm is a time-slicing feature. In this case, all equal priority tasks get shared CPU processing time. Time slicing is achieved by using FreeRTOS tick interrupts. 

FreeRTOS Time sharing Preemptive Scheduling Example

This picture shows the timing diagram about the execution sequence of high, medium and low priority tasks.

FreeRTOS Preemptive scheduling algorithm example

This diagram shows the execution sequence of tasks according to time-slicing policy.

FreeRTOS Preemptive time slicing scheduling algorithm example

Further Examples:

Leave a Comment

eighteen + 18 =