Current location - Training Enrollment Network - Mathematics courses - What is the core idea of linux scheduling algorithm?
What is the core idea of linux scheduling algorithm?
The first part: Real-time scheduling algorithm.

POSIX 1003.b defines what a real-time system is: it means that the system can provide the required service level within a limited response time. The logical correctness of real-time system program depends not only on the accuracy of calculation, but also on the results. If the time limit of the system cannot be met, it will be a system error.

According to the different requirements of real-time system, it can be divided into soft real-time and hard real-time. Hard real-time system means that the system must meet the worst-case service time and event response time under any circumstances. For example, the control of spacecraft is the reality of such a system. All the characteristics of other real-time systems can be called soft real-time systems. If cleared, soft real-time systems are those. From a statistical point of view, a task (in the following discussion, we will not distinguish between tasks and processes) can guarantee the processing time of the system, and get the deadline for events to be processed before the deadline comes, without bringing fatal errors. For example, a real-time multimedia system is a soft real-time system.

The CPU and other resources of the computer system are effectively scheduled and managed to provide real-time operating system support. In the multitask real-time system, the scheduling and management of resources are more complicated. The following discussion will focus on various real-time task scheduling algorithms, ordinary Linux operating system process scheduling and the classification of various real-time Linux systems, and then study their support for real-time characteristics and improvement of ordinary Linux systems. Some problems in the real-time field are summarized, and various real-time Linux operating systems are summarized. In the final analysis, how to solve these problems.

Classification of CPU real-time scheduling algorithms

The real-time scheduling algorithms of various real-time operating systems can be divided into the following three categories: Wang99] [Gopalan0 1]: priority-based scheduling algorithm (priority-driven scheduling PD), CPU utilization-based scheduling algorithm (share-driven scheduling SD), and time-based process scheduling algorithm (time-driven scheduling TD). The following describes three scheduling algorithms one by one.

1. 1

/& gt; Priority-based scheduling algorithm, priority-based scheduling algorithm, each process is given a priority, and every time a process is scheduled, the scheduler always has the task with the highest scheduling priority to execute. According to different priority allocation methods, priority-based scheduling algorithms can be divided into the following two types: Krishna0 1] [Wang99]: static priority scheduling algorithm.

The algorithm obtains that all processes running in these systems are statically assigned priorities. Apply the attributes of static priority assignment, such as user priority in the task cycle, or other predetermined policies. RM (Rate Monotonicity) scheduling algorithm is a typical static priority scheduling algorithm. According to the length of the scheduling priority cycle of executed tasks, those tasks with small execution cycle have higher priority.

Dynamic priority scheduling algorithm;

The algorithm dynamically allocates the priority of tasks according to their resource requirements, and the purpose of resource allocation and scheduling is more flexible. For non-real-time systems, there are many such algorithms, such as short job priority scheduling algorithm. Real-time task scheduling algorithm, EDF algorithm is the most widely used dynamic priority scheduling algorithm. It assigns priority according to the deadline of each task in the ready queue, and the latest deadline has the highest priority.

1.2

The scheduling algorithm based on priority scheduling algorithm is simple and effective, but it is hard real-time scheduling based on proportional sharing. In many cases, this algorithm is not suitable for soft real-time applications, such as real-time multimedia conference systems. For soft real-time applications, * * * resource scheduling algorithm (SD algorithm) is more suitable.

Proportional * * * shared scheduling algorithm refers to the proportional * * shared scheduling algorithm of CPU utilization. Its basic idea is that a group of scheduled tasks should be weighted according to a certain proportion, so that their weights are proportional to the execution time.

There are two ways to realize the proportional * * * shared scheduling algorithm [Nieh0 1]: the first method is to adjust the first frequency of the scheduling queue and arrange the first-line queue in the preparation process, and the second method is to continuously schedule the processes to be put into production in the ready queue, but allocate the weight of a process running time slice according to the adjustment.

Proportional scheduling algorithms can be divided into the following categories: round robin method, fair sharing method, fair queuing method, new lottery scheduling method, (lottery).

One problem with the proportional * * * shared scheduling algorithm is that it does not define any concept of priority. All tasks are shared according to the proportion of CPU resources they use. When the system is overloaded, all the tasks performed will slow down. Therefore, in order to ensure that the system has a certain CPU processing time in the real-time process, the dynamic weight adjustment process is generally adopted.

1.3。 Scheduling algorithm based on time process scheduling algorithm

For those systems with stable and simple input, time-driven (driving time: TD) data processing can be used, which can provide good prediction. This scheduling algorithm is essentially an offline static scheduling method with a design prototype. In the design stage of the system, in all cases, in a clear system, the start and end time of each task switch is arranged and designed in advance. The algorithm is suitable for small embedded systems, automatic control systems, sensors and other application environments.

