First, I want to use a simple command to cause this issue. In Linux or Unix systems, the following output content is similar to the PS-l command:
[Root @ dbbak root] # ps-L-Linux testing is used here
F s uid PID ppid C pri Ni addr sz wchan tty time cmd
4 S 0 20538 20534 0 75 0-1063 wait4 pts/2 00:00:00 bash
4 R 0 20663 20538 0 75 0-828-pts/2 00:00:00 PS
We can easily notice several important information, including:
UID: the identity of the performer.
PID: indicates the code of the process.
Ppid: indicates which process the process is derived from, that is, the code of the parent process.
Pri: indicates the priority of the process that can be executed. The smaller the value, the earlier it is executed.
Ni: indicates the nice value of the process.
The first three pieces of information are easy to understand, but the last two strange pieces of information are PRI and Ni. What exactly is this? Relatively speaking, PRI is better understood, that is, the priority of the process, or the general point is the sequence in which the program is executed by the CPU. The smaller the value, the higher the priority of the process. What about Ni? That is, the nice value that we want to talk about. It indicates the value of the priority that the process can be executed. As mentioned above, the smaller the PRI value, the faster it will be executed. After the nice value is added, the PRI will be changed to: PRI (new) = PRI (old) + nice. In this way, when the nice value is negative, the program will have a smaller priority value, that is, the higher the priority, the faster the program will be executed.
Processes are not equal when they are created. They are given different priority values, for example, some programs that are important to the operations of the computer itself must have a higher priority than other secondary programs (otherwise, they have a smaller priority value ). As mentioned above, the nice value indicates that the process priority value can be corrected. Therefore, each process is assigned a nice value to the system during its plan execution, in this way, the system can actively intervene in the priority value of the process based on system resources and various resource consumption conditions of specific processes. In this process, you can also Manually intervene in the process, but you must be granted the corresponding permissions.
In Unix or Linux systems, a variable value from-20 to + 19 is used to represent the nice value (this is the case for Linux and Aix, the value range of the HP-UX system is from 0 to 39), and in general, the child process inherits the system nice value of the parent process. A program with the highest priority has the lowest nice value, so in UNIX and Linux systems, value-20 makes a task very important (HP-UX is 0); in contrast, if the nice of the task is + 19 (HP-UX is 39), it indicates it is a noble, selfless task, allow all other tasks to have a larger share of CPU time than they can have. This is what nice names mean.
During system startup, The INIT process is assigned 0 (the HP-UX system value is assigned 20 ), all other processes inherit this nice value (because other processes are init sub-processes ). Users can log on to the initialization process from the beginning, and other child processes derived from the process will also assign the nice value. The original user can only increase the nice value of the execution process under it (relative to its own nice value) but not smaller. to decrease the value, you must have the corresponding permissions, for example, the root user can reduce the number as follows:
[Oracle @ perf_dbc ~] $ Nice
0
[Oracle @ perf_dbc ~] $ Nice-N 3 ls
Agent bin important_bak logs statistics_import.log tmp_forum_stats.dmp tmp_taobao_stats.dmp tmp_tbcat_stats.dmp top. dmp worksh
[Oracle @ perf_dbc ~] $
[Oracle @ perf_dbc ~] $ Nice-n-3 ls
Nice: cannot set priority: Permission denied
The root user can give the sub-process a smaller Nice value, as shown below:
[Root @ dbbak root] # Nice
0
[Root @ dbbak root] # Nice-n-3 ls
192.168.205.191.txt anaconda-ks.cfg clariion. Log desktop disk1 EMC. Sh file_sort install. Log install. log. syslog log ops rhel_ OS _soft root_link_name
For background processes, its nice will add 4 after the value is displayed. For example, when the nice 12 command & 'command is executed, it runs the program (HP-UX system) with nice = 36 ). There is a problem here, that is, what if the nice value set by the user exceeds the nice boundary value (Linux and Aix are-20 to 19, HP-UX is 0 to 39, then the system takes the nice boundary value as the nice value of the process.
There are two nice and renice commands related to the process.
The nice command is to set the nice value of the command process to be executed. The command format is nice-N Adjustment Command command_option. Here we will set the nice value of the command to be executed. If adjustment is not specified here, the default value is 10.
The renice command is used to set the nice value of a running process. For example, if the nice value of a running process is 0 and the renice value is 3, the nice value of the running process is 3. Renice must have corresponding permissions for execution. It can set the nice value of a process based on the user, process ID, and process group.
Assume that there are two runnable processes A and B in a CPU rotation, and if their nice values are both 0 (20 for a HP-UX ), in addition, the kernel will allocate 1 k cpu time slices to each of them. However, if process a is 0 but B is-10, then the CPU may allocate 1 K and K time slices to A and B respectively. The nice value affects the number of CPU time slices allocated to processes by the kernel. The higher the priority of a process with more time slices, the lower the priority value.
The nice value seen from the use of top, PS, and other commands is the nice value owned by the process, and % nice seen by iostat, etc, that is, the percentage of CPU used by the process that has changed the priority in the process space. In the preceding example, 0.5 K/2.5 k = 1/5 = 20%.
So far, it should be emphasized that the Nice value of a process is not the priority of the process. They are not a concept, but the nice value of the process will affect the priority change of the process.