Linux内核函数set_user_nice

标签 linux linux-kernel kernel scheduling scheduler

我有一项家庭作业,我们需要向 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/

相关文章:

带有 MAP_POPULATE 的 Linux mmap(),手册页似乎提供了错误的信息

linux - 如何将时间戳从十六进制格式转换为 EPOCH 时间戳?

c++ - 使用 linux 功能读取 block 的不连续运行

linux - 如何从存储库生成 .deb 包

linux - 在内核 block I/O 层拦截数据

C - 从内核模块写入物理内存

kernel - Android 文件系统钩子(Hook)

c - 错误协议(protocol) 41 >= NPROTO(41) 向 linux 内核添加新协议(protocol)

python - 如何使用 KFileDialog 选择多个目录?

linux - sed 命令不接受 ksh 中带有 '.' 的子字符串