0
0
FreeRTOSprogramming~5 mins

Why scheduling determines real-time behavior in FreeRTOS

Choose your learning style9 modes available
Introduction

Scheduling decides which task runs and when. This controls how fast and reliably your program reacts to events.

When you want your program to respond quickly to sensor input.
When multiple tasks share the CPU and you need to control their order.
When you want to guarantee a task finishes before a deadline.
When you want to avoid delays caused by other tasks running too long.
Syntax
FreeRTOS
void vTaskStartScheduler(void);

// Starts the scheduler which runs tasks based on priority and readiness.

FreeRTOS scheduler picks the highest priority task that is ready to run.

Preemptive scheduling means a higher priority task can interrupt a lower priority one.

Examples
Task1 has higher priority (2) than Task2 (1), so Task1 runs first.
FreeRTOS
// Create two tasks with different priorities
xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL);

// Start the scheduler
vTaskStartScheduler();
This shows how scheduling controls which task runs based on state and priority.
FreeRTOS
// Task1 blocks waiting for event
// Task2 runs while Task1 is blocked
// When event occurs, Task1 becomes ready and preempts Task2
Sample Program

This program creates two tasks with different priorities. Task1 runs every 1 second, Task2 every 0.5 seconds. Because Task1 has higher priority, it runs first when ready.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void Task1(void *pvParameters) {
    for (;;) {
        printf("Task1 running\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void Task2(void *pvParameters) {
    for (;;) {
        printf("Task2 running\n");
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

int main(void) {
    xTaskCreate(Task1, "Task1", 1000, NULL, 2, NULL);
    xTaskCreate(Task2, "Task2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    for (;;) {}
    return 0;
}
OutputSuccess
Important Notes

Higher priority tasks run before lower priority ones.

Tasks that are blocked or waiting do not use CPU time.

Proper scheduling ensures your program meets timing needs.

Summary

Scheduling controls which task runs and when.

It affects how fast your program reacts to events.

Using priorities helps meet real-time deadlines.