nice(NI), priority(PR), realtime priority(RT)的算术关系

首先,你要明白有两种 process 类型。一个是 normal process,对应的 scheduling policy 是 SCHED_OTHER 等。另外一个是 runtime process,支持的 scheduling policy 有 SCHED_FIFO 和 SCHED_RR 等。一、Kernel 早期的 scheduler以早期的 kernel scheduler 为例,我们先说一下 normal process,scheduler 大致根据 nice value (NI) 调度 process。nice 的范围就是你说的 。除了 nice value 之外,process 的 goodness 还由剩余的 time slice 以及 MM 有关系。而 realtime process, 则直接用 realtime priority 作为 goodness 给 scheduler 使用 FIFO 或者 RR 进行调度。所以 normal process 和 realtime process 的计算方法是不一样的,可以看一下 2.4 时代的代码,当时还没有统一的 mapping 到一起。Linux/kernel/sched.c130 /*131 * This is the function that decides how desirable a process is..132 * You can weigh different processes against each other depending133 * on what CPU they\u0026#39;ve run on lately etc to try to handle cache134 * and TLB miss penalties.135 *136 * Return values:137 * -1000: never select this138 * 0: out of time, recalculate counters (but it might still be139 * selected)140 * +ve: "goodness" value (the larger, the better)141 * +1000: realtime process, select this.142 */143 144 static inline int goodness(struct task_struct * p, int this_cpu, struct mm_struct *this_mm)145 {146 int weight;147 148 /*149 * select the current process after every other150 * runnable process, but before the idle thread.151 * Also, dont trigger a counter recalculation.152 */153 weight = -1;154 if (p-\u0026gt;policy \u0026amp; SCHED_YIELD)155 goto out;156 157 /*158 * Non-RT process - normal case first.159 */160 if (p-\u0026gt;policy == SCHED_OTHER) {161 /*162 * Give the process a first-approximation goodness value163 * according to the number of clock-ticks it has left.164 *165 * Don\u0026#39;t do any other calculations if the time slice is166 * over..167 */168 weight = p-\u0026gt;counter;169 if (!weight)170 goto out;171 172 #ifdef CONFIG_SMP173 /* Give a largish advantage to the same processor... */174 /* (this is equivalent to penalizing other processors) */175 if (p-\u0026gt;processor == this_cpu)176 weight += PROC_CHANGE_PENALTY;177 #endif178 179 /* .. and a slight advantage to the current MM */180 if (p-\u0026gt;mm == this_mm || !p-\u0026gt;mm)181 weight += 1;182 weight += 20 - p-\u0026gt;nice;183 goto out;184 }185 186 /*187 * Realtime process, select the first one on the188 * runqueue (taking priorities within processes189 * into account).190 */191 weight = 1000 + p-\u0026gt;rt_priority;192 out:193 return weight;194 }


推荐阅读