首页 文章

为什么niceness值与进程优先级成反比?

提问于
浏览
7

进程的niceness随着进程优先级的增加而降低 .

摘自Linux初始化编程第4版,第169页:

默认优先级为0.正优先级用于在没有其他更高优先级任务准备好运行时运行的后台任务 . 负优先级会导致程序更频繁地运行,占用可用CPU时间的更大份额 . 有效优先级的范围是-20到20.这通常令人困惑,因为数值越高,执行优先级越低 .

对于较高的进程优先级而言,负值是否有任何特殊原因(而不是为更高优先级的进程提高优先级)?

4 回答

  • 2

    歇斯底里的原因 - 我的意思是历史...我很确定它开始时的数字从0 ... 20上升,而最低的数字是先获得的 . 然后有人得出结论:“嗯,如果我们需要做一些更重要的事情呢?” - 我们必须消极 .

    你希望优先级是一个可排序的值,所以如果你从“默认为零”开始,你必须将更高的优先级设为更高的数字(但是每日说话的“优先级1”高于“优先级2” - 当你的老板说“把它作为你的第一要务”,它确实意味着它很重要,对吗?) . 作为计算机,显然优先级0高于优先级1,优先级-1高于优先级0 .

    最后,这是一个随意的选择 . 也许Ken Thomson,Dennis Ritchie或其中一个人能够肯定地说他们为什么选择那个序列,而不是0..255 .

  • 2

    首先,答案有点长,但这只是为了澄清 .

    与在linux内核中一样,每个传统进程可能具有被称为静态优先级的优先级从100(最高)到139(最低) . 因此,基本上可以为该过程分配40个优先级 .

    因此,当创建任何进程时,它将获得其父级的优先级,但如果用户想要更改它的优先级,则可以在nice(nice_value)系统调用的帮助下完成 .

    你的问题的原因是 every process wants base time quantum which is used as how much time the process will get the CPU for its execution in milliseconds and this is calculated as

    time={
         if static_priority<120
    
           (140-static_priority)*20 
    
         if static_priority>=120
    
           (140-static_priority)*5
    

    so The sys_nice( )service routine handles the nice( )system call. Although the nice_value may have any value, absolute values larger than 40 are trimmed down to 40. Traditionally, negative values correspond to requests for priority increments and require superuser privileges, while positive ones correspond to requests for priority decreases. In the case of a negative nice_value, the function invokes the capable( ) function to verify whether the process has a CAP_SYS_NICE capability. Moreover, the function invokes the security_task_setnice( )security hook. 所以最后,nice_value用于计算静态优先级,然后这个静态优先级用于计算基准时间量 .

    so it's clear that -ve values are used for increment the priority so needs super user access & +ve values are used for decrease the priority so no need of super user access.

  • 2

    @ Ewald的回答是正确的,正如Jerry Peek等人所证实的那样 . 在Unix Power Tools中(O'Reilly,2007,p.507):

    这就是为什么这个好的数字通常被称为漂亮:一个高性能的工作对你的系统用户非常友好(即它以低优先级运行),而一个没有好处的工作会占用CPU . “优秀”这个词很尴尬,就像优先系统本身一样 . 不幸的是,这是唯一一个既准确又好的术语(好的数字用于计算优先级但不是优先级本身)并避免可怕的迂回(“增加优先级意味着降低优先级......”) .

    Nice至少V6 Unix具有这个含义,但V6手册从未明确解释过这一点 . 允许值的范围是-220到20,为超级用户保留负数 . 在V7中,范围变为-20到20 .

  • 1

    是的 - 当数字上升时,它会变为NICER,而当数字下降时,它会变为MEANER . 因此,当它没有占用所有资源时,这个过程被视为“更友好”,因为它变得更加贪婪 .

    把它想象成“好”点 - 你对别人越好,得分就越多 .

相关问题