【问题标题】:Understading the Linux Kernel Scheduler了解 Linux 内核调度程序
【发布时间】:2013-10-20 01:39:54
【问题描述】:

我正在研究 Linux 内核,并试图弄清楚循环调度算法是如何工作的。在kernel\sched_rt.c 文件中,有一个名为task_tick_rt 的方法定义如下:

static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
{
    update_curr_rt(rq);

    watchdog(rq, p);

    /*
     * RR tasks need a special form of timeslice management.
     * FIFO tasks have no timeslices.
     */
    if (p->policy != SCHED_RR)
            return;

    if (--p->rt.time_slice)
            return;

    p->rt.time_slice = DEF_TIMESLICE;

    /*
     * Requeue to the end of queue if we are not the only element
     * on the queue:
     */
    if (p->rt.run_list.prev != p->rt.run_list.next) {
            requeue_task_rt(rq, p, 0);
            set_tsk_need_resched(p);
    }

}

我不明白的是(除了有一个无用的queued 参数)是代码试图通过if (--p->rt.time_slice) 检查实现的目标。我不明白为什么任务列表指针p 减1,换句话说,为什么方法检查上一个任务 而不是当前的?对此的任何澄清表示赞赏。

【问题讨论】:

    标签: c linux linux-kernel scheduler


    【解决方案1】:

    查看c运算符优先级http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

    -> 运算符的优先级高于前缀 ++,因此可以编写此特定条件:

    if (--(p->rt.time_slice))
    

    换句话说,递减的是时间片,而不是指针。


    queued 参数在这里可能看起来没用,但它有理由出现。特别注意从哪里调用task_tick_rt()。它的唯一引用是当它被分配给struct sched_classrt_sched_class 实例中的.task_tick 函数指针时: http://lxr.free-electrons.com/source/kernel/sched/rt.c#L1991

    所以我们看到每个调度算法都有自己的struct sched_class 函数向量,内核将调用该函数向量来进行调度服务。如果我们查看其他算法,我们会看到 CFS(完全公平调度)算法也有自己的实例 struct sched_class,命名为 fair_sched_classhttp://lxr.free-electrons.com/source/kernel/sched/fair.c#L6179

    CFS 案例中的.task_tick 成员指向task_tick_fair()http://lxr.free-electrons.com/source/kernel/sched/fair.c#L5785

    注意task_tick_fair() 确实使用了queued 参数。因此,当.task_tick 成员被调用(herehere)时,会为queued 参数传入 0 或 1。所以虽然task_tick_rt() 不使用它,但queued 参数必须仍然是它们的,所以struct sched_class 函数向量中的函数指针类型都匹配。

    简而言之,struct sched_class 函数向量指定了调度算法与内核其余部分之间的接口。 queued 参数应该有一个给定的算法选择使用它,但在循环的情况下,它被简单地忽略了。

    【讨论】:

    • 完美的解释。谢谢好心的先生!
    • @DigitalTrauma 感谢您解释 sched_class 的目的。我知道有两个调度策略SCHED_FIFOSCHED_RR。但是,查看lxr.missinglinkelectronics.com/#linux+v3.10/kernel/sched/… sched/rt.c,我无法弄清楚 sched_class 如何帮助选择要使用的策略。
    • @newprint - 请将此作为一个新问题提出。
    猜你喜欢
    • 2011-09-11
    • 2014-02-05
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2011-11-02
    • 1970-01-01
    相关资源
    最近更新 更多