After learning the complete fair scheduling algorithm (CFS) of Linux, the learning trajectory is recorded
This article focuses on the working methods of the fully fair scheduling algorithm, and some scheduling knowledge
We can see the details of the currently running process through PS-L, where
- F: Represents the process flag, identifies the permissions that the process has, and when I switch to root, 4 means root, and 1 has only fork () permissions
- R: Indicates the current state of the process
- R: currently running (RUNNING)
- S: Sleeping (Sleep)
- D: Non-disruptive
- T: Stop (stop)
- Z: Zombie Process (ZOMBIE)
- UID: The user ID of the user who owns the process
- PID: Process Number
- PPID: The process of the process parent process is good
- C:CPU is used as a percentage
- PRI: Priority
- Ni:nice value
- Addr/sz/wchan: all memory-related
- TTY: The terminal of the lander, and the remote login is not open to the relationship
- Time: CPU-intensive
- CMD: The command that caused this process
The main focus of this talk is about the PRI and the NI.
We divide the main process into two types: 1. I/O consumption process; 2. processor-intensive processes there are also processes that are both I/O consumption and processor consumption
The I/O consumption process, like literally, spends most of its time on I/O requests and waiting I/O, with very little time to actually use the CPU, and the processor consuming process spends most of its time using the CPU for calculations, and if the length of time slices for these two inheritance allocations are equal, will show injustice.
At the same time we want to give processor consumption process more processor time, while the I/O consumption process less processor time, so Linux is not a simple time-slice scheduling algorithm, but the improved priority scheduling algorithm CFS
Linux system is preemptive , what concept, the system is currently running a process, but this time a higher priority of the process suddenly get a resource into a ready state, then he came to the CPU to kick open the running process (you play Kay, I first came to rob the good times with the CPU ...
CFS directly distributes not time slices, but CPU usage ratio, this ratio will receive nice is worth the impact, then you understand, nice value low gravity is high, nice high proportion is low
Q: Linux uses the CFS scheduler, the decision to seize the time is the above said CPU use ratio, pay attention! instead, a new process with lower CPU usage will run immediately, and a new process with higher CPU usage will delay running , for what?
A: since the starting point of CFS is based on a very simple concept: the effect of process scheduling should be as good as the ideal multitasking processor in the system. In this system, each process will be able to obtain 1/n processor time
That is, each process really uses the CPU time is the same, including I/O consumption and processor consumption to achieve true fairness, which explains the problem just now, CPU usage is less than the low occupation time will inevitably lower than the high-occupancy process, Then we have to let this process have the ability to seize, a ready to preempt, this looks like "CPU usage is higher than" (in fact, it does not change) " looks like CPU time is as much as other processes" (in fact, not many)
preemption Ability is high, the interaction is relatively good , you see you people play a game, interactivity is high, or you press a key to put a skill computer does not give you processing, you will pit teammates
- Linux uses a fully fair scheduling algorithm (CFS)
- Linux process scheduling does not use the direct split time slices, but the priority has been improved, with two different priority ranges, one is the nice value, the range is 20 to +19, the larger the nice value means lower priority, A process with a low nice value gets more processor time (proportionally), the second range is real-time priority, its value is configurable, and by default it varies from 0 to 99, which is the opposite of the Nice value, and the higher the real-time priority value means the higher the process priority Any real-time process has a higher priority than the normal process
- The Ps:nice value also keeps changing the old priority and, of course, you can set the nice value, which must be root
- The nice value is not a priority, but it affects the priority PRI (new) =pri (old) +nice
- Too long a time slice can lead to poor human-computer interaction, and too short a time slice can cause a lot of processor time to be wasted on process switching , and the unfairness of processor time between I/O consuming processes and processor-consuming processes is also apparent.
- CFS does not directly allocate the time slice to the process, but instead assigns the processor's use gestures to the process, which is also affected by the Nice value
- The CFS approach is to allow each process to run for a period of time, cycle round, choose the least running process as a running process , so according to the meaning of the Nice value-the percentage of processor occupied, according to the total number of running processes in the system to run according to the proportion of "time slice"
- Linux design always wants to assign to n processes the same number of processor runs per process , when n tends to infinity, it can be allocated to an infinitely small time period , but it would be very bad, we can not allocate an infinite period of time, Although the smaller time period can lead to better interactivity, it still brings unacceptable switching consumption , so a target delay is introduced to simulate the approximate value of an infinitely small dispatch period, now assuming that the target delay is 20ms, By dividing it by the number of processes that are currently running, you can get the time slice length that each process gets, and when the number of processes is infinite, the time allocated for each process tends to be infinitely small, good, and the process switch explodes, so if the minimum is set to 1ms, Number of processes I also guarantee that each process gets 1ms uptime before being taken, so this 1ms is called the minimum granularity
Linux process priority and nice values