Whether Linux is a real-time system or a timeshare operating system
1. Real-time operating system definition
A real-time operating system (RTOS) is when external events or data are generated that can accept andprocessed quickly enough to control the production process or respond quickly to the processing system within a specified time, and to control the operating system for all real-time tasks in a coordinated and consistent operation。 Thus, providing timely response and high reliability are its main features. Real-time operating system hashard real-time and soft real-time's points,Hard -working requirements must be completed in the specified time, which is guaranteed during the design of the operating system, soft real-time only according to the priority of the taskTo complete the operation as quickly as possible. The operating system we typically use can become a real-time operating system after a certain change.
A real-time operating system is an operating system that ensures that certain functions are completed within a certain time limit. For example, an operating system can be designed to ensure that a robot on a production line can acquire an object. In a "hard" real-time operating system, the operating system will end up with an error if it is not possible to complete the calculation of the object within the allowed time. In the "soft" real-time operating system, the production line can still continue to work, but the output of the product can not be reached within the allowable time to slow down, which makes the robot has a short period of non-production phenomenon. Some real-time operating systems are designed for specific applications and others are generic. Some general purpose operating systems call themselves a real-time operating system. But to some extent, most of the general purpose operating systems, such as Microsoft's Windows NT or IBM's os/390, have real-time system features. This means that even if an operating system is not a strict real-time system, they can solve some real-time application problems.
2. Features of the real-time operating system
2) Thread Priority
3)multiple Interrupt levels
The small embedded operating system often requires a real-time operating system, the kernel to meet the real-time operating system requirements.
3. Real-time operating system relatedConcept
(1) Basic concepts
Code critical segment: refers to an inseparable code when processing. Once this part of the code is executed, interrupts are not allowed;
Resource: Any entity that is occupied by the task;
Shared resources: Resources that can be used by more than one task;
Task: Also known as a thread, is a simple program. Each task is given a certain priority, with its own set of CPU registers and its own stack space. Typically, each task is an infinite loop, each of which is in the following five states: Dormant state, ready state, running state, suspended state, interrupted state;
Task switching: Saves the current state of the running task (everything in the CPU register) in the task's own stack, then re-loads the current state of the next running task from the stack of the task to the CPU register and starts the next task;
Kernel: Manages individual tasks, allocates CPU time for each task, and is responsible for communication between tasks. It is divided into an inalienable kernel in the form of a stripped kernel;
Dispatch: One of the main functions of the kernel, which determines which task is to run. General priority-based scheduling method;
(2) Questions about priorities
Task priority: The static priority and the dynamic priority that can be changed by priority;
Priority reversal: Priority reversal is the most problematic problem in real-time systems. The allocation of shared resources can cause tasks with low priority to run first, and then to run after high-priority tasks. The solution is to use the "Precedence Inheritance"Algorithm to temporarily change task priority,to curb priority reversal。
(3) Mutual exclusion
Although shared data areas simplify the exchange of information between tasks, you must ensure that each task is exclusive when it processes shared shared data. The general method that satisfies the mutex condition is: off interrupt, using test-and-set command (TAS), disable task switching, and use semaphore.
Because the use of real-time operating system is to be able to deal with a variety of sudden events in a timely manner, that is, to deal with various interruptions, so the measurement of embedded real-time operating system of the main and most representative performance parameters should undoubtedly be interrupted response time. Interrupt response time is typically defined as:
Interrupt Response time = Interrupt delay time + time to save CPU State + Execution time of the ISR of the kernel into function .
Interrupt delay Time =max (maximum time to shut down, maximum instruction time) + time to start executing the first instruction of the ISR .
Time-sharing operating system "words": time-sharing operating system
"Phonetic notation": fēn shícāo Zuòxìtǒng
"English": time-sharing Operating System
"Interpretation": an operating system that enables a computer to serve several, dozens of, or even hundreds of users at the same time. To connect the computer with many end users, the time-sharing operating system will change the system processor and memory space at a certain time interval, and switch to the application of each end user in turn. Because of the short time interval, each user feels like he is an exclusive computer. The feature of time-sharing operating system is that it can effectively increase the utilization rate of resources. For example, the UNIX system uses the deprivation dynamic first CPU scheduling, which strongly supports the time-sharing operation.
The generation of time-sharing system is a new type of OS which is formed to satisfy the user's demand. It has distinct performance differences from multi-channel batch processing systems. The needs of users are manifested in the following aspects: human-machine interactive sharing host for user-friendly machine
The basic idea of time-sharing system
Time slice: Is the computer system resources (especially CPU time) in the time division, each time period is called a time slice, each user turns to use the time slice in turn.
Time-sharing technology: the operation of the processor is divided into a very short time slices, according to the time slice turns the processor into the use of the online operation.
Time-sharing operating system: is an online multi-user interactive operating system. In general, the use of time-slice rotation to make a computer for multiple Terminal Services. The response time is guaranteed to be fast enough for each user and provides an interactive session capability.
Design goal: To respond to the user's request in a timely manner, and to maximize the utilization of system resources under possible conditions.
Suitable for office automation, teaching and transaction processing, etc. to ask for opportunities to talk about the occasion.
A host connected to a number of terminals, each terminal has a user in use, interactive to the system to make command requests, the system accepts each user's command, the use of time-slice rotation to process the service request, and interactive way to display the results to the user on the terminal;
Time-sharing system implementation of the key issues: timely reception. Timely processing.
Interactivity: The user interacts with the system.
Multiplexing: Multiple users use the same CPU on their terminals at the same time.
Independence: Users can operate independently of each other, not interfere with each other, not confused.
Timeliness: Users in a short period of time can get a timely response to the system.
Factors affecting response time: The number of terminals, the size of time slices, the amount of information exchanged, the speed of information exchange.
Time-sharing-now popular PC, the server is the use of this mode of operation, that is, the CPU is divided into a number of times to separate the operation of processing requests
Real-time-generally used in single-chip microcomputer, such as the elevator up and down control, for the button and other action requirements for real-time processing
Linux is a time-sharing system, but it can be changed to live, such as: Ucos is the Linux modified real-time system, as for their differences, under the reference Baidu similar answer, personal feeling simple and clear: "Time-sharing system is a system can be two or more than two account service!"
A real-time system is an operating system that responds to instructions immediately! Microsoft's common system can't! And it's freezing! A fighter's operating system is a real-time system, think of if the fighter in the war when the computer reaction is the pilot on a command or the crash, who dares to open the plane? ”
What is the difference between a real-time operating system and what is a non-real-time operating system?
The operating system used in embedded real-time system we call embedded real-time operating system, it is both embedded operating system and real-time operating system. As an embedded operating system, it has the features of the embedded software, such as clipping, low resource occupancy, low power consumption, and so on, and as a real-time operating system (the discussion of real-time operating system features is limited to strong real-time operating system, the real-time operating system mentioned below also refers to strong real-time operating system) It differs greatly from the general operating system (such as Windows, Unix, Linux, and so on), and we will describe the main features of the real-time operating system by comparing the differences between the two operating systems.
In our daily work learning environment, we are most exposed to general operating systems, which are developed by time-sharing operating systems, most of which support multi-user and multi-process, and are responsible for managing numerous processes and allocating system resources to them. The basic design principles of time-sharing operating system are as follows: Minimizing the average response times of the system and increasing the throughput rate of the system, providing service for as many user requests as possible in unit time. From this we can see thatTime -sharing operating system pays attention to average performance and does not pay attention to individual performance。 For the entire system, the average response time for all tasks is not concerned with the response time of a single task, and for a single task, the average response time per execution is not concerned with the response time of a particular execution. Many of the strategies and techniques used in the general operating system embody this design principle, such as the virtual storage management mechanism, because of the use of the LRU page replacement algorithm, so that most of the demand for access can be quickly completed through physical memory, only a small part of the request needs to be completed by paging, but in general, The average time to visit and not to use the virtual storage technology is not greatly improved, while the virtual space can be much larger than the physical memory capacity and other benefits, so virtual storage technology in general operating system has been widely used. Similar examples are many, such as the Unix file system in the file location of the indirect index query mechanism, and even the hardware design of the cache technology and CPU Dynamic branch prediction technology, etc. also reflect this design principle. This shows that the emphasis on the average performance, that is, statistical characteristics of the design principles of the impact is very far-reaching.
For the real-time operating system, we have already mentioned that in addition to meet the functional requirements of the application, it is more important to meet the real-time requirements of the application, and the many real-time tasks composed of an application for real-time requirements are different, in addition to real-time tasks may also have some complex association and synchronization relationship, such as the implementation of sequential restrictions, mutual exclusion of shared resources access requirements, and so on, this is the system of real-time guarantee has brought great difficulties. SoThe most important design principles that real-time operating systems follow are: using a variety of algorithms and strategies to always ensure predictability of system behavior (predictability). Predictability refers to any time when the system is running, in any case, the real-time operating system's resource provisioning strategy can be used to compete for resources (including CPU, memory, network bandwidth, etc.) in a number of real-time tasks to allocate resources reasonably, so that the real-time requirements of each real-time task can be satisfied. Unlike the general operating system, the real-time operating system focuses not on the average performance of the system, but on the need for every real-time task to meet its real-time requirements in the worst-case scenario, meaning that the real-time operating system focuses on individual performance and, more accurately, individual worst-case performance. For example, if the real-time operating system uses the standard virtual storage technology, then a real-time task execution of the worst case is that each time the visit requires paging, so that the cumulative task in the worst case of the run time is unpredictable, so the real-time performance of the task is not guaranteed. So it can be seen that the virtual memory technology widely used in the general operating system is not suitable for the real-time operating system.
As the basic design principle of the real-time operating system differs greatly from that of general operating system, there are many differences between the choice of resource scheduling strategy and the method of operating system implementation, which are mainly reflected in the following points:
(1) Task Scheduling strategy:
The task scheduling strategy in general operating system generally adopts preemptive scheduling strategy based on priority, and for the same process with the same priority, the time slice rotation scheduling method is used, and the user process can adjust its priority by the system call dynamically, and the operating system can adjust the priority of some processes according to the situation.
The most widely used task scheduling strategy in real-time operating system is mainly divided into two kinds, one isStatic Table DriverWay, and the other isfixed-priority preemptive schedulingWay.
Static table-driven mode refers to the system before the operation of the engineers in accordance with the real-time requirements of the task in a manual way or with the help of AIDS to generate a task schedule, the timetable is similar to the timetable for the operation of the train, indicating the starting run time of each task and the length of the operation, once the production schedule is no longer changed At run time, the scheduler simply starts the corresponding task at the specified time based on this table. The main advantages of the static table-driven approach are:
? The running schedule is generated before the system runs, so a more complex search algorithm can be used to find a better scheduling scheme.
? The runtime scheduler is less expensive;
? The system has the very good predictability, the real-time verification is also more convenient;
The main drawback of this approach is that there is no flexibility, and once the requirements change, the entire run schedule is rebuilt.
Due to the very good predictability, this approach is mainly used in aerospace, military and other systems in the real-time requirements of the very strict field.
Fixed priority preemptive scheduling method is similar to the priority-based scheduling method used in general operating system, but in the preemptive scheduling mode of fixed priority, the priority of the process is fixed., and the priority is specified by a priority allocation policy (such as Rate-monotonic, Deadline-monotonic, and so on) before running. The advantages and disadvantages of this method and the static table drive way of the advantages and disadvantages of the exact opposite, it is mainly used in some simpler, more independent embedded systems, but with the continuous maturity and perfection of the scheduling theory, this way will gradually in some of the real-time requirements are very strict areas of application. Most of the real-time operating systems on the market today are using this scheduling method.
(2) Memory management:
On the virtual storage management mechanism we have had some discussion on the above. In order to solve the unpredictability of virtual storage system, the real-time operating system is generally used in the following two ways:
? On the basis of the original virtual storage management mechanismAdd page lock function, the user can lock the critical page in memory so that the swap program will not swap the page out of memory. The advantage of this method is that the virtual storage management mechanism is not only beneficial to software development, but also improves the predictability of the system. The disadvantage is that the design of the TLB mechanism is also based on the principle of the average performance, so the predictability of the system is not fully guaranteed;
? UsingStatic Memory PartitioningTo divide a fixed memory area for each real-time task. The advantage of this approach is that the system has a good predictability, the disadvantage is that the flexibility is not enough, the task of memory needs to change the memory needs to be re-partition, in addition to the virtual storage management mechanism of the benefits also lost.
The current real-time operating system in the market generally adopts the first management mode.
(3) Interrupt processing:
In general operating systems, most external interrupts are turned on, and interrupt processing is typically done by the device driver. Because the user process in the general operating system generally does not have the real-time requirements, and the interrupt handler interacts directly with the hardware device, there may be real-time requirements, so the interrupt handler priority is set higher than any user process.
However, it is inappropriate for the real-time operating system to use the above interrupt processing mechanism. First, an external interrupt is an input from the environment to a real-time operating system, and its frequency is related to the rate of change in the environment, regardless of the real-time operating system. If the frequency of an external interrupt is unpredictable, the time overhead of a real-time task being blocked by the interrupt handler at runtime is also unpredictable, which makes the task's real-time performance not guaranteed, and if the frequency of external interrupts is predictable, Once an external interrupt is generated more frequently than its predicted value (such as a false interrupt signal from a hardware failure or an error in the predicted value itself), the predictability of the entire system can be compromised. Second, each user process in a real-time operating system typically has a real-time requirement, so it is inappropriate for the interrupt handler to take precedence over the priority allocation of all user processes.
a kind of interrupt processing method which is more suitable for real-time operating system：Block all other interrupts except clock interrupts, and interrupt handlers become periodic polling operations, these operations are done by a kernel-mentality device driver or by a user-configured device support library. The main advantage of this approach is to fully guarantee the predictability of the system, the main disadvantage is that the response to environmental changes may not be as fast as the above interrupt processing, and the polling operation to a certain extent, reduce the effective utilization of the CPU.another way to do this is to use the interrupt method for external events that do not meet demand by polling, and still use polling for other times. At this point, however, the interrupt handler has the same priority as the other tasks, and the scheduler uniformly processes the task and interrupt handlers in the ready state based on the priority.。 This approach speeds up the response of external events and avoids the second problem with the above interrupt mode, but the first problem persists.
In addition, to improve the predictability of clock interrupt response time, the real-time operating system should mask interrupts as little as possible.
(4) Mutually exclusive access to shared resources:
General operating system generally uses the semaphore mechanism to solve the mutually exclusive access problem of shared resources.
For a real-time operating system, if the Task Scheduler takes a static table-driven approach, the mutually exclusive access problem of shared resources is taken into account when generating the run schedule, which is no longer considered at runtime. If the task scheduling takes precedence-based approach, then the traditional semaphore mechanism can easily cause the priority inversion problem when the system is running, that is, when a high-priority task accesses the shared resource through the semaphore mechanism, the semaphore is occupied by a low-priority task, and the inversion While this low-priority task may be preempted by some other medium-priority tasks while accessing shared resources, high-priority tasks are blocked by many lower-priority tasks, and real-time is difficult to secure. Soin the real-time operating system, the traditional semaphore mechanism is often extended to introduce such as Priority Inheritance Protocol (inheritance Protocol), priority top-level protocol (precedence Ceiling Protocol). As well as the stack Resource policy and other mechanisms , better solve the problem of priority inversion。
(5) system calls and the time overhead of internal system operations:
The process obtains the services provided by the operating system through system calls, and the operating system accomplishes some internal management through internal operations, such as context switching.in order to ensure the predictability of the system, all system calls in the real-time operating system and the time overhead of the internal operation of the system should be bounded, and the boundary is a specific quantitative value. In general operating systems, these time costs are not so limited.
(6) The reentrant nature of the system:
In the general operating system, the kernel mentality system calls are often non-reentrant, when a low-priority task calls the kernel-mind system call, the high-priority tasks that arrive within that time period must wait until the low-priority system call completes to obtain the CPU, which reduces the system's predictability. Sokernel mentality system calls in real-time operating systems are often designed to be reentrant.
Real-time system or timeshare operating system