What is the difference between Linux process priority NI and PR?

  

Why should there be process priority?

This does not seem to require too much explanation. After all, since the multitasking operating system
was born, the ability of the process to take up cpu is a matter that must be artificially controlled. Because some processes are relatively important, and some processes are less important.

The way the process priority works has basically changed since the invention. Whether it is only one cpu era or the multi-core cpu era, it is achieved by controlling the length of cpu time occupied by the process.

This means that in the same scheduling cycle, the higher priority process takes longer, while the lower priority process takes shorter.

Please do not really confuse these two concepts in the system: nice (NI) and priority (PR), they are inextricably linked, but for the current Linux system, they Not the same concept.

We look at this command:


Do you really understand the difference between the specific meaning of the PRI column and the NI column?

The same, if it is the top command:


Do you figure out the difference between PR and NI? If not, then we can first figure out what is the nice value.

What is the NICE value?

The NICE value should be a familiar concept for those familiar with Linux/UNIX. It is a value that reflects the status of a process "priority", which ranges from -20 to 19, a total of 40 level.

The smaller the value, the higher the process”priority>, and the higher the value “priority"

For example, we can use the NICE command to set the NICE value of a bash command to be executed by:
[root@zorrozou-pc0 zorro]# nice -n 10 bash

This way I open a bash again, and its nice value is set to 10, and by default, the priority of the process should be inherited from the parent process, this value is generally 0.

We can directly view the nice value of the current shell by the nice command:
[root@zorrozou-pc0 zorro]# nice 10

Compare the normal situation:
[root @zorrozou-pc0 zorro]# exit

Exit the current bash with a value of 10 and open a normal bash. Let's look at its Nice value:
[root@zorrozou-pc0 zorro]# bash< Br>[root@zorrozou-pc0 zorro]# nice 0

In addition, the renice command can be used to adjust the nice value of a running process. We can also use the commands such as top, ps, etc. to view the process. Nice value, I will not say more about the specific method, you can refer to the relevant man page.

What everyone needs to pay attention to is that I am using the name nice value here, not the priority.

The value of nice is not a priority, but it does affect the priority of the process.

In English, if we describe a person nice, it generally means that this person's popularity is better. What kind of people are good? Often humbly and polite.

For example, if you go to lunch with a nice person, you order two identical meals. After you get one, the nice one will say: "You eat first, you eat first." ! ”, this is a good relationship, this person nice! But if the other one is late, then the nice person is going to be hungry.

What does this mean?

The more Nicole's ability to seize resources, the worse, and the less nice, the stronger the ability to seize. This is the meaning of the size of the nice value, the lower the nice value, the less the process is less, the stronger the ability to seize the cpu, the higher the priority (the author's explanation is too vivid, Xiao Bian could not help but manually praise! !).

In the original Linux using O1 scheduling, we will also call the nice value static priority, which is basically consistent with the characteristics of the nice value, that is, when the nice value is set, unless we use renice Change it, otherwise it will be unchanged.

The value of priority is changed on the previous O1 scheduler of the kernel, so it is also called dynamic priority.

What are the priorities and real-time processes?

Let's take a look at what is the priority value, which is the PRI value seen in the ps command or the PR value seen in the top command.

In order to distinguish these concepts, the following:

Unified to use the value of nie to represent the NI value, or static priority, that is, the priority adjusted with the nice and renice commands;

The practical priority value indicates the PRI and PR values, or dynamic priority.

We also unify the concept of the word "priority" as the meaning of the priority value.

In the kernel, the value of the process priority is defined by a macro whose name is MAX_PRIO and whose value is 140.

This value is further composed of two additional values, one is the NICE_WIDTH macro representing the value range of the nice value, and the other is the MAX_RT_PRIO macro representing the real-time priority range of the realtime process.

To put it plainly, Linux actually implements 140 priority ranges, ranging from 0-139. The smaller the value, the higher the priority. The nice value of -20 to 19, mapped to the actual priority range is 100-139.

The default priority of the newly generated process is defined as:
#define DEFAULT_PRIO (MAX_RT_PRIO + NICE_WIDTH /2)

The actual corresponding value is 0 for the nice value.

Under normal circumstances, the priority of any process is this value, even if we adjust the priority of the process through the nice and renice commands, its value range will not exceed the range of 100-139, Unless the process is a real-time process, its priority value will become one of the range 0-99.

There is a message here that the current Linux is an operating system that already supports real-time processes.

What is a real-time operating system?

We will not explain the meaning of this in detail and its application in the industrial field. If you are interested, you can refer to the Wikipedia of real-time operating system.

In a nutshell, the real-time operating system needs to ensure that the relevant real-time processes respond in a short period of time without long delays and require minimal interrupt latency and process switching latency.

For such a demand, the general process scheduling algorithm, whether O1 or CFS, cannot be satisfied, so the kernel will map 100 priority priorities to the real-time process at the time of design. To be higher than the priority of the normal process (nice value), and the scheduling algorithm of the real-time process is different, they use a simpler scheduling algorithm to reduce the scheduling overhead.

In general, processes running on Linux systems can be divided into two categories:

  • Real-time processes
  • Non-real-time processes

    Their The main difference is the distinction by priority.

    All priority values ​​in the range of 0-99 are real-time processes, so this priority range can also be called real-time process priority, while non-real-time processes are in the 100-139 range.

    The chrt command can be used in the system to view and set the real-time priority status of a process. We can first look at the use of the chrt command:


    We will first pay attention to the displayed Policy options section, and we will find that the system provides five scheduling strategies for various processes.

    But it is not stated here that the five scheduling strategies are used for the two processes respectively. The scheduling strategies that can be used for real-time processes are: SCHED_FIFO, SCHED_RR, and for non-real-time processes: SCHED_OTHER, SCHED_OTHER, SCHED_IDLE.

    The overall priority policy of the system is:

  • If there is a real-time process in the system that needs to be executed, the real-time process will be executed first;
  • until the real-time process exits or actively gives up The non-real-time process is scheduled to be executed when the CPU is running.

    The real-time process can specify a priority range of 1-99. The method to execute a program to be executed in real time is:
    [root@zorrozou-pc0 zorro]# chrt 10 Bash
    [root@zorrozou-pc0 zorro]# chrt -p $$
    pid 14840's current scheduling policy: SCHED_RR
    pid 14840's current scheduling priority: 10

  • Copyright © Windows knowledge All Rights Reserved