进程的优先级

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进程的优先级相关的知识,希望对你有一定的参考价值。

服务器的进程就像人需要做的事一样具有先后顺序,而进程的顺序称为优先级。
管理员可以用renice来修改进程的优先级
renice -nice值 pid //调整某个进程的nice(取值为-20~19)
nice -nice值 命令 //设置命令运行时的优先级,如nice -8 ping 127.9回车后查进程状态

1.在后台运行多个ping命令
ping 127.0.0.1 回车后按ctrl+z #在后台暂停运行ping命令
ping 127.0.0.2 同上
ping 127.0.0.8 同上

2.ps el -C ping 进程信息如下
4 S root 2786 2747 0 80 0 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.1
4 S root 2787 2747 0 80 0 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.2
4 S root 2789 2747 0 80 0 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.8
0 R root 2796 2747 0 80 0 - 28166 - 11:35 pts/0 00:00:00 grep --color=auto ping

在另一窗口执行:top -p 2786,2787,2789
注:以上的2786、2787、2789是用ps elf -C ping查到的进程pid号

3.renice -5 2786 将pid为2786的nice值调为-5,PRI=20+NI
2786 (进程 ID) 旧优先级为 0,新优先级为 -5

4.ps elf -C ping
4 S root 2786 2747 0 70 -5 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.1
4 S root 2787 2747 0 80 0 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.2
4 S root 2789 2747 0 80 0 - 31978 skb_re 11:35 pts/0 00:00:00 ping 127.0.0.8
0 R root 2820 2747 0 80 0 - 28166 - 11:37 pts/0 00:00:00 grep --color=auto ping

在程序开启的时候指定其nice值
用法:nice -nice值 命令
nice --15 vim a.txt & 将nice指定为-15
ps elf -C vim 在另一窗口执行

以上是关于进程的优先级的主要内容,如果未能解决你的问题,请参考以下文章

Linux 内核调度器 ⑧ ( 进程优先级源码 includelinuxschedprio.h | 进程分类 | 实时进程 | 普通进程 | 进程优先级数值 | 0 ~ 99 实时进程 )

浅析Linux下进程的调度策略与优先级

进程的优先级

linux系统进程优先级

Linux的进程优先级NI和PR有啥区别

Linux操作系统进程优先级和进程切换