1. nice命令

内核根据进程的nice值决定进程需要多少处理器时间. nice值的取值范围是是: -20到20. 一个具有-20 的 nice 值的进程有很高的优先级. 一个 nice 值为 20 的进程的优先级则很低.

1) 用 ps axl 显示所有正在运行的进程的 nice 值

# ps axl

F   UID   PID PPID PRI NI     VSZ RSS WCHAN STAT TTY TIME COMMAND

4     0     1     0 16    0 2172 552 -        S    ? 0:17 init [5]

1     0     3     1 34 19       0    0 ksofti SN   ? 3:18 [ksoftirqd/0]

1     0    10     1   5 -10     0    0 worker S<   ? 0:01 [events/0]

4     0 5145      1 25 10 32124 18592 -       SNs ? 0:08 /usr/bin/python /usr/bin/rhn-applet-gui --sm-client-id default4

4     0 5147 5142 16      0 3528 604 -        S    ? 0:00 /sbin/pam_timestamp_check -d root

1   503 17552 4180 16     0 14208 3920 -      S    ? 0:01 /home/www/apache2/bin/httpd -f /home/www/apache2/conf/httpd.conf -k start

2) 如何给一个shell脚本分配一个低的优先级(更高的nice值)?

在下面的例子里, 当我在后台启动 nice-test.sh 脚本, nice 值为 0.

$ ./nice-test.sh &

[3] 13009

$ ps axl | grep nice-test

0   509 13009 12863 17    0 4652 972 wait S pts/1 0:00 /bin/bash ./nice-test.sh

[注: 第六列数值为 0 的是 nice 值]

现在, 以不同的 nice 值来执行相同的脚本, 如下所示:

$ nice -10 ./nice-test.sh &

[1] 13016

$ ps axl | grep nice-test

0   509 13016 12863 30 10 4236 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh

[注: 第六列数值为 10 的是该 shell 脚本的 nice 值]

3) 如何给一个shell脚本分配一个高的优先级(更低的nice值)?

下面的例子里, 分配给 shell 脚本 nice-test.sh 一个"-10"的 nice 值.

$ nice --10 ./nice-test.sh &

[1] 13021

$ nice: cannot set priority: Permission denied

注意: 只有 root 用户可以设置一个负的 nice 值. 用 root 用户登录再次尝试. 注意在下面的nice 命令里的 10 前面有一个双破折号.

# nice --10 ./nice-test.sh &

[1] 13060

# ps axl | grep nice-test

4     0 13060 13024 10 -10 5388 964 wait S< pts/1      0:00 /bin/bash ./nice-test.sh

[注: 第六列数值为-10 的是该 shell 脚本的 nice 值]

2. renice命令

renice 可以给正在运行的进程设置调度优先级.

1) 怎样降低一个运行进程的优先级(增加 nice值)?

下面的例子里, 一个存在的 shell 脚本运行时的 nice 值为 10. (ps 输出的第 6 列)

$ ps axl | grep nice-test

0   509 13245 13216 30 10 5244 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh

为增加 nice 值(因此降低优先级), 按如下方法执行 renice 命令.

$ renice 16 -p 13245

13245: old priority 10, new priority 16

$ ps axl | grep nice-test

0   509 13245 13216 36 16 5244 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh

[注: 现在, nice-test.sh (PID 13245)的第 6 列显示新的 nice 值是 16]

2) 怎样增加运行进程的优先级(减少 nice值)?

下面的例子里, 一个已有的 shell 脚本运行时 nice 值为 10. (ps 输出第 6 列)

$ ps axl | grep nice-test

0   509 13254 13216 30 10 4412 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh

为提升其优先级, 赋予其一个较低的nice值. 然而, 只有root可以提升运行进程的优先级, 否则你会得到以下错误信息.

$ renice 5 -p 13254

renice: 13254: setpriority: Permission denied

Login as root to increase the priority of a running

process

$ su -

# renice 5 -p 13254

13254: old priority 10, new priority 5

# ps axl | grep nice-test

0   509 13254 13216 25    5 4412 968 wait     SN pts/1      0:00 /bin/bash ./nice-test.sh

注: 第 6 列现在显示了一个较低的 nice 值 5(提升的优先级)]

最新文章

  1. FlashBuilder项目环境配置
  2. C# 通过Selecnuim WebDriver操作非IE浏览器
  3. &lt;mvc:default-servlet-handler/&gt;导致SimpleUrlHandlerMapping失效
  4. Maven创建webapp(二)
  5. 详解MySQL---DDL语句、DML语句与DCL语句
  6. Spring中Bean的配置:基于注解的方式
  7. 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上
  8. POJ 1195 2维线段树(树套树实现) 树状数组
  9. Codeforces Round #204 (Div. 2) C
  10. Mysql笔记【2】-数据表的基本操作
  11. ionic2新手入门整理,搭建环境,创建demo,打包apk,热更新,优化启动慢等避坑详解
  12. python selenium-webdriver 登录验证码的处理(十二)
  13. 关于C++用法的学习心得
  14. UI自动化(六)js
  15. sql执行顺序与性能优化小技巧(一)
  16. Vim 编辑文件时,突然断开链接
  17. Windows下安装Apache 2.2.21图文教程
  18. http://www.cnblogs.com/CBDoctor/p/4459750.html
  19. SQL精华应用
  20. 集大通APP案例分析

热门文章

  1. How to use liftover
  2. Linux下Find命令的使用
  3. CentOS终端界面登入Linux
  4. 十日谈 (share)
  5. Java 性能优化实战记录(2)---句柄泄漏和监控
  6. DPM(Deformable Parts Model)--原理(一)(转载)
  7. SQL编写
  8. 用jquery ,当改变窗口或屏幕大小时调用function,用哪个事件?
  9. linux查找webshell
  10. 工作中遇到的问题--Hibernate一对多保存简化Service层代码