In-depth analysis of Linux kernel source code-Process Scheduling (2) (1)

Source: Internet
Author: User

3 Linux scheduler-Schedule ()

3.1 Basic Principles

The essence of scheduling is the allocation of resources. The system uses different Scheduling algorithms Scheduling Algorithm) to allocate such resources. Generally, the selected Scheduling algorithm depends on the resource allocation Policy Scheduling Policy. Here, only the several algorithms related to Linux Scheduling and their principles are described.

A good scheduling algorithm should consider the following aspects:

1) Fairness: ensure that each process has a reasonable CPU time.

2) Efficiency: Keep the CPU busy, that is, there are always processes running on the CPU.

3) Response Time: make the response time of the interactive user as short as possible.

4) Turnaround time: the time for batch processing users to wait for output is as short as possible.

5) throughput: make the number of processes processed per unit time as much as possible.

Obviously, these five goals cannot be achieved at the same time. Therefore, different operating systems will make appropriate trade-offs in these aspects to determine their own scheduling algorithms, for example, UNIX adopts dynamic priority scheduling, BSD adopts multi-level feedback queue scheduling, and Windows adopts preemptive multi-task scheduling.

The following describes the main scheduling algorithms and their basic principles:

1. time slice rotation scheduling algorithm

Time Slice) is a period of Time allocated to the process for running. In the general rotation method, the system queues all processes that are ready to run according to the principle of first-come first-served, and allocates the CPU to the first process during each scheduling, and execute a time slice. When the time slice is used up, the system sends a signal to notify the scheduling program. Then, the scheduling program stops the process and sends it to the end of the running queue, wait for the next execution. Then, allocate the processor to the new first process in the ready queue and let it execute a time slice. In this way, all processes in the running queue can obtain the processing time of the time slice at a given time.

2. Priority Scheduling Algorithm

In order to take care of the priority of pressing processes after they enter the system, the highest priority scheduling algorithm is introduced. When the algorithm is used for process scheduling, the system assigns the processor to the process with the highest priority in the running queue. At this time, the algorithm can be further divided into two methods:

(1) Non-preemptible priority algorithm, also known as non-deprivation Scheduling: Nonpreemptive Scheduling)

In this way, once the system allocates the CPU of the processor to the process with the highest priority in the running queue, the process continues until it is completed; or when the process is abandoned due to an event, the system can allocate the processor to another high-priority process. This scheduling algorithm is mainly used in the batch processing system and in some real-time systems that do not have strict real-time requirements.

(2) preemptible Priority Scheduling Algorithm (Preemptive Scheduling)

The essence of this algorithm is that the processes currently running in the system always have the highest priority in the processes that can run. When this scheduling algorithm is used, a new executable process is compared with the current running process. If it is higher than the current process, process scheduling is triggered. This priority scheduling algorithm can better meet the requirements of urgent processes. Therefore, it is often used in real-time systems with strict requirements, as well as batch processing and time-sharing systems with high performance requirements. Linux also uses this scheduling algorithm.

3. Multi-Level feedback queue Scheduling

This is the most fashionable scheduling algorithm nowadays. The essence is: combining the advantages of time slice rotation scheduling and preemptible priority scheduling, that is, a process with the same priority runs the given time slice first, and a process with the same priority runs the given time slice in turn.

4. Real-Time Scheduling

Finally, let's take a look at the scheduling in the real-time system. What is a real-time system is that the system responds to external events as soon as possible. In real-time systems, preemptive scheduling is widely used, especially for real-time systems with strict requirements. This scheduling method is not only flexible, but also has a small latency. However, this scheduling method is also complicated.

3.2 Linux Process Scheduling time

The scheduling program in Linux is a Schedule function, which is called frequently and determines whether to switch the process, which process to switch. Let's first look at the circumstances in which the scheduling program will be executed. We call this situation the scheduling time.

Linux scheduling time mainly includes:

1. process state transition time: Process Termination and process sleep;

2. When the time slice of the current process is used up, current-> counter = 0 );

3. the device driver actively calls schedule;

4. When a process is returned to the user State from interruption, exception, or system call;

Time 1: The process must call sleep) or exit functions to convert the status. These functions will call the scheduler to schedule the process;

Timing 2: because the time slice of the process is updated by clock interruption, this situation is the same as timing 4.


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.