Linux task scheduling mechanism

General Linux system to support real-time and non real-time two kinds of processes, real-time process with respect to the ordinary process has absolute priority. Correspondingly, real-time process using SCHED_FIFO or SCHED_RR scheduling policy, the process of using ordinary SCHED_OTHER scheduling policy.

In the realization of scheduling algorithms in Linux Each task has four parameters related to scheduling, they are rt_priority, policy, priority (nice), counter. Scheduling process scheduling program based on four parameters.

In SCHED_OTHER scheduling policy, scheduler always chooses the priority + counter maximum value to the process scheduled for execution. Logically there are scheduling policy analysis SCHED_OTHER scheduling period (epoch), in each scheduling period, the size of a process's priority value and counter the impact of the current moment which should be a process scheduled to perform, where priority is a constant value, it has been determined during the process of creation, which represents the priority of the process, but also on behalf of which the process in how much time slice of each scheduling period that can be obtained ; counter is the value of a dynamic, which reflects the a process in the current scheduling period remaining time slice . At the beginning of each scheduling period, the value of priority is assigned to counter, then the process is executed every time scheduling, counter values are reduced. When the counter value is zero, the process runs in its own time slice scheduling period is no longer participating in the process of the present scheduling period of scheduling. When all processes are running out of time slice, a scheduling cycle ends, and then again and again. In addition it can be seen in the scheduling period Linux system is not static , it is a dynamic change in the amount of, for example, in the number and value of their priority runnable processes can affect the length of a epoch. A point worth noting is that in the above 2.4 kernel, priority is substituted nice, but two similar effect.

Visible is the scheduling policy is essentially one kind SCHED_OTHER proportion of share scheduling policy, its design approach to ensure fairness during the process of scheduling - a low-priority process in every epoch will also get their deserved those CPU execution time, while it also provides a different prioritization process, a process with high priority values to get more execution time. For real-time process, they are using the priority scheduling strategy based on real-time priority rt_priority, but according to different scheduling strategies, real-time scheduling method between same-priority process is different:

SCHED_FIFO : different processes according to the static priority queuing, and then at the same priority queue, who should be ready to run on the first dispatcher who, and running process will not be terminated until the following occurs: (1). there is a higher-priority process of the seizure CPU;. (2) own request because the resource blocked;. (3) give up their own CPU (call sched_yield).

SCHED_RR : This scheduling policy SCHED_FIFO exactly the same with the above, except that it assigned a time slice to each process, the process time slice to give up being executed execution; the length of time slices can be obtained by calling sched_rr_get_interval.

As the Linux system itself is a desktop-oriented system, so there will be some problems when applied to real-time applications below it:

- Linux system scheduling unit of 10ms , so it is not possible to provide precise timing;

- When a process calls the system call into the kernel mode operation, it can not be preempted;

- Linux kernel implements used in a large number of locks to interrupt the operation will result in the loss of interruption;

- the use of virtual memory technology, when a page fault occurs, you need to read the exchange of data from the hard disk, hard disk read and write but due to the randomness of the storage location will cause random read and write time, which will affect in some cases, some real-time deadline for the task;

- Although Linux process scheduler also supports real-time priority, but the lack of effective scheduling mechanism and scheduling algorithm for real-time tasks; interrupt handling protocol processing and other equipment of its network subsystems do not associate it with its corresponding scheduling process, and they themselves have no clear scheduling mechanism;

References: " Linux-based real-time systems ."

 Real-time Linux research

thin core (microkernel) - Thin-Kernel

Thin core (or micro-kernel) method uses a second core as the abstract interface between the hardware and the Linux kernel. Non-real-time Linux kernel runs in the background, as the core of a thin low-priority task custody of all non-real-time tasks. Real-time tasks run directly on the thin kernel. Thin kernel is mainly used (in addition to hosting real-time tasks outside) interrupt management. Thin kernel intercepts interrupts to ensure that non-preemptive real-time kernel can not run lean kernel. This allows the thin kernel to provide hard real-time support.


