Understading Linux的内核调度 [英] Understading the Linux Kernel Scheduler

查看:377
本文介绍了Understading Linux的内核调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习Linux内核,我试图找出如何轮循调度算法的作品。在内核\\ sched_rt.c 文件中,有一个名为方法 task_tick_rt 是这样定义的:

I'm studying the Linux Kernel and am trying to figure out how the Round Robin scheduling algorithm works. In the kernel\sched_rt.c file, there's a method called task_tick_rt defined like this:

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);
    }

}

我不明白(除了一个事实,即有一个无用的排队参数)什么是什么code试图通过<$ C $实现C>如果(--p-&GT; rt.time_slice)检查。我不明白为什么在任务列表指针 P 正在减1,换句话说,为什么是方法检查 previous任务?任何对此的说明是AP preciated。

What I don't understand (besides the fact that there's a useless queued parameter) is what the code is trying to achieve by the if (--p->rt.time_slice) check. I don't understand why the task list pointer p is being decremented by 1, in other words, why is the method checking the previous task instead of the current one? Any clarification on this is appreciated.

推荐答案

查看C运算符precedence <一个href=\"http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_$p$pcedence\">http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_$p$pcedence

Check out c operator precedence http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

- &GT; 经营者具有更高的precedence超过preFIX ++ ,所以这个特定的条件可以写:

The -> operator has a higher precedence than the prefix ++, so this particular condition could be written:

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

在换句话说,它是正在被递减时间片,而不是指针。

In other words, it is the timeslice which is being decremented, not the pointer.

排队参数可能会出现在这里毫无用处,但它有一个理由在那里。特别注意其中 task_tick_rt()从调用。其唯一的参考是当它被分配给 rt_sched_class 实例 .task_tick 函数指针>结构sched_class :
<一href=\"http://lxr.free-electrons.com/source/kernel/sched/rt.c#L1991\">http://lxr.free-electrons.com/source/kernel/sched/rt.c#L1991

The queued parameter may appear useless here, but it has a reason to be there. Specifically notice where task_tick_rt() is called from. Its only reference is when it is assigned to the .task_tick function pointer in the rt_sched_class instance of struct sched_class: http://lxr.free-electrons.com/source/kernel/sched/rt.c#L1991

所以,我们看到,每个调度算法都有自己的结构sched_class 功能载体,其内核将调用调度服务。如果我们看一下其他的算法,我们看到了CFS(完全公平调度)算法也有自己的结构sched_class 的实例,命名为 fair_sched_class
<一href=\"http://lxr.free-electrons.com/source/kernel/sched/fair.c#L6179\">http://lxr.free-electrons.com/source/kernel/sched/fair.c#L6179

So we see that each scheduling algorithm has its own struct sched_class function vector which the kernel will call into for scheduling services. If we look at other algorithms, we see the CFS (Completely Fair Scheduling) algorithm also has its own instance of struct sched_class, named fair_sched_class: http://lxr.free-electrons.com/source/kernel/sched/fair.c#L6179

.task_tick 成员在CFS的情况下分 task_tick_fair()
<一href=\"http://lxr.free-electrons.com/source/kernel/sched/fair.c#L5785\">http://lxr.free-electrons.com/source/kernel/sched/fair.c#L5785

The .task_tick member in the CFS case points to task_tick_fair(): http://lxr.free-electrons.com/source/kernel/sched/fair.c#L5785

请注意 task_tick_fair() 确实使用的排队参数。因此,当 .task_tick 成员称为成(的这里 href=\"http://lxr.free-electrons.com/source/kernel/sched/core.c#L2179\">),0或1传入的排队参数。因此,尽管 task_tick_rt()不使用它,在排队参数必须仍然是他们所以在函数指针类型在结构sched_class 函数向量的所有比赛了。

Note task_tick_fair() does make use of the queued parameter. So when the .task_tick member is called into (here and here), a 0 or a 1 is passed in for the queued parameter. So while task_tick_rt() doesn't use it, the queued parameter must still be their so the function pointer types in the struct sched_class function vector all match up.

总之,结构sched_class 函数向量指定调度算法和内核其余部分之间的接口。在排队参数是应该有一个给定的算法选择使用它,但在循环赛的情况下,它会被忽略。

In short, the struct sched_class function vector specifies the interface between scheduling algorithms and the rest of the kernel. The queued parameter is there should a given algorithm choose to use it, but in the case of Round Robin, it is simply ignored.

这篇关于Understading Linux的内核调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