Linux 核函数 set_user_nice
Linux kernel function set_user_nice
我有一项家庭作业,我们需要向 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)
我错过了什么吗?
谢谢
内核版本是2.4.18吗?我正在查看该来源,但在 sched.c 中没有看到 set_user_nice。
无论如何,我认为他们正在处理那里的竞争条件。有可能在他们设置新进程优先级之间,进程本身已经改变了它。所以他们正在检查是否是这种情况,如果是,则重新安排任务。
好的,所以我在较新的源代码中寻找这个函数,并且这个函数在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 内核添加一些功能,我们正在研究 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)
我错过了什么吗? 谢谢
内核版本是2.4.18吗?我正在查看该来源,但在 sched.c 中没有看到 set_user_nice。
无论如何,我认为他们正在处理那里的竞争条件。有可能在他们设置新进程优先级之间,进程本身已经改变了它。所以他们正在检查是否是这种情况,如果是,则重新安排任务。
好的,所以我在较新的源代码中寻找这个函数,并且这个函数在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:
所以现在看起来确实正确计算了新旧优先级之间的差异。