0
0
FreeRTOSprogramming~5 mins

Task states (Ready, Running, Blocked, Suspended) in FreeRTOS

Choose your learning style9 modes available
Introduction

Tasks in FreeRTOS can be in different states to manage how they run. This helps the system decide which task to run and when.

When you want to know if a task is ready to run or waiting for something.
When debugging why a task is not running as expected.
When designing your program to handle tasks that wait for events or resources.
When you want to pause a task temporarily without deleting it.
When managing multiple tasks to keep the system responsive.
Syntax
FreeRTOS
Task states in FreeRTOS are:
- Ready
- Running
- Blocked
- Suspended

Ready: Task is ready to run but waiting for CPU time.

Running: Task is currently using the CPU.

Examples
This means the task can run anytime but is not running now.
FreeRTOS
Ready: Task is waiting in the queue to run when CPU is free.
Only one task runs at a time on a single-core CPU.
FreeRTOS
Running: Task is actively executing its code.
For example, waiting for a message or timer.
FreeRTOS
Blocked: Task is waiting for an event or time delay to finish.
Used to pause a task without deleting it.
FreeRTOS
Suspended: Task is stopped and will not run until resumed.
Sample Program

This program creates a task that prints a message and then waits for 1 second. The task moves between Running (printing) and Blocked (waiting) states.

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

void vTaskFunction(void *pvParameters) {
    for (;;) {
        printf("Task is running\n");
        vTaskDelay(pdMS_TO_TICKS(1000)); // Blocked state for 1 second
    }
}

int main(void) {
    TaskHandle_t xHandle = NULL;
    xTaskCreate(vTaskFunction, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, &xHandle);
    vTaskStartScheduler();
    // The scheduler will now run the task
    return 0;
}
OutputSuccess
Important Notes

A task in Ready state waits for CPU time but does not run until scheduled.

Blocked tasks do not use CPU until their wait condition is met.

Suspended tasks stay inactive until you explicitly resume them.

Summary

Tasks have states to control when they run.

Ready means waiting to run, Running means using CPU.

Blocked means waiting for something, Suspended means paused.