Linux kernel--process scheduling

Source: Internet
Author: User

The scheduler is part of the kernel and is responsible for selecting the next process to run. The process scheduler can be thought of as a kernel subsystem that allocates a limited amount of processor time between running processes. Linux is a multi-task operating system, only through the rational scheduling program, system resources to the maximum limit of the role of the multi-process will have the effect of concurrent execution.

Multi-task operating system is divided into non-preemptive multi-tasking and preemptive multi-task. Linux is a preemptive multitasking operating system, which is called preemption when the scheduler decides when to stop a process from running so that other processes can get execution opportunities. The time the process can run before being preempted is pre-set, called the process's time slice. A time slice is actually a processor time period that is assigned to each running process. In a non-preemptive multi-tasking mode, it will run until the process stops itself voluntarily.

Strategy
The

      policy determines when the scheduler will let the process run. Processes can be divided into I/O consumption type and processor consumption type. The former refers to the process most of the time used to submit I/O requests or wait I/O requests. As a result, such a process often handles the operational state, but it usually runs for just a few moments because it will eventually block when waiting for more I/O requests. In contrast, processing a consuming process consumes most of the time spent on executing code. Unless they are preempted, they often run continuously because they do not have much I/O requirements. For this type of process to handle consumption, the scheduling strategy is to minimize the frequency of their operation, and for them, it is more appropriate to extend their running time.

      scheduling policies typically look for a balance between two conflicting goals: fast process response (short response times) and maximum system utilization (high throughput). The most basic class of scheduling algorithm is priority-based scheduling. Priority high first run, low after running, the same priority of the process in the rotation mode (one by one, repeated). In some operating systems, including Linux, high-priority processes use a longer slice of time. The scheduler always chooses that the time slice is not exhausted and the high priority process runs. Both the user and the system can influence the scheduling of the system by setting the priority of the process.

      Linux based on the above ideas, a scheduling method based on dynamic priority is implemented. At first, the method sets the basic priority, but it allows the scheduler to add and subtract priority as needed. For example, if a process spends more than its run time on I/O waits, the process is an I/O consuming process. Its priority will be increased dynamically. Conversely, if the entire time of a process is exhausted, the process is a processor-consuming process, and its priority is dynamically degraded.

A time slice is a value that is the time at which a process can continue to run before it is preempted. The scheduling strategy must specify a default time slice, the time slice too long can cause the system to respond poorly to the interaction; it makes it impossible for the system to execute the application concurrently; the time slices will significantly increase the consumption of the process switching, because there will certainly be a significant portion of the system time spent on process switching, The time slices that these processes can use to run are very short. When the time slice of a process is exhausted, it is assumed that the process has expired.

The Linux system is preemptive, and when a process enters the task_running state, the kernel checks to see if its priority is higher than the currently executing process. If so, the scheduler is awakened, seizing the currently running process and running a new, running process. Also, when a process's time slice becomes 0 o'clock, it is preempted and the scheduler is woken up to select a new process.

Scheduler's executable queue

The most basic data structure in the scheduler is the run queue, which is a list of the executable processes on the processor, one for each processor. Each process that can be put into execution is uniquely attributed to an executable queue. Each run queue has two priority series groups, one active and one expired. Priority groups enable each priority of a runnable processor to contain a corresponding queue that contains the list of executable processes on the corresponding priority.

Many operating systems use an explicit method to recalculate the time slices of each process when the time slices for all processes are exhausted. When the time slice of a process is exhausted, it is moved to an expired array, but before that time slice has been recalculated.

Selecting a process and switching to it is done through the schedule () function. When the kernel wants to hibernate, the function is called directly, and if any process is preempted, then the function is invoked. The schedule () function runs independently of each processor. Therefore, each processor makes its own judgment about which process to run next.

The scheduler passes the sleep time of the process to determine whether a process is an I/O consumption type or a processing consumption type. To support this inference mechanism, Linux records the time that a process is used for hibernation and for execution, which exists in the task_struct sleep_avg domain.

The dormant (blocked) process handles a special non-executable state. There are various reasons for process hibernation, but it is certainly in order to wait for some events. The event may be a period of time, read more data from file I/O, or be a hardware event. A common cause of hibernation is a file i/o--, such as a process performing a read () operation on a file, which needs to be read from disk. Also, the process needs to wait while getting the keyboard input. In either case, the kernel operates the same way: the process marks itself as dormant, moves itself out of the executable queue, puts it into the waiting queue, and then calls schedule () to select and execute a different process. The process of awakening is the opposite: the process is set to executable state and then moved from the wait queue to the executable queue.

Hibernation and wake-up status graphs

650) this.width=650; "title=" Hibernate and Wake Switch graph "style=" border-top:0px; border-right:0px; border-bottom:0px; border-left:0px; Display:inline "border=" 0 "alt=" Sleep and wake Switch graph "src=" http://img1.51cto.com/attachment/201409/7/817917_ 141006560760ru.jpg "" 532 "height=" 336 "/>

Load Balancing Program

The Linux scheduler prepares separate executable queues and locks for each processor that has a symmetric, multiple system, which means that each processor has its own list of processes that dispatch operations only for those processes that belong to it. Efficiency, the entire dispatch system is independent from each processor view. What if there are 5 processes on the queue with one processor, and the other has only 1 processes on the queue? These issues are addressed by the load Balancing program, which is responsible for ensuring load balancing between the executable queues. If an imbalance is found, the processes in the relatively busy queue are pumped into the current executable queue. Ideally, the number of processes on each queue should be equal. In a single-processor system, the load balancer is not executed.

Preemption and Context switching

Context switching, that is, switching from one executable process to another executable process. Whenever a new process is selected for operation, Schedule () invokes the context switch function Context_switch ().

Realtime

Linux offers two real-time scheduling strategies: Sched_fifo and SCHED_RR. And the normal, non-real-time scheduling strategy is sched_normal. Sched_fifo implements a simple, first-in, first-out scheduling algorithm that does not use time slices. Processes at the Sched_fifo level are dispatched before any sched_normal-level processes. Once a sched_fifo-level process is in the executable state, it should be executed until it itself is blocked or displayed by the release processor, which is not based on the time slice and can be executed continuously. Only higher-priority SCHED_FIFO or SCHED_RR tasks can preempt sched_fifo tasks. If there are two or more sched_fifo processes, they will be executed in turn. SCHED_RR is roughly the same as Sched_fifo, except that the SCHED_RR-level process can no longer be executed after exhausting the time allotted to it beforehand. Both of these real-time algorithms implement static precedence.

Linux kernel--process scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.