The advantage of this algorithm is the predictability of task execution, but the biggest disadvantage is the lack of flexibility, and there will be a task to be executed when the CPU remains idle.

CPU scheduling in general Linux system

General Linux system supports real-time and non-real-time processes, and real-time processes have absolute priority over ordinary processes. Accordingly, the real-time process scheduling strategy is SCHED_FIFO or SCHED_RR, and the ordinary process scheduling strategy is SCHED_OTHER.

Each task scheduling algorithm is implemented in Linux and has four scheduling parameters, namely rt_priority priority policy (Nice) and counter. Scheduling process scheduling is based on these four parameters.

SCHED_OTHER scheduling strategy, the scheduler always chooses the value of priority+counter to schedule the execution of the process. The SCHED_OTHER scheduling strategy is used to logically analyze the scheduling process, which is characterized in that the priority is a fixed scheduling period (epoch), and the influence of the priority in the process of each scheduling period and the value of the counter are generated when the variable value has been determined, which represents the priority of the process and the number of time slices. By this method, we can get that the counter is a dynamic value in each scheduling cycle, which reflects the current scheduling cycle. At the beginning of each scheduling cycle, it is assigned to the priority counter, so what is the value of the counter that each process is scheduled to run? Reduce. When the value of the counter is zero, the process will no longer participate in the process scheduling in the scheduling cycle during the time slice scheduling cycle in which the process has been running. When all processes run out of time slices, the scheduling cycle ends, and then over and over again. In addition, we can see that the scheduling cycle in Linux system is not fixed, but its number is dynamic, for example, the number of running processes and their priority values? Can affect an epoch-making length. It is worth noting that in the 2.4 kernel, the first task is to replace two similar functions.

SCHED_OTHER, a proportional sharing scheduling strategy, is visible, and its design method ensures the fairness of process scheduling-a low-priority process will also get their share of execution time of those CPUs in each era. In addition, it also provides different process priorities, and more high priority values can be obtained when the process executes.

For real-time processes, they use the priority scheduling strategy based on real-time priority rt_priority, but the process scheduling method with the same real-time priority is based on different scheduling strategies.

BR/>; SCHED_FIFO: Different processes are queued according to static priority, and then in the same priority queue, the first scheduled and running process ready to run will not be terminated until the following situation occurs: 1. High priority process encroached on CPU, and your own resource request was blocked; Give up CPU actively (call sched _ yield);

SCHED_RR is like this: this scheduling strategy SCHED_FIFO is exactly the same as the above, except that time slices are allocated to each process. In the process of implementation, the length of time slice can be called by sched_rr_get_interval.

Because Linux itself is a desktop-oriented system, it is used for some problems in real-time applications:/> /& gt; Linux system scheduling unit is 10ms, so it can't provide accurate timing interrupt; P> When a process call system call runs in kernel mode, it cannot be preempted;

Linux kernel implementation uses a large number of closed interrupt operation losses;

Because of the use of virtual memory technology, when there is a page fault, it is necessary to exchange the data read from the hard disk, but the randomness of the storage location of hard disk reading and writing will lead to random reading and writing time, which will affect the deadline of real-time tasks in some cases;

Although Linux process scheduler also supports real-time priority, it lacks effective real-time task scheduling mechanism and algorithm. Its network sub-protocol processing and other equipment interrupt processing and scheduling are accompanied by corresponding processes and its own clear scheduling mechanism;

Various real-time Linux systems

Home & gt About

3. 1 RT-Linux and RTAI

