Linux Kernel Process Scheduling

Source: Internet
Author: User

Processes can be divided into I/O consumption type and processor consumption type.
I/O-consuming processes spend most of their time submitting IO requests or waiting for IO requests. Such processes are often in a running state, but they usually run for a short time, because it is always blocked when waiting for more IO requests. (I/O includes keyboard activity, not limited to disk I/O)

CPU consumption: The time is spent on the Execution Code. Unless they are preemptible, they usually run continuously. Considering the speed of the system, such processes should not run frequently, the scheduling policy is to minimize their running frequency.

The scheduling policy seeks a balance between two contradictions: Process Response Speed (short response time) and maximum System Utilization Rate (high throughput)

Process Scheduling is more inclined to schedule io-consuming processes.

The scheduling algorithm is based on the priority, that is, the process with the highest priority runs first, and the process with the same priority runs at the lower priority according to the rotation mode. (The time slice of the process with the highest priority is also long ), the scheduler always selects the process with the highest priority and the time slice is not used up.

Linux implements a Dynamic Priority Scheduling Algorithm Based on the above ideas. For example, if a process spends more time on Io wait than it runs, the process is obviously an IO-consuming process, and its dynamic priority will be increased. If all the time slices of a process are exhausted, the dynamic priority of the process is reduced if it is a CPU-consuming process.

The Linux Kernel provides two priority ranges:

1. Nice value: (static priority)
-20 to + 19
The default value is 0.
The length of the time slice is 800 100 5 ms
2. Real-time priority
The default value range is 0 to 99. Any real-time process has a higher priority than a common process. Support for POSIX real-time priority in Linux

A process has an initial priority, that is, a static priority. After being specified by the user, the priority cannot be modified. The dynamic priority is based on the nice value plus the rewards and penalty points for interaction between-5 and + 5 processes.

For example, a highly interactive process, even if its nice value is 10, its dynamic priority may eventually reach 5. On the contrary, a mild processor swallowed up, although the original nice value is 10, its final dynamic priority may be 12.

The scheduler determines whether the process is Io-consuming or processor-consuming based on the process sleep time.

For example:
A process is sleeping most of the time, so it is Io-consuming. If a process runs longer than the sleep time, it is a processor-consuming type.

Process type nice value length of time slice
----------------------------------------------------------------------------
The value of the initial process parent process is half of that of the parent process.
Processes with the lowest priority + 19 5 milliseconds (min_timeslick)
Process with the default priority 0 100 milliseconds (def_timeslick)
Process with the highest priority-20 800 milliseconds (max_timeslick)
 
========================================================== ==========================================

Time slice recalculation:

It calculates time slices based on static priorities.

When a process is created, the newly created sub-process and parent process are equally divided into the remaining process time slices of the parent process.
When each process fork a new process, the time slice of the Parent and Child processes is half of the time slice before the parent process fork.
The child process shares the time slice of the parent process.

Process Preemption

When a process enters the task_running status, the kernel checks whether the priority of the process is higher than that of the currently running process. If yes, the scheduler will be awakened, this command is used to seize the currently running process and run a new runnable process. When the time slice of a process changes to 0, it is preemptible and the scheduler is awakened to select a new process.

The linux2.6 kernel has the following goals:
1. Fully implement O (1)
2. Full SMP scalability each processor has its own lock and executable queue
3. Enhance SMP affinity. assign a group of tasks to a CPU for continuous execution. The process is moved between CPUs only when the task queue needs to be balanced.
4. Enhanced interoperability. Even if the system is under considerable load, the system can respond and schedule interactive processes immediately.
5. ensure fairness. In a reasonably set time range, no process will be in the hunger state, and no process will be able to obtain a large number of time slices in an unfair manner.
6. obviously, the most common optimization scenario is that there are only one or two processes in the system that can run, but the optimization is also fully capable of extending to a system with multiple processors and running multiple processes on each processor.

Process sleep and wake up

The sleeping process is in a special non-executable state. The two statuses of sleeping processes are task_interruptible and task_uninterruptible.
The only difference between them is that the process in task_uninterruptible ignores the signal, and the process in task_interruptible state will wake up and respond to the signal in advance if it receives a signal. both processes are in the same waiting queue, waiting for some events to run.

Load Balancing Program
 
It ensures that the load between executable queues is balanced. The Load Balancing Program compares the executable queue of the current processor with other runable queues in the system. if it finds an imbalance, the processes in the relatively busy queue will be drawn to the currently runable queue. the process data attached to the program is very close to each other so that the process data on each queue is equal.

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.