Follow+Star Public Account Number, don’t miss out on exciting content
Author | strongerHuang
WeChat Public Account | Embedded Column
Many friends have been using bare-metal programming for a long time, and when switching to learn RTOS, they often misunderstand many concepts within it.
Most people starting to learn about real-time operating systems (RTOS) do not fully understand some of the concepts involved.
Right from the start, concepts like critical sections, scheduling, semaphores, and mutexes can leave most beginners feeling confused.
It’s normal not to understand these concepts right away; if someone does, I would actually find that unusual.
1 What is Multi-Tasking
Most people should come from a bare-metal background, where bare-metal systems are generally referred to as single-task systems, polling systems or foreground-background systems.
This concept should be clear to everyone: polling is executed within a large while loop. The foreground-background system responds to interrupts (foreground) while executing the while loop.
int main(void){ /* Initialization */ while(1) { /* Loop to handle multiple tasks */ }}
So, what is multi-tasking?
When a multi-tasking operating system uses a certain task scheduling strategy to allow two or more processes to concurrently share a processor, in fact, the processor will only serve one task at a time.
Because the task scheduling mechanism ensures that the switching speed between different tasks is very fast, it creates the illusion that multiple tasks are running simultaneously.
— From Baidu Encyclopedia
Beginners can understand this as: having several polling systems similar to the one above.
The following code divides the multiple tasks in the large while loop into several tasks to be handled separately.
void Task1(void){ /* Initialization */ while(1) { /* Handle task 1 */ }}
void Task2(void){ /* Initialization */ while(1) { /* Handle task 2 */ }}
This brings us to the switching between tasks: task scheduling.
2 Task Scheduling
Before discussing task scheduling, let me share a side note:
When I started learning RTOS, I thought that with such a short time (1ms) for the CPU to switch back and forth between tasks, and with so much code to execute during the switch, how much code could the CPU actually execute?
In fact, I underestimated the capabilities of the processor at that time.
For example, with the STM32F103 running at 72M, do you have any idea how much code can be executed in 1ms?
Following this question, everyone can further think and understand that the few lines of code for switching (task scheduling) are negligible compared to the CPU speed (of course, this is relative to high-speed processing. For low-frequency processors, this time may be relatively longer).
Task Scheduling
Task scheduling can be divided into preemptive scheduling and cooperative scheduling.
In RTOS, to ensure that tasks receive real-time responses, preemptive scheduling is generally used. Taking UCOS as an example:
You will find that during program execution, if a high-priority task arises, the high-priority task will interrupt the low-priority task.
Only after the high-priority task is completed will the low-priority task receive its turn.
Some may ask: Can a high-priority task run continuously or for a long time?
The answer is: NO
This relates to task priority allocation and task design issues.
Generally speaking, high-priority tasks are waiting for an event to trigger, executing something urgent that is not too time-consuming.
Time-consuming tasks are usually left for low-priority tasks to handle slowly when the system is not busy.
Scheduling Process
We set the system tick to 1ms, so the system will check every 1ms for higher-priority tasks in the ready queue.
This 1ms tick is generated by a timer interrupt, typically produced by the core tick timer in STM32.
As shown in the figure above, at position (2), it detects that a higher-priority task (7) is ready, and at this point, it will switch to execute task (7).
———— END ————
Reply with ‘RTOS’ to read more related articles.
Follow the WeChat Public Account ‘Embedded Column’ for more content, reply ‘Join Group’ to join the technical exchange group according to the rules.
Click ‘Read the Original‘ to see more shares, and feel free to share, bookmark, like, and view.