• Linux下nice/renice命令小结


    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(提升的优先级)]

  • 相关阅读:
    用感知机(Perceptron)实现逻辑AND功能的Python3代码
    xpadder教程:自定义设置游戏手柄的图片
    用Python实现小说中的汉字频率统计
    天猫精灵X1智能音箱使用感想
    一道常被人轻视的前端JS面试题
    jQueryNotes仿QQ空间添加标记
    JQ对象到底是什么
    正则匹配规则
    自定义右键菜单
    IIS处理并发请求时出现的问题及解决
  • 原文地址:https://www.cnblogs.com/oskb/p/3865993.html
Copyright © 2020-2023  润新知