0% found this document useful (0 votes)
181 views5 pages

Scheduling in FreeRTOS - Pre-Emptive Vs Co-Operative

The document explains the scheduler in FreeRTOS, which manages task execution based on priorities and states, enabling multitasking through either cooperative or preemptive scheduling. It details the mechanisms of both scheduling types, including how tasks yield control and how the scheduler prioritizes tasks, with specific APIs provided for implementation. Example code snippets illustrate the differences between preemptive and cooperative scheduling modes in FreeRTOS.

Uploaded by

dwaraknath2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views5 pages

Scheduling in FreeRTOS - Pre-Emptive Vs Co-Operative

The document explains the scheduler in FreeRTOS, which manages task execution based on priorities and states, enabling multitasking through either cooperative or preemptive scheduling. It details the mechanisms of both scheduling types, including how tasks yield control and how the scheduler prioritizes tasks, with specific APIs provided for implementation. Example code snippets illustrate the differences between preemptive and cooperative scheduling modes in FreeRTOS.

Uploaded by

dwaraknath2202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Scheduling in FreeRTOS

1 WHAT IS THE SCHEDULER IN FREERTOS?


- The “scheduler” in FreeRTOS is responsible for deciding which task should execute at any given
time. It manages task transitions between states like Ready, Running, Blocked, or Suspended, based
on system configuration and task priorities. The scheduler ensures multitasking, allowing multiple
tasks to run efficiently by controlling task execution through a scheduling policy.

2 SCHEDULING POLICIES IN FREERTOS


FreeRTOS can be configured to work in either *Co-operative* or *Pre-emptive* scheduling mode.
The scheduling policy determines how tasks are switched and which task gets CPU time.

2.1 CO-OPERATIVE SCHEDULING


- In *Co-operative scheduling*, tasks must voluntarily yield control of the CPU for other tasks to
run. The scheduler only switches tasks when the running task either explicitly yields using
taskYIELD() or enters the Blocked or Suspended state.

- In this mode, no task can be pre-empted by another task with the same or higher priority unless it
chooses to yield.

Key API:

void taskYIELD(void); // Forces the current task to yield.

2.2 PRE-EMPTIVE SCHEDULING


- “Pre-emptive scheduling” allows the FreeRTOS scheduler to automatically switch between tasks
based on priority. The highest-priority task that is ready to run will always be executed. If a higher-
priority task becomes ready (e.g., after an interrupt), it will pre-empt the currently running task.

- In addition, if tasks share the same priority, they will be switched using “Round-Robin
Scheduling”, where each task runs for a fixed amount of time (a time slice), ensuring fair usage of
CPU time.

Key API for enabling Pre-emptive Scheduling:

#define configUSE_PREEMPTION 1 // Enable pre-emption in FreeRTOSConfig.h


3 WHAT IS PRE-EMPTION?
- “Pre-emption” is the process where the currently running task is interrupted (or "pre-empted") if
a higher-priority task becomes ready to run. This ensures that the highest-priority tasks always get
immediate CPU time.

- If tasks have equal priority, FreeRTOS uses Round-Robin Scheduling to switch between them,
ensuring each task gets a fair share of CPU time.

3.1 ROUND ROBIN SCHEDULING


- In “Round-Robin Scheduling”, tasks of the same priority are given equal CPU time in a cyclic
order. Each task runs for a specified time slice, after which the scheduler switches to the next task.

- Example: If two tasks, A and B, have the same priority, Task A runs for one time slice, then Task B
runs for the next, and this pattern continues.

3.2 PRIORITY-BASED SCHEDULING


- In “Priority-Based Scheduling”, tasks with a higher priority are selected to run over lower-priority
tasks. The scheduler always gives the CPU to the highest-priority task that is in the ready state. If
tasks have the same priority, Round Robin Scheduling is applied among them.

4 WHAT IS CO-OPERATIVE SCHEDULING?


- “Co-operative scheduling” requires tasks to explicitly yield control of the CPU to allow other tasks
to run. A task continues to run until it either enters a blocked state (e.g., waiting for a delay or an
event) or voluntarily calls taskYIELD() to let the scheduler switch to another task.

- In this model, task switching is cooperative rather than enforced by the scheduler.

Co-operative Scheduling API:

void taskYIELD(void); // Manually yields the CPU.

5 EXAMPLE CODE FOR PRE-EMPTIVE AND CO-OPERATIVE SCHEDULING

5.1 EXAMPLE: PRE-EMPTIVE SCHEDULING


In pre-emptive mode, tasks switch automatically based on priority or time slices (for equal-priority
tasks).

// FreeRTOSConfig.h: Enable pre-emption


#define configUSE_PREEMPTION 1

// Task1 function

void vTask1(void *pvParameters) {

for(;;) {

// Task1 logic

printf("Task1 is running\n");

vTaskDelay(pdMS_TO_TICKS(1000)); // Block for 1 second

// Task2 function

void vTask2(void *pvParameters) {

for(;;) {

// Task2 logic

printf("Task2 is running\n");

vTaskDelay(pdMS_TO_TICKS(1000)); // Block for 1 second

int main(void) {

xTaskCreate(vTask1, "Task 1", 1000, NULL, 1, NULL);

xTaskCreate(vTask2, "Task 2", 1000, NULL, 1, NULL);

// Start the scheduler

vTaskStartScheduler();

for(;;);

5.2 EXAMPLE: CO-OPERATIVE SCHEDULING


In co-operative mode, tasks must yield voluntarily for other tasks to run.
// FreeRTOSConfig.h: Disable pre-emption

#define configUSE_PREEMPTION 0

// Task1 function

void vTask1(void *pvParameters) {

for(;;) {

// Task1 logic

printf("Task1 is running\n");

vTaskDelay(pdMS_TO_TICKS(1000)); // Block for 1 second

// Task2 function

void vTask2(void *pvParameters) {

for(;;) {

// Task2 logic

printf("Task2 is running\n");

vTaskDelay(pdMS_TO_TICKS(1000)); // Block for 1 second

taskYIELD(); // Explicitly yield to let other tasks run

int main(void) {

xTaskCreate(vTask1, "Task 1", 1000, NULL, 1, NULL);

xTaskCreate(vTask2, "Task 2", 1000, NULL, 1, NULL);

// Start the scheduler

vTaskStartScheduler();

for(;;);

}
Key APIs for FreeRTOS Scheduling:

- vTaskStartScheduler(): Starts the task scheduler.

- taskYIELD(): Voluntarily yields control of the CPU.

- vTaskDelay(): Puts the task into a blocked state for a certain period.

- configUSE_PREEMPTION: Configuration option in FreeRTOSConfig.h to enable or disable pre-


emptive scheduling.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy