使用nice命令调整进程优先级

Adjusting Process Priority with nice  

When Linux processes are started, they are started with a specific priority. By default, all regular processes are equal and are started with the same priority, which is the priority number 20. In some cases, it is useful to change the default priority that was assigned to the process when it was started. You can do that using the  nice  and  renice  commands. Use  nice  if you want to start a process with an adjusted priority. Use  renice  to change the priority for a currently active process. Alternatively, you can use the  r  command from the  top  utility to change the priority of a currently running  process.  

Changing process priority may make sense in two different scenarios. Suppose, for example, that you are about to start a backup job that does not necessarily have to finish fast. Typically, backup jobs are rather resource intensive, so you might want to start it in a way that it is not annoying other users too much, by lowering its priority.  

Another example is where you are about to start a very important calculation job. To ensure that it is handled as fast as possible, you might want to give it an increased priority, taking away CPU time from other processes.  

On earlier Linux versions, it could be dangerous to increase the priority of one job too much, because other processes (including vital kernel processes) might risk being blocked out completely. On current Linux kernels, the situation is not that urgent anymore:  

■ Modern Linux kernels differentiate between essential kernel threads that are started as real-time processes and normal user processes. Increasing the priority of a user process will never be able to block out kernel threads or other processes that were started as real-time processes. 

■ Modern computers often have multiple CPU cores. A single threaded process that is running with the highest priority will never be able to get beyond the boundaries of the CPU it is running on.

When using nice or renice to adjust process priority, you can select from values ranging from -20 to 19. The default niceness of a process is set to 0 (which results in the priority value of 20). By applying a negative niceness, you increase the priority. Use a positive niceness to decrease the priority. It is a good idea not to use the ultimate values immediately. Instead, use increments of 5 and see how it affects the application.

TIP Do not set process priority to -20; it risks blocking other processes from getting served.

EXAMPLES:

Let’s take a look at examples of how to use nice and renice . The command nice -n 5 dd if=/dev/zero of=/dev/null & starts an infinite I/O-intensive job, but with an adjusted niceness so that some place remains for other processes as well. To adjust the niceness of a currently running process, you need the PID of that process. The following two commands show how ps aux is used to find the PID of the dd job from the previous example. Next, you see how the renice command is used to change the niceness of that command:

[root@rhel7 tmp]# nice -n 5 dd if=/dev/zero of=/dev/null &
[1] 2872
[root@rhel7 tmp]# jobs
[1]+  Running                 nice -n 5 dd if=/dev/zero of=/dev/null &

1. Use ps aux | grep dd to find the PID of the dd command that you just started. The PID is in the second column of the command output.

[root@rhel7 tmp]# ps aux | grep dd
root         2  0.0  0.0      0     0 ?        S    Jun27   0:00 [kthreadd]
root        43  0.0  0.0      0     0 ?        S<   Jun27   0:00 [ipv6_addrconf]
dbus       618  0.0  0.0  34948  1856 ?        Ssl  Jun27   0:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root      2872 99.9  0.0 107936   620 pts/1    RN   04:23   2:37 dd if=/dev/zero of=/dev/null
root      2883  0.0  0.0 112644   956 pts/1    R+   04:26   0:00 grep --color=auto dd

2. Use renice -n 10 -p 1234 (assuming that 1234 is the PID you just found). 
Note that regular users can only decrease the priority of a running process. You must be root to give processes increased priority.

[root@rhel7 tmp]# renice -n 10 -p 2872
2872 (process ID) old priority 5, new priority 10

 

posted on 2016-06-28 16:48  遠離塵世の方舟  阅读(1277)  评论(0编辑  收藏  举报

导航