Linux Process Priority and nice value, linux priority nice
After learning the complete Fair Scheduling Algorithm (CFS) of linux, record the learning track
This article mainly describes how the scheduling algorithm works and some scheduling knowledge.
We can view the details of the currently running process through ps-l.
- F: indicates the process flag and identifies the permissions owned by the process. When I switch to root, "4" indicates that the process has root permissions, and 1 has only fork () permissions.
- R: indicates the current state UID of the process: the user ID that owns the process.
- R: RUNNING)
- S: SLEEP)
- D: cannot be interrupted.
- T: STOP)
- Z: ZOMBIE)
- PID: process ID
- PPID: The process of the parent process is good.
- C: CPU usage percentage
- PRI: Priority
- NI: nice Value
- ADDR/SZ/WCHAN: memory-related
- TTY: the login terminal, which is not related to remote login
- TIME: CPU usage TIME
- CMD: The command that causes this process
This lecture focuses on PRI and NI.
We divide the main process into two types: 1. I/O-consuming processes; 2. processor-consuming processes, of course, are both I/O-consuming and processor-consuming processes.
I/O-consuming processes, literally, consume most of their time on I/O requests and wait for I/O. There is very little time to use CPU, the processor consumable process will spend most of its time using CPU for computing and so on. If the length of the time slice allocated for these two kinds of inheritance is equal, it will be unfair.
At the same time, we want to give CPU-consuming processes more time than I/O consumable processes, so linux does not adopt a simple time slice scheduling algorithm, instead, the improved priority scheduling algorithm CFS
Linux is preemptible.What is the concept? The system is currently running a process, but a process with a higher priority suddenly gets a certain resource into the ready state, then he came to the front of the cpu and kicked off the running process (you play the Kay, I'll come first) to snatch the good time spent with the CPU...
CFS directly allocates not time slices, but the CPU usage ratio. This proportion will be affected by nice values. You know, nice values have a high proportion and nice values have a low proportion.
Q:The CFS scheduler used in Linux determines the time to seize the CPU usage ratio described above. Note that!Instead, new processes with lower CPU usage will be immediately put into operation, and new processes with higher CPU usage will be delayed.Why?
A:BecauseStarting Point of CFSBased on a very simple concept:The effect of process scheduling should be like that the system has an ideal perfect multi-task processor. In this system, each process can obtain a 1/n processor time.
That is to say, each processCpu usage timeIt is the same, including the I/O and processor consumption types to achieve true fairness, which explains the problem just now, if the CPU usage is lower than the CPU usage, it will inevitably be less than the CPU usage, so we have to let this process have the ability to seize, and we can seize it as soon as we are ready.Looks likeCPU usage is higher than "(in fact not changed)"Looks likeCPU usage is as long as other processes "(actually not much)
The preemption capability is high, and the interaction is relatively good.You can see that when you play a game, the interaction will be high. Otherwise, if you press a key to put a skill computer and don't handle it for you, you will be pitted on your teammates.
- Linux uses a completely fair scheduling algorithm (CFS)
- In Linux, process scheduling does not directly share time slices, but improves the priority,Two different priority ranges are adopted. One is nice, and the range is-20 to + 19. A larger nice value means a lower priority, processes with low nice values get more processor time (in proportion), and the second range is real-time priority. The value is configurable, by default, the value ranges from 0 to 99, which is opposite to the nice value. The higher the real-time priority value means that the higher the process priority, the higher the priority of any real-time process.
- PS: The nice value will constantly change the old priority. Of course, you can also set the nice value. The nice value must be root if it is negative.
- Nice value is not a priority, but it will affect the priority of PRI (new) = PRI (old) + nice
- Too long time slice will lead to poor human-computer interaction, while too short time slice will lead to a large amount of processor time waste on process switching.And the unfair processor time between I/O-consuming and processor-consuming processes also appears
- Instead of directly allocating time slices to the process, CFS distributes the CPU usage to the process, which is also affected by the nice value.
- CFS allows each process to run for a period of time and rotate cyclically,Select the least-running process to write a running process.So according to the meaning of nice value-the percentage of processor used, run according to the "time slice" of the proportion according to the total number of processes that can be run in the system
- In linux, I always want to allocate N processes with the same running time as each process.When N tends to be infinite, it should be saidCan be allocated to an infinitely small period of timeBut it will be terrible, and we cannot allocate an infinitely small period of time,Although a smaller period of time can bring better interactivity, it still leads to unacceptable switching consumption., SoTarget latencyTo simulate the approximate value of an infinitely small scheduling cycle. Now, if the target latency is 20 ms, divide it by the number of processes that can be run now to get the length of time slice for each process, when the number of processes is infinite, the time allocated by each process tends to be infinitely small, which is very good, and the process switching has exploded. If we set the minimum value to 1 ms, when the number of processes is too large, I also ensure that each process gets a 1 ms running time before being forcibly occupied, so this 1 ms is calledMinimum Granularity