当队列中没有剩余任务时,linux 调度程序 return 会做什么

What does the linux scheduler return when there are no tasks left in the queue

linux 调度程序调用一个调度程序算法来查找任务列表中的下一个任务。

如果没有剩余任务,调度算法return会怎样?

下面是一段代码

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. }

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

一个

来源kernel/sched/core.chttp://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 

因此,指向此任务的指针存储在 运行队列中 (struct rq) kernel/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)

我认为,空闲任务将 运行 某种带有特殊 asm 命令的循环通知 CPU 核心没有有用的工作...

这个posthttp://duartes.org/gustavo/blog/post/what-does-an-idle-cpu-do/ says idle task executes cpu_idle_loop kernel/sched/idle.c (there may be custom version of loop for arch and CPU - called with cpu_idle_poll(void) -> cpu_relax();):

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