π©π» [Unveiling the RTOS World] A Deep Dive into the Five States of Tasks, Enabling You to Master the Art of Embedded Scheduling!
π Have you ever wondered how smart devices like smartwatches, drones, and robotic vacuum cleaners manage various tasks in their “brains”? Today, we will explore the “Five Brothers of Task States” in FreeRTOS in a simple, humorous way!
β οΈ This article exceeds 3000 words, filled with images, examples, and metaphors, making it suitable for intermediate embedded engineers, hardware developers, and RTOS beginners to bookmark!
π§ What is FreeRTOS?
FreeRTOS is a real-time operating system (RTOS) specifically designed for embedded devices. It acts like a “task scheduling master,” allowing the processor to switch between multiple tasks in an orderly manner.
So, how many states can a task have? How do they run? How do they switch? Do tasks conflict with each other? Is there any “palace intrigue”? Letβs officially enter the RTOS world!
πΉ The Five Task States, Unveiling Their Status in the RTOS World
First, letβs get to know the five major sects of RTOS, which are the five task states:
π’ 1. Running
π Sect: CPU’s Chosen Main Character Sect
When a task is currently running on the CPU, it is in the [Running State]!
π Characteristics:
-
The processor is busy with it.
-
If the processor has only one core, only one task can be in this state.
π¬ Analogy: You are in line at a bank window, and when itβs finally your turn to handle your business, at that moment, you are the “running task”!
π‘ 2. Ready
π Sect: Always on Standby Sect
Task State: I can run, I want to run, but itβs not my turn yet!
π Characteristics:
-
Waiting for the CPU to be free.
-
A task with a higher priority than you is currently running.
π¬ Analogy: You are waiting to place an order at McDonald’s, the line is long, but you already have the menu in hand and are ready to pay.
π Advanced Tip: FreeRTOS prioritizes scheduling higher priority tasks, so if your priority is low, you will have to wait.
π΄ 3. Blocked
π Sect: Waiting for the Wind Sect
Task State: I have nothing to do for now, waiting for a “message” or “time”.
π Characteristics:
-
Waiting for a certain event (like receiving a semaphore, queue data, or the end of a delay).
-
Will not occupy the CPU.
π¬ Analogy: You ordered takeout, and you can’t just wait idly, so you do something else (or sleep) until the food arrives!
π₯ API Example:<span>vTaskDelay()</span>
puts the task to sleep until it is awakened when the time is up.
β Practical Scenario:
void vMyTask(void *pvParameters) { while(1) { printf("Preparing to sleep for a while...\n"); vTaskDelay(pdMS_TO_TICKS(1000)); // Wake up once every second! printf("I'm awake! I will continue working~\n"); }}
π 4. Suspended
π Sect: Passive Hang Sect
Task State: I have “manually put myself to sleep”; I will wake up when you call me!
π Characteristics:
-
Will not be scheduled.
-
No timeout mechanism; must use
<span>vTaskSuspend()</span>
and<span>vTaskResume()</span>
to control.
π¬ Analogy: You are playing a game, and your mom calls you to take out the trash; you must pause! You can only continue playing after completing the task (being called back).
π₯ Example:
vTaskSuspend(NULL); // Suspend itself// ...then called by external code to vTaskResume() to wake up
β« 5. Deleted
π Sect: Retired from the World Sect
Task State: I have “exited the world” and am completely deleted, no longer participating in scheduling.
π Characteristics:
-
Will not be scheduled again.
-
Can manually
<span>vTaskDelete(NULL)</span>
, releasing resources.
π¬ Analogy: You have completed your business and left; the window is now empty, and itβs the next person’s turn.
π₯ Example:
void vSelfDeleteTask(void *pvParameters) { // ...business logic vTaskDelete(NULL); // Self-termination}
π Priority & Scheduling Strategy Overview
β« How is Priority Assigned?
-
The higher the number, the higher the priority.
-
The default lowest priority is 0 (also called
<span>tskIDLE_PRIORITY</span>
). -
If you want a task to be a VIP, just give it a high priority!
βοΈ Set the maximum priority value in the configuration file:
#define configMAX_PRIORITIES 5
π§ Tip: Donβt set too many priorities, as it can waste memory.
ποΈ What is the Scheduling Method?
πΊ Preemptive Scheduling
Whoever is important runs first, cutting in line at any time!
As long as a higher priority task is ready, the current lower priority task will be immediately preempted.
π Time Slicing
Tasks of the same level take turns running, each for a time slice.
π Control Macro:
#define configUSE_TIME_SLICING 1
π What About Multi-Core Systems?
π§© AMP Scheduling Strategy (Asymmetric Multi-Core)
-
Each core runs an independent FreeRTOS.
-
Inter-core communication relies on shared memory.
π§ SMP Scheduling Strategy (Symmetric Multi-Core)
-
One FreeRTOS controls multiple cores.
-
The scheduler is smart enough to allow multiple tasks to run simultaneously on different cores.
π― Configuration Tip:
#define configUSE_CORE_AFFINITY 1
Then use:
vTaskCoreAffinitySet(xHandle, 0x01); // Restrict the task to run only on Core0
π§ͺ Easy to Learn: Letβs Write a Task!
void vBreatheTask(void *pvParameters) { for (;;) { printf("Inhale... Exhale...\n"); vTaskDelay(pdMS_TO_TICKS(2000)); }}xTaskCreate(vBreatheTask, "Breath", 1000, NULL, 2, NULL);
π― Tip: Remember to create the task and start the scheduler in <span>main()</span><code><span>:</span>
vTaskStartScheduler();
π¦ Summary Table: Quick Reference for the Five Task States
State | Is Running | Can be Scheduled | Waiting for Event | Auto Recovery |
---|---|---|---|---|
Running | β | β | β | β |
Ready | β | β | β | β |
Blocked | β | β | β | β (Timeout or Event) |
Suspended | β | β | β | β (Manual Recovery Required) |
Deleted | β | β | β | β |
π§© Further Reading
-
FreeRTOS Official Website: https://www.freertos.org
-
Recommended Chinese Documentation: FreeRTOS Chinese Community, RT-Thread Learning Materials
-
Example Source Code: STM32 + FreeRTOS Breathing Light/Serial Print Example
π¬ Final Thoughts
π If you found todayβs content enlightening, feel free to give a thumbs up and bookmark it! Want to learn more about practical RTOS programming? In the next issue, we will discuss the secrets of Queues, Semaphores, and Event Groups!
π¬ Feel free to leave your project requirements and learning questions; letβs make the tedious embedded development more interesting!
π Follow me for daily fun technical insights; see you next time!