use*_*534 4 c linux scheduler linux-kernel
我正在研究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);
}
Run Code Online (Sandbox Code Playgroud)
}
我不明白的事情(除了有一个无用的queued参数这个事实)是代码试图通过if (--p->rt.time_slice)检查实现的.我不明白为什么任务列表指针p递减1,换句话说,为什么方法检查前一个任务而不是当前任务?对此有任何澄清表示赞赏.
检查c运算符优先级 http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
该->经营者具有比前缀的优先级高++,所以这一特殊情况可以写成:
if (--(p->rt.time_slice))
Run Code Online (Sandbox Code Playgroud)
换句话说,它是正在递减的时间片,而不是指针.
这里的queued参数似乎没用,但它有理由在那里.特别注意task_tick_rt()从哪里调用.它唯一的参考是当它被分配给实例中的.task_tick函数指针rt_sched_class时struct sched_class:http:
//lxr.free-electrons.com/source/kernel/sched/rt.c#L1991
因此我们看到每个调度算法都有自己的struct sched_class函数向量,内核将调用它来调度服务.如果我们看一下其他算法,我们看到CFS(完全公平调度)算法也有自己的实例struct sched_class,命名为fair_sched_class:
http //lxr.free-electrons.com/source/kernel/sched/fair.c#L6179
.task_tickCFS案件的成员指出task_tick_fair():http:
//lxr.free-electrons.com/source/kernel/sched/fair.c#L5785
注意task_tick_fair() 确实使用queued参数.因此,当.task_tick调用成员(此处和此处)时,将为queued参数传入0或1 .因此,虽然task_tick_rt()不使用它,但queued参数必须仍为它们,因此struct sched_class函数向量中的函数指针类型都匹配.
简而言之,struct sched_class函数向量指定调度算法与内核其余部分之间的接口.该queued参数是应该有一个给定的算法选择使用它,但在循环赛的情况下,它会被忽略.
| 归档时间: |
|
| 查看次数: |
1785 次 |
| 最近记录: |