What are the four task states in FreeRTOS, and what moves a task between them?
Running (on the CPU), Ready (able to run, waiting for the CPU), Blocked (waiting for an event or timeout) and Suspended (removed from scheduling until explicitly resumed). The scheduler always runs the highest-priority Ready task.
* The scheduler only chooses among Ready tasks. Blocked tasks return on an event or timeout; Suspended ones only on vTaskResume(). *
Every task has a state. Blocked, Ready and Suspended together form the Not Running super-state.
- Running → Ready: the task is preempted by a higher-priority task (or its time slice ends).
- Ready → Running: the scheduler picks it as the highest-priority ready task.
- Running → Blocked: the task calls a blocking API function, such as
vTaskDelay()or waiting on a queue. - Blocked → Ready: the event arrives, or the timeout expires.
- Any state → Suspended:
vTaskSuspend()is called. - Suspended → Ready: only
vTaskResume()brings it back.
The Blocked state is what makes an RTOS efficient. A task waiting for data uses no CPU at all, instead of spinning in a loop checking a flag. For example, a task waits at most 10 ms for data on a queue. It leaves the Blocked state either when data arrives within 10 ms or when 10 ms pass with no data. The timeout makes it robust, since it will never wait forever for a sensor that has died.
Tip: Blocked = waiting for something (with an optional timeout); Suspended = parked by someone, with no timeout.
Go deeper:
FreeRTOS — Task states — the official state diagram and each transition's API call.