Although there own advantages (hard real-time support with standard Linux kernel coexistence) thin kernel method, but this method has its drawbacks. Real-time tasks and non-real-time tasks are independent, which resulted in difficult to debug . Moreover, the non-real-time tasks have not been fully supported Linux platform (thin kernels is called thin for a reason). Examples of this approach are on RTLinux (now consists of Wind River Systems Proprietary), real-time application program interface (RTAI) and Xenomai.

nanokernel

Here thin kernel approach relies on task management includes minimal kernel , and the kernel method nanokernel further reduced. In this manner, it is not like a kernel and a hardware abstraction layer (HAL). Advanced Micro kernel provides hardware resources to run multiple operating systems to share a higher level. Advanced Micro kernel because the hardware abstraction, so it can give priority to higher-level operating system to support real-time.


Note that this approach and the approach to virtualization to run multiple operating systems there are some similarities. In the case of using this method, the hardware abstraction nanokernel in real-time and non real-time kernel. This way the operating system on bare metal abstract from the client (guest) and the hypervisor is very similar.

Examples of Advanced Micro Devices on the kernel of an operating system Adaptive Domain Environment for Operating Systems (ADEOS). ADEOS supports multiple concurrent operating systems run synchronously. When hardware events occur, ADEOS on the chain each operating system queries to determine which one to use system to handle events.

resource kernel ( Resource-Kernel )

Another real-time architecture is the core resource law. This method for the kernel to add a module that provides resources for the reservation (reservation). This mechanism ensures that for the time division multiplex access (time- multiplexed) system resources (CPU, network or disk bandwidth). These resources have more reserved parameters, such as cycle, required processing time (i.e., time required to complete the process), and the deadline.


资源内核提供了一组应用程序编程接口(API),允许任务请求这些预留资源。然后资源内核可以合并这些请求,使用任务定义的约束定义一个调度,从而提供确定的访问(如果无法提供确定性则返回错误)。通过调度算法,如Earliest-Deadline-First(EDF),内核可以处理动态的调度负载。

资源内核法实现的一个示例是CMU公司的Linux/RK,它把可移植的资源内核集成到Linux中作为一个可加载模块。这种实现演化成商用的 TimeSys Linux/RT 产品。

标准的Linux内核最新版本2.6中加入了实时功能

目前探讨的这些方法在架构上都很有趣,但是它们都在内核的外围运行。然而,如果对标准Linux内核进行必要的修改使其支持实时性,结果会怎么样呢?

今天,在2.6内核中,通过对内核进行简单配置使其完全可抢占,您就可以得到软实时功能。在标准2.6 Linux内核中,当用户空间的进程执行内核调用时(通过系统调用),它便不能被抢占。这意味着如果低优先级进程进行了系统调用后,高优先级进程必须等到调用结束后才能访问CPU。

新的配置选项CONFIG_PREEMPT改变了这一内核行为,在高优先级任务可用的情况下(即使此进程正在进行系统调用),它允许进程被抢占。


However, this configuration option is a compromise. Although this option to achieve a soft real-time performance even under load conditions and also allows the operating system to run smoothly, but doing so paid the price. The cost is slightly reduced and the throughput performance of the kernel , because CONFIG_PREEMPT option adds overhead. This option for desktop and embedded systems are useful, but not in any case scenarios are useful (eg, servers).

In the 2.6 kernel Another useful configuration options are high-precision timer. This new option allows the timer to run 1μs accuracy (if the underlying hardware supports it), and to achieve efficient management of the timer by red-black tree. By red-black tree, you can use a lot of timers will not affect the timer subsystem (O (log n)) performance.

Just need a little extra work, you can achieve hard real-time by PREEMPT_RT patch. PREEMPT_RT patch provides several modifications, enabling hard real-time support. Some of these modifications include re-implement some kernel locking primitives in order to achieve a fully preemptive, to achieve mutually exclusive kernel priority inheritance, and the interrupt handler converted to kernel threads in order to achieve the thread can be preempted .

Guess you like

Origin blog.csdn.net/qq_42381849/article/details/93001099
Recommended