【问题标题】:What does the linux scheduler return when there are no tasks left in the queue当队列中没有任务时,linux调度程序返回什么
【发布时间】:2015-05-24 16:24:17
【问题描述】:

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

【问题讨论】:

    标签: linux-kernel scheduler


    【解决方案1】:

    有一个特殊的“空闲”任务,PID=0,有时称为swapper (Why 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 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) 调用-> 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();
    

    【讨论】:

    • 早期空闲:tldp.org/HOWTO/Linux-i386-Boot-Code-HOWTO/init_main.html "rest_init() { /* init process, pid = 1 */ kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); unlock_kernel(); current->need_resched = 1; /* idle process, pid = 0 */ cpu_idle(); /* never return */ }" "/* used in the idle loop; sti takes one instruction cycle to complete */ #define safe_halt() __asm__ __volatile__("sti; hlt": : :"memory") CPU will resume code execution with the instruction following "hlt" on the return from an interrupt handler."
    • also criticalblue.com/news/wp-content/uploads/2013/12/… "该列表用于将不同类型的任务优先于其他任务。...完整列表如下所示:stop_sched_class → rt_sched_class → fair_sched_class → idle_sched_class → NULL Stop 和 Idle 是特殊调度类,stop用于调度per-cpu stop任务,抢占一切,可以不被任何东西抢占,Idle用于调度per-cpu空闲任务(也称为swapper task)运行如果没有其他任务可运行。”
    • 谢谢 osgx :) 这就是我要找的。​​span>
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 2021-10-09
    • 2012-12-03
    • 1970-01-01
    • 2021-02-09
    • 2020-07-09
    • 2021-01-02
    • 2015-11-29
    相关资源
    最近更新 更多