How Multitasking is Implemented in RTOS Real-Time Operating Systems

Follow our official account to never miss an update! How Multitasking is Implemented in RTOS Real-Time Operating Systems

Many friends who have been using bare-metal programming for a long time may have misunderstandings about many concepts when switching to learn RTOS.

1Introduction

Most people do not understand some concepts in RTOS when they start learning real-time operating systems.

They are often confronted with concepts like critical sections, scheduling, semaphores, and mutexes, which can be quite confusing.

It’s normal not to understand these concepts right away; I would actually find it unusual if someone did.

2What is Multitasking?

Most people should come from a bare-metal background, where the bare-metal system is generally referred to as a single-task system, polling system or foreground-background system.

This concept is easy to understand: polling means executing within a large while loop. The foreground-background system responds to interrupts while executing the while loop.

int main(void){  /* Initialization */  while(1)  {    /* Process multiple tasks */  }}

So, what is multitasking?

When a multitasking operating system uses a scheduling strategy to allow two or more processes to share a processor concurrently, the processor actually only services one task at any given moment.

Because the task scheduling mechanism ensures rapid switching between different tasks, it creates the illusion that multiple tasks are running simultaneously.

— From Baidu Encyclopedia

Beginners can understand it as having similar to the above multiple polling systems.

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 involves switching between tasks: task scheduling.

3Task Scheduling

Before discussing task scheduling, let me share a side note:

When I first learned RTOS, I thought it was incredible that the CPU could switch between tasks so quickly (1ms), and that so much code had to be executed during the switch. How much code could the CPU actually execute?

In fact, I underestimated the capability of the processor.

For example, with the STM32F103 running at 72M, do you have any idea how much code can be executed in 1ms?

Reflecting on this problem, everyone can understand that the code for switching (task scheduling) can be negligible in terms of CPU speed (of course, this is relative).

Task Scheduling

Task scheduling can be divided into preemptive scheduling and round-robin scheduling.

In RTOS, to ensure that tasks receive real-time responses, preemptive scheduling is generally used. Let’s take UCOS as an example:

How Multitasking is Implemented in RTOS Real-Time Operating Systems

You will find that during program execution, if a higher-priority task arrives, the higher-priority task will interrupt the lower-priority task.

Only when the higher-priority task is completed will the lower-priority task receive its turn.

Some may ask: Can a high-priority task run indefinitely or for a long time?

The answer is: NO

This relates to task priority allocation and task design issues.

Generally, high-priority tasks wait for an event to trigger, executing urgent tasks that do not take too long.

Long-running tasks are generally left for lower-priority tasks to handle when the system is not busy.

How Multitasking is Implemented in RTOS Real-Time Operating Systems

The 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 from the kernel 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).

Recommended Reading:

1. Where does the SysTick clock source of STM32 come from?

2. Selected summary article 2019-03-30

4Conclusion

My Zhihu: strongerHuang

My website: www.strongerhuang.com

If you find this article helpful, please hit “like,” share it, and it will motivate me to continue updating.

Scan the QR code below to follow our official account for more exciting content!

How Multitasking is Implemented in RTOS Real-Time Operating Systems

Long press to recognize the QR code above to follow

Leave a Comment

Your email address will not be published. Required fields are marked *