real-time operating system

The concept of a real-time operating system

Real-time operating system (RTOS) means that when external events or data are generated, they can accept and process them at a fast enough speed, and the processing results can control the production process or respond quickly to the processing system within the specified time. , and controls the operating system where all real-time tasks run in unison. Therefore, providing timely response and high reliability are its main characteristics. The real-time operating system can be divided into hard real-time and soft real-time. Hard real-time requires that the operation must be completed within the specified time, which is guaranteed during the design of the operating system; soft real-time only needs to complete the operation as quickly as possible according to the priority of the task. That's it. The operating system we usually use can become a real-time operating system after certain changes.

Features of a real-time operating system

High precision timing system

Timing accuracy is an important factor affecting real-time performance. In real-time application systems, it is often necessary to accurately determine the real-time operation of a certain device or perform a certain task, or to accurately calculate a time function. These depend not only on the clock accuracy provided by some hardware, but also on the high-precision timing function implemented by the real-time operating system.

Multi-level interrupt mechanism

A real-time application system usually needs to process a variety of external information or events, but the urgency of processing has different priorities. Some must be responded to immediately, while others can be dealt with later. Therefore, it is necessary to establish a multi-level interrupt nesting processing mechanism to ensure timely response and processing of high-urgency real-time events.

Real-time scheduling mechanism

The real-time operating system should not only respond to real-time event interrupts in time, but also schedule and run real-time tasks in time. However, processor scheduling cannot be done arbitrarily, because it involves switching between two processes, which can only be done at the time point to ensure "safe switching". The real-time scheduling mechanism includes two aspects, one is in the scheduling strategy and Algorithms guarantee priority scheduling of real-time tasks; the second is to establish more "safe switching" time points to ensure timely scheduling of real-time tasks.

real-time operating system kernel

kernel

The kernel is responsible for task management and communication between tasks. When the kernel decides to run another task, it saves the current task's context (CPU registers ) to the current task's separate stack area. The new task's context is restored from its stack area, resuming code execution for the new task. This process is called context switching or task switching. Information such as the heap top address of each task is stored in the task control block (TCB) data structure. TCBs are assigned when tasks are created and are managed by the RTOS.

Multitasking

Multitasking is the process of scheduling and switching CPUs between multiple tasks , and a single CPU is switched between multiple sequential tasks. Multitasking provides the ability to break down an application into a set of small dedicated tasks that share a processor. Real-time kernels make applications easier to design and maintain. A task is a simple program that thinks it owns the entire CPU. The design process for a real-time application involves splitting the work to be done into tasks responsible for parts of the problem.

to interrupt

An important issue in real-time systems is the time required from the interrupt acknowledgment to the actual start of execution of the user interrupt handling code. When processing critical code, the RTOS disables interrupts. The longer interrupts are disabled, the greater the interrupt latency. RTOSs typically disable interrupts in less than 50 uS, the shorter the better.

scheduling

Scheduling is the kernel's primary responsibility for figuring out which tasks to run and when. Most real-time kernels use a priority policy, where each task is assigned a priority based on its importance. The priority of tasks is specified by the application. In a priority-based scheduling kernel, control of the CPU is always given to the highest priority task that is ready. However, when the highest priority task gets the CPU depends on the type of scheduling used. There are two types of scheduling: non-preemptive scheduling and preemptive scheduling.

non-preemptive scheduling

Non-preemptive scheduling requires each task to voluntarily relinquish control of the CPU. In order to keep tasks concurrent, this process must be done frequently. Non-preemptive scheduling is also known as cooperative multitasking. When a task relinquishes the CPU, the kernel executes the next most important task code that becomes available. Asynchronous events are handled by ISRs. An ISR can make a higher priority task ready, but returns to the interrupted task after the ISR completes. A new higher priority task will gain control of the CPU only if the current task voluntarily relinquishes the CPU. The latency of non-preemptive scheduling is much lower than that of the front-end and back-end systems; the latency is determined by the time of the longest task.

preemptive scheduling

In a preemptive kernel, when an event makes a higher-priority task ready, the current task is immediately suspended and the higher-priority task takes control of the CPU. If the ISR makes a higher-priority task ready, the interrupted task is suspended and execution resumes with a new higher-priority task. Most real-time systems use preemptive scheduling, which is more responsive.

heavy entry

A reentrant function is a function that can be used by multiple tasks without fear of data corruption. Conversely, non-reentrant functions cannot be shared by multiple tasks, but use of non-reentrant functions can be mutually exclusive by using semaphores or by disabling interrupts in code in critical sections. Reentrant functions can be interrupted and resumed at any time without data loss. Reentrant functions use local variables (CPU registers or stack variables) and protect their data if using global variables. Compilers designed specifically for embedded software often provide reentrant runtime libraries. Non-preemptive scheduling does not require reentrant functions unless the functions are shared between tasks and ISRs. Preemptive scheduling requires the function to be reentrant if the function is shared by multiple tasks.

kernel service

The real-time kernel provides various services to applications. One of the most common services provided by the kernel is semaphore management. A semaphore is a protocol mechanism used to control access to a shared resource (mutual exclusion), publish the occurrence of an event, or allow two tasks to synchronize their activities. A semaphore is usually a switch for code to continue executing . If the semaphore is already in use, the requesting task will be suspended until the semaphore is released by its current owner. Suspended tasks typically consume no CPU time.

The kernel also provides time-related services that allow tasks to delay themselves by an integer number of system clocks . Clock ticks typically occur every 10 to 200 milliseconds, depending on application requirements.

A task or ISR passes information to another task, this is called inter-task communication, and the services for sending and receiving messages are usually provided by the kernel. The two most common kernel services for sending messages are message mailboxes and message queues. A message mailbox, also known as a message exchange, is usually a pointer variable. Messages (pointers) are sent to mailboxes by services, tasks or ISRs provided by the kernel. The content of the message pointed to by the sending task and receiving task contract pointers. Message queues are used to send multiple messages to a task. A message queue is essentially an array of mailboxes.

Commercial RTOS

There are currently about 100 RTOS suppliers. Products are available for 8, 16 and 32-bit microprocessors. Some of these are complete operating systems, including real-time kernels, input/output managers, window systems, file systems, network stacks, language interface libraries , debuggers , and cross-platform compilers, among others. The cost of an RTOS can range from $100 to over $10,000. With so many vendors, the difficulty lies in choosing the right RTOS.

small embedded system

Many small embedded systems such as engine controls, smart instruments, robotics, computer peripherals, and telecommunications equipment can benefit from using an RTOS. Such systems are usually based on 8-bit microprocessor designs. With a 64 KB address space, most 8-bit microprocessors cannot afford a memory-intensive RTOS. Commercial kernels only require about 1 to 3 KB of ROM . Some kernels even allow the user to specify the stack size on a task-by-task basis. This feature helps to reduce the RAM space required by the application . A common misconception about RTOS is that it significantly increases CPU overhead. In fact, an RTOS only needs 1% to 4% of CPU time in exchange for valuable services. Features of a small RTOS include:

low cost

Has minimal interrupt latency

Kernel service execution time determination

Ability to manage at least 20 tasks

Allows dynamic creation and deletion of tasks

Provide semaphore management services

Provides time delays and timeouts based on kernel services

おすすめ

転載: blog.csdn.net/weixin_45485619/article/details/127609305