进程的优先级和友善度

进程的优先级和友善度

根据我读过的一些资料,nice 值较低的进程具有更高的优先级。由于优先级较高,因此 CPU 会为该进程分配更多时间。

但是,这听起来有点奇怪,因为进程的优先级值是通过将 nice 值与 20 相加来计算的。

PR = NI + 20。使用以下命令可以轻松看到top

按照这个公式,nice值越高的进程优先级越高,但是有些资源说nice值低的进程优先级更高。

哪一个是真的?

答案1

按照这个公式,nice值越高的进程优先级越高,但是有些资源说nice值低的进程优先级更高。

关闭,但没有 cookie。优先级的数值高并不意味着优先级高。 top从 读取优先级/proc/<pid>/stat。请参阅man 5 proc对于该文件的解释:

(18) priority  %ld
    (Explanation  for Linux 2.6) For processes running a real-time scheduling
    policy (policy below; see sched_setscheduler(2)),  this  is  the  negated
    scheduling  priority,  minus  one;  that  is, a number in the range -2 to
    -100, corresponding to real-time  priorities  1  to  99.   For  processes
    running  under  a  non-real-time  scheduling policy, this is the raw nice
    value (setpriority(2)) as represented in the kernel.  The  kernel  stores
    nice  values  as numbers in the range 0 (high) to 39 (low), corresponding
    to the user-visible nice range of -20 to 19.

    Before Linux 2.6,  this  was  a  scaled  value  based  on  the  scheduler
    weighting given to this process.

因此:PR 从 0(高)变为 39(低)。

相关内容