当队列中没有任务时,linux调度程序返回什么

时间:2015-03-21 07:47:06

标签: linux-kernel scheduler

linux调度程序调用调度程序算法,该算法在任务列表中查找下一个任务。

如果没有剩下任务,调度算法会返回什么?

下面是一段代码

struct rt_info* sched_something(struct list_head *head, int flags) { /* some logic */ return some_task; // What task's value if there are no tasks left. }

1 个答案:

答案 0 :(得分:2)

PID = 0时有特殊的“空闲”任务,有时称为swapperWhy do we need a swapper task in linux?)。当没有其他任务准备好运行时,此任务计划到CPU核心。有几个空闲任务 - 每个CPU核心一个

来源kernel/sched/core.c http://lxr.free-electrons.com/source/kernel/sched/core.c?v=3.16#L3160

3160 /**
3161  * idle_task - return the idle task for a given cpu.
3162  * @cpu: the processor in question.
3163  *
3164  * Return: The idle task for the cpu @cpu.
3165  */
3166 struct task_struct *idle_task(int cpu)
3167 {
3168         return cpu_rq(cpu)->idle;
3169 }
3170 

因此,指向此任务的指针存储在runqueue(struct rqkernel/sched/sched.h中:

502  * This is the main, per-CPU runqueue data structure. ...  */
508 struct rq {
557         struct task_struct *curr, *idle, *stop;

sched / core.c中有一些初始化代码:

4517 /**
4518  * init_idle - set up an idle thread for a given CPU
4519  * @idle: task in question
4520  * @cpu: cpu the idle task belongs to
4524  */
4525 void init_idle(struct task_struct *idle, int cpu)

我认为,idle task会运行某种带有特殊asm命令的循环来通知CPU核心没有用的工作......

此帖子http://duartes.org/gustavo/blog/post/what-does-an-idle-cpu-do/表示空闲任务执行cpu_idle_loop kernel/sched/idle.c(可能存在针对arch和CPU的自定义循环版本 - 使用cpu_idle_poll(void)调用 - > {{3 }}):

 40 #define cpu_relax()     asm volatile("rep; nop")
 45 static inline int cpu_idle_poll(void)
 ..
 50         while (!tif_need_resched())
 51                 cpu_relax();

221                         if (cpu_idle_force_poll || tick_check_broadcast_expired())
222                                 cpu_idle_poll();
223                         else
224                                 cpuidle_idle_call();