We often refer to RTOS (Real Time Operating System) as a real-time operating system, but what exactly is a real-time operating system? Is it truly real-time?This question is likely to be on the minds of many beginners.
Brief Overview of RTOS
RTOS: Real Time Operating System, which means real-time operating system.
According to Baidu Encyclopedia:
A real-time operating system is one that can accept and process external events or data at a sufficiently fast speed, with the results being able to control the production process or respond quickly to the processing system within a specified time. It schedules all available resources to complete real-time tasks and ensures that all real-time tasks run in a coordinated manner.
The emphasis of a real-time operating system is on real-time (timely response). In simple terms, it means that the program can promptly address and handle urgent matters without experiencing issues like “system hang”.
For example: A moving vehicle detects an obstacle ahead with its sensors and must immediately slow down or stop, rather than taking a long time to react (a slow response could lead to a collision).
Comparison with Bare Metal
Students transitioning from bare metal to RTOS often compare the two:
-
What advantages does RTOS have over bare metal?
-
Is RTOS more convenient than bare metal?
-
……
I can confidently say: RTOS is indeed more convenient than bare metal and has more advantages. Of course, this is provided that the MCU resources (Flash, RAM) meet the requirements.
In the past, MCU resources were relatively scarce, for example, Flash less than 10K and RAM less than 1K. In such cases, the advantages of using RTOS are not obvious and may even expose its shortcomings.
However, nowadays, MCU resources are relatively abundant, often exceeding 1M of Flash and 100K of RAM. In this situation, running bare metal seems like a waste of MCU resources.
Recommended reading:
The Shift in Thinking from Bare Metal to RTOSThe Differences Between Bare Metal Systems and Multithreaded SystemsIs it Meaningful to Run RTOS on 51 Microcontrollers?
Comparison with Time-Sharing Operating Systems
Many people think of time-sharing operating systems (TSOS) when discussing RTOS. What are the differences between RTOS and TSOS? What are their respective characteristics?
With the current speed of processors, the real-time capabilities of time-sharing operating systems have also improved significantly. The differences can actually be understood from their literal meanings; time-sharing refers to dividing time into small slices, typically at the microsecond level or even lower.
Understanding the scheduling mechanism of TSOS will help clarify the differences between the two.
This section can refer to my previous article:What are the Differences Between RTOS and TSOS?
Is RTOS Truly Real-Time?
Returning to today’s topic: Is RTOS truly real-time?
Strictly speaking, RTOS does not respond to and process urgent matters in real-time, it only responds in a very short time (typically at the millisecond level), giving the impression of real-time response.
A single CPU can only handle one task at a time (it can only execute one program at a time). When you create tasks 1, 2, 3, etc., the CPU executes them in turn (according to priority).
1. System Tick
An important configuration for RTOS real-time response is the system tick (SysTick).
For example, in FreeRTOSConfig.h
#define configTICK_RATE_HZ ((TickType_t)1000)
And in the μCOS system’s os_cfg.h
#define OS_TICKS_PER_SEC 1000u
The system tick determines the timing of the RTOS’s underlying scheduling. If set to 1000, it means a scheduling occurs every 1ms, indicating a response every 1ms.
Taking the example of the vehicle encountering an obstacle: when the sensor detects an obstacle, it notifies a higher-priority task to brake, and this process requires only 1ms to respond.
You might say: If I set it to 10000, will it respond in 0.1ms? Is a larger system tick always better?
In theory, a larger system tick value leads to faster responses, but system scheduling also consumes time:
The duration of scheduling remains constant; if the time between N and N+1 is shorter (tick), the time available for executing tasks becomes shorter.
Therefore, the tick value is not always better when larger; a reasonable value is needed. You can refer to:What is the Appropriate Tick Setting for Real-Time Operating Systems?
2. Hardware Interrupts
Students transitioning from bare metal to RTOS often think: I can achieve real-time response using interrupts.
Indeed, interrupts can achieve real-time responses, but they cannot meet most requirements.
For instance, in the case of the vehicle braking: if an obstacle is detected ahead, an interrupt response is triggered immediately, executing the deceleration action. If this action is an S-curve (gradual deceleration), the execution time is 1s.
If you execute this 1s braking action within the interrupt function, the CPU will not be able to perform other tasks. Do you think this is acceptable?
Hardware interrupts can only provide an “urgent notification” and cannot perform (time-consuming) execution actions.
RTOS combined with hardware interrupts can perfectly solve this problem: the interrupt notifies a higher-priority task to execute the braking action, but this process may take 1ms.
Thus, you will find that RTOS is not truly real-time; it simply responds in a very short time, which is often imperceptible.
1. Why is there so much industry focus on a new processor core?
2. The second issue of the 2022 “Microcontroller and Embedded Systems Applications” electronic journal is freshly released!
3. Chip price increases: unavoidable!
4. The courseware and video course on “In-Depth Understanding of RISC-V Program Development” are now online~
5. Don’t regret it! 18 practical experiences in embedded C summarized by industry veterans
6. Transitioning from hardware to software requires caution!
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are any copyright issues, please contact us, and we will confirm the copyright based on the materials you provide and either pay for the manuscript or delete the content.