RT-Linux is a research project of the University of New Mexico (New Mexico Institute of Technology) [RTLinuxWeb] [Barabanov97. Its basic idea is to realize hard real-time support on Linux system and realize a microkernel real-time operating system (also known as the real-time subsystem of RT-Linux), while ordinary Linux system runs as a low-priority task in the operating system. Another task in the common Linux system can communicate with real-time tasks through FIFO. The framework of RT-Linux is shown in figure 1:

Figure 1 RT-Linux structure

The key technology of RT-Linux is software simulation hardware interrupt controller. When Linux system stops CPU interruption from time to time, the request interception of RT-Linux subsystem can't be put down, but it doesn't really stop hardware interruption, thus avoiding the system response failure for a period of time due to interruption and improving the real-time performance. When a hardware interrupt of RT-Linux passed to the Linux kernel reaches the intercepted interrupt, it is determined whether there is a real-time subsystem interrupt routine to handle or handle it. In addition, the minimum timing accuracy in a normal Linux system is the frequency that determines the real-time clock of the system, and the system clock of Linux is set to the clock interruption of 100 per second, so when the system timing accuracy of Linux is 10 millisecond, that is, the clock period is 10ms, the real-time clock of RT-Linux is set to the one-shot state, which can provide more scheduling granularity of more than ten microseconds.

Priority-driven task scheduling algorithms of RT-Linux real-time subsystem, such as RM and EDF, can also be used for other scheduling algorithms.

RT-Linux's proprietary system, with heavy work, is indeed a good choice, but it only provides the scheduling of CPU resources, and the relationship between real-time system and Linux system is not very close, so developers can make full use of the functions already implemented in Linux system, such as protocol stack. Real-time tasks such as RT-Linux industrial control are simple hard real-time requirements, but a lot of work needs to be done if multimedia processing is to be applied.

Italian real-time application programming interface (RTAI) comes from RT-Linux, and it is designed in the same way as RT-Linux. This is a real-time hardware abstraction layer defined by RTAI on Linux to solve the problems in the original design. This abstraction layer interface provides the interaction of real-time task Linux system, which can increase the real-time support from Linux kernel source code to Linux kernel.

3.2。 KURT-Linux

KURT-Linux developed by the University of Kansas can provide real-time microsecond accuracy [KurtWeb] [Srinivasan]. The real-time kernel is implemented separately from RT-Linux. KURT-Linux is implemented on the basis of commonly used Linux system, which is also the first real-time system based on Linux that can be called by ordinary Linux system.

KURT-Linux system can be divided into three states: normal state, real-time state and mixed state. Under normal conditions, ordinary Linux real-time running state is used to schedule strategic tasks in real time, and the mixed state of real-time and non-real-time tasks can execute real-time state, which can be used to make real-time requirements more stringent.

In order to improve the real-time performance of Linux system, it is necessary to improve the support for accurate clock system. However, if the clock frequency is simply increased, the scheduling load will increase and the performance of the system will be seriously reduced. In order to solve this contradiction, the clock precision method [UTIMEWeb] used in KURT-Linux improves the UTIME of Linux system, and sets the clock chip to a one-shot state (one-shot mode), that is, each clock chip sets a timeout, and then sets a timeout on the clock chip required by the clock interrupt handler when the timeout event occurs again. The basic idea is that accurate time means that when the clock interrupt occurs, we need a more accurate time to achieve such accuracy, but we don't necessarily need the system clock frequency. It uses CPU clock counter and time stamp counter (TSC) to provide accurate CPU frequency and precise time.

KURT-Linux real-time task scheduling adopts the real-time scheduling algorithm of static CPU and is based on time (TD). Real-time tasks require real-time events to occur at the design stage and must be clearly listed. For periodic tasks, the algorithm can achieve better task scheduling.

One advantage of KURT-Linux over RT-Linux is that it can use the Linux system called by the system, which was originally designed for hard real-time support. However, because it is a simple implementation, it will use simple time-driven scheduling instead of Linux scheduling. In some cases, it will be affected by non-real-time tasks such as real-time process scheduling, and the deadline of real-time tasks is fragile and inconsistent, which is the so-called strict real-time system (fast real-time). Applications based on KURT-Linux: art (ATM Reference Transportation System), multimedia playing software. Another method of KURT-Linux requires frequent clock chip programming.

3.3。 Red -Linux

RED-Linux is a real-time Linux system developed by University of California, Irvine [REDWeb] [Wang99], which will support real-time scheduling and Linux to realize the same operating system kernel. It supports three types of scheduling algorithms, namely time-driven dirty first and shared-driven.

In order to improve the scheduling granularity of the system, RED-Linux learned the interrupt management mechanism of RT-Linux software simulation and increased the frequency of clock interrupts. The interrupt simulator of RED-Linux just queues interrupts in the queue. When the hardware interrupt arrives, there is no actual interrupt handler.

In addition, in order to solve the problem that the process of Linux kernel mode can't be interrupted, RED-Linux inserts many functions of Linux kernel preemption primitive, which makes this process preempt to a certain extent in kernel mode. This method improves the real-time performance of the kernel.

The design goal of RED-Linux is to provide a conventional scheduling framework that can support multiple scheduling algorithms. The system adds several attributes for each task. On the basis of process scheduling:

Priority: the priority of the job;

Start time: the start time of the work;

Completion time: the end time of the work; BR p> budget: the amount of resources and work used in the operation process;

Adjust the value? These attributes and scheduling are based on the priority of their use in almost all scheduling algorithms. In this case, three different scheduling algorithms are seamlessly coupled into a unified algorithm.