Linux程序優先順序調整
NI 是優先值,是使用者層面的概念, PR是程序的實際優先順序, 是給核心(kernel)看(用)的。
一般情況下,PR=NI+20, 如果一個程序的優先順序PR是20, 那麼它的NI(nice)值就是20-20=0。
可以通過改變NI來改變PR: PRI(new) = PRI(old) + nice
NI is the nice value, which is a user-space concept. PR is the process's actual priority, as viewed by the Linux kernel.
For normal processes, the kernel priority is simply +20 from the nice value. Thus a process with the neutral nice value of zero has a kernel priority of 20. This offset-by-20 is done so that a process with a nice value of -20, the highest priority nice value, receives a kernel priority of zero. Lower numeric values equal higher scheduling priority.
For realtime processes, the kernel priority is the process's real-time priority, but thePR column will simply print RT.
In some versions of the Linux kernel, the kernel priority could differ from the nice value, as the process scheduler applied a small bonus or punishment to interactive or processor-hogging tasks, respectively. In these older kernels, the value given by the PR column could differ from the nice value. This isn't true with the kernel's current scheduler, the Completely Fair Scheduler (CFS).
This article come from:
http://www.quora.com/Linux/What-is-the-difference-between-the-NI-and-PR-values-in-the-top-1-commands-output
2. Nice調整限制:
nice 值可調整的範圍為 -20 ~ 19 ;
Useful priorities are: 20 (the affected processes will run only when nothing else
in the system wants to), 0 (the ''base'' scheduling priority), anything negative (to make things go very fast).
root 可隨意調整自己或他人程式的 Nice 值,且範圍為 -20 ~ 19 ;
一般使用者僅可調整自己程式的 Nice 值,且範圍僅為 0 ~ 19 (避免一般使用者搶佔系統資源);
一般使用者僅可將 nice 值越調越高,例如本來 nice 為 5 ,則未來僅能調整到大於 5;
3. Nice調整方法
3.1新執行的指令即給予新的 nice 值
[
選項與引數:
-n :後面接一個數值,數值的範圍 -20 ~ 19。
www.2cto.com
用ps -l查的話,裡面顯示的PRI就是PRI(new),核心會自動調整,不是PRI(old) + nice的值,會調高一點點
3.2 已存在程式的 nice 重新調整
renice [number] PID
4. 設定某使用者的所有程序優先順序
修改/etc/security/limits.conf, 增加一行
userxxx - nice 19
要使 limits.conf 檔案配置生效,必須要確保 pam_limits.so 檔案被加入到啟動檔案中。檢視 /etc/pam.d/login 檔案中有:
session required /lib/security/pam_limits.so
5. 其他:
5.1用taskset限制程序的CPU使用率:
http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html
http://www.jb51.net/LINUXjishu/97275.html
5.2採用cpulimit限制程序的CPU使用率(發生SIGSTOP和SIGCONT訊號):
http://cpulimit.sourceforge.net/
http://www.ttlsa.com/tools/cpulimit-limit-cpu-usage-on-linux/
http://maxmanii.blog.163.com/blog/static/1200622672009517112429702/