Linux Process Priority and nice Value
After learning the complete and fair scheduling algorithm (CFS) in Linux, record the learning trajectory. This article mainly describes how the fully fair 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 of the process.
- R: RUNNING)
- S: SLEEP)
- D: cannot be interrupted.
- T: STOP)
- Z: ZOMBIE)
- UID: User ID that owns the process user
- 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 ready, 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 CPU usage ratio as mentioned above. Note that! On the contrary, new processes with low CPU usage will be immediately put into operation, and new processes with high CPU usage will be delayed. Why?
A: The starting point of CFS is based on A very simple concept: process scheduling should be as effective as A perfect multi-task processor in the system. In this system, each process can obtain a 1/n processor time.
That is to say, the cpu usage time of each process is the same, including I/O and processor consumption to achieve true fairness. This 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 it will be ready to seize, in this way, "it seems that the CPU usage is high" (In fact it hasn't changed) "It seems that the CPU usage time is as high as that of other processes" (actually not much)
The ability to compete is high, and the interaction is relatively good. You can see that when you play a game, the interaction will be high. Otherwise, you will not be able to put a skill computer by pressing a key, you're hitting your teammates.
- Linux uses a completely fair scheduling algorithm (CFS)
- In Linux, process scheduling does not directly evenly distribute time slices. Instead, it improves the priority and uses two different priority ranges, one of which is nice, the value range is-20 to + 19. A larger nice value means a lower priority. A process with a lower nice value gets more processor time (in proportion ), the second range is the real-time priority, and its value is configurable. By default, it 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, moreover, the unfair time between I/O-consuming and processor-consuming processes is also apparent.
- 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.
- The CFS practice is to allow each process to run for a period of time and rotate cyclically. Select the least-running process as the write process. Therefore, according to the meaning of nice value-the percentage of CPU occupied, based on the total number of processes that can be run in the system.
- In linux, we always want to allocate the same running time to N processes. When N tends to be infinite, it is reasonable to say that it can be allocated to an infinitely small time period, however, doing so would be terrible, and we could not allocate an infinitely small period of time. Although a smaller period of time can bring better interactivity, it still leads to unacceptable switching consumption, therefore, a target latency is introduced to simulate the approximate value of an infinitely small scheduling cycle. Now we assume that 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 and very good, the process switching has exploded, so if we set the minimum value to 1 ms, and the number of processes is too large, I also ensure that each process gets 1 ms of running time before being forcibly occupied, this 1 ms is called the minimum granularity.
This article permanently updates the link address: