我有一项家庭作业,我们需要向 Linux 内核添加一些功能,我们正在研究 red hat 2.4.18。 我查看了 sched.c,函数 set_user_nice:
void set_user_nice(task_t *p, long nice)
{
unsigned long flags;
prio_array_t *array;
runqueue_t *rq;
if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
return;
/*
* We have to be careful, if called from sys_setpriority(),
* the task might be in the middle of scheduling on another CPU.
*/
rq = task_rq_lock(p, &flags);
if (rt_task(p)) {
p->static_prio = NICE_TO_PRIO(nice);
goto out_unlock;
}
array = p->array;
if (array)
dequeue_task(p, array);
p->static_prio = NICE_TO_PRIO(nice);
p->prio = NICE_TO_PRIO(nice);
if (array) {
enqueue_task(p, array);
/*
* If the task is running and lowered its priority,
* or increased its priority then reschedule its CPU:
*/
if ((NICE_TO_PRIO(nice) < p->static_prio) || (p == rq->curr))
resched_task(rq->curr);
}
out_unlock:
task_rq_unlock(rq, &flags);
}
我不明白最后一个 if 语句中的代码到底检查了什么, 因为上面几行,我们有这一行:
p->static_prio = NICE_TO_PRIO(nice);
然后,在 if 语句中我们检查:
(NICE_TO_PRIO(nice) < p->static_prio)
我错过了什么吗? 谢谢
最佳答案
好的,所以我在较新的源代码中寻找这个函数,这个函数在kernel/sched/core.c中实现。 我正在谈论的部分:
old_prio = p->prio;
3585 p->prio = effective_prio(p);
3586 delta = p->prio - old_prio;
3587
3588 if (queued) {
3589 enqueue_task(rq, p, ENQUEUE_RESTORE);
3590 /*
3591 * If the task increased its priority or is running and
3592 * lowered its priority, then reschedule its CPU:
3593 */
3594 if (delta < 0 || (delta > 0 && task_running(rq, p)))
3595 resched_curr(rq);
3596 }
3597 out_unlock:
所以现在看来确实正确计算了新旧优先级之间的差异。
关于Linux内核函数set_user_nice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597216/