Process Scheduling (I) -- Design and Implementation of Linux kernel reading notes

Source: Internet
Author: User
The scheduler of a process is a kernel subsystem that ensures that the process can work effectively. The scheduler is responsible for deciding which process to run, when to run, and when to run

The scheduler of a process is a kernel subsystem that ensures that the process can work effectively. The scheduler is responsible for deciding which process to run, when to run, and how long to run. To put it simply, a scheduler allocates processing time to a pile of ready processes, and a scheduler is the basis of a multi-task operating system. The principle of the scheduler is to use the cpu resources to the maximum extent. that is to say, when a running process exists in the system, the cpu cannot be idle, if there is no ready process in the system, the cpu runs an idle process.

1. multi-task

A multi-task operating system is an operating system that allows concurrent interaction of multiple processes. Note that concurrency is used instead of parallelism. If your computer has two or more CPUs, your computer can actually execute multiple tasks simultaneously and concurrently. Multi-task operating systems can be divided into two categories: preemptible multi-task and non-preemptible multi-task.

In a preemptible multi-task, the scheduler determines when to stop the execution of a process. preemption is an action that the scheduler forces to stop the execution of a process ). The running time of a process before it is preemptible is fixed and has a special name called timeslice ). The time slice is actually the processor time period allocated to each process.

Rather than preemptible multitasking, the process makes concessions on its own. after a period of execution, the process voluntarily gives up the cpu. A process actively suspends its own operation is called a concession (yielding). If a process is suspended and refuses to make a concession, the operating system may crash.

So in the above two cases, preemptible multitasking is like "legal", and you will be removed as long as the time is up. Instead of preemption, it is like "morality". if you have morality, after a while, you will withdraw yourself. if some "people" Occupy the pitfalls, there is no other way for other processes to condemn it with morality.

2. linux process scheduling

In linux, the initial process scheduler is very primitive, and it is difficult to adapt to a number of runable processes and multi-processor environments. Later, starting from linux2.5, I made a major adjustment to the linux process scheduler and used a scheduling algorithm called O (1). This algorithm was named after the algorithm behavior. O (1) Although the scheduling algorithm shows almost perfect features and scalability on dozens of processors, however, this algorithm does not deliver ideal results when scheduling interaction processes. Therefore, in the early stage of development of linux2.6, the CFS algorithm was proposed, that is, the completely fair scheduling algorithm.

3. Strategy (1) IO-consuming processes and processor-consuming processes

IO consumable processes refer to processes that are mostly used to wait for IO operations. the sample user interface (GUI) program is an IO consumable program, this program needs to constantly listen to user input. Such a process is often in a running state, but each running time is very short.

Processor consumable processes are processes that spend most of their time executing code. for example, MATLAB, a large computing program, is a processor consumable process.

Some applications are divided into IO-consuming processes, but also have the characteristics of processor-consuming processes. For example, a word processing program may wait for input from a user most of the time, but at some time the program may stick to the processor for crazy checks on syntax and spelling errors.

The scheduler needs to find a balance between the two conflicting targets-rapid response of processes and high throughput. Unix and linux tend to schedule IO-consuming processes to achieve good user response.

(2) process priority

One of the most basic types of scheduling algorithms is scheduling based on the process priority. this is an idea to classify processes based on the value of the process and its time requirements for the processor. Generally, a process with a higher priority is executed first and then run later. a process with the same priority is scheduled in rotation mode (one by one, repeated ). In some operating systems, the time slice for high-priority processes is also longer. The scheduler always selects a process with a high priority and the time slice has not been exhausted.

Linux uses two types of priorities. The first is the nice value, which ranges from-20 to + 19. a larger value indicates a lower priority. This priority applies to general processes.

In addition, linux uses real-time priority for real-time processes. The value ranges from 0 to 99. a larger value indicates a higher priority. The priority of real-time processes is higher than that of normal processes. Therefore, the priority of these two processes is in the range of two different processes.

(3) time slice

A time slice is a numeric value that indicates the duration for a process to run until it is preemptible. If the time slice is too long, the system will not respond well to the interaction. if the time slice is too short, it will significantly increase the processor time consumption caused by process switching. Therefore, the contradiction between IO-consuming processes and processor-consuming processes is once again apparent. IO-consuming processes do not require long time slice, while processor-consuming processes expect the longer the time slice, the better.

Long time slice will lead to poor interactive performance of the system. many operating systems pay attention to this point, so the time slice is set very short, such as 10 ms. However, the CFS scheduling algorithm in linux does not directly allocate time slices to processes. Instead, it gives the CPU usage score to the processes. in this way, the time obtained by the processes is closely related to the system load. This proportion will also be affected by the nice value. The nice value as the weight will adjust the processor time usage ratio of the process. Processes with a higher nice value (lower priority) will be given a lower weight, resulting in a loss of a small portion of the processor usage ratio, with a lower nice value (higher priority) to obtain more processor usage ratios.

In most preemptible operating systems, whether a new ready process can be executed immediately (that is, to seize the original process) depends entirely on the priority of the new process and whether there is a time slice. The preemptible time of the CFS scheduler used in linux depends on the CPU usage ratio of the new process. If the CPU usage of the new ready process is lower than that of the current process, then the new process preemptible the current process and immediately put into operation.

Related Article

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.