我试图定位Linux的调度程序,并在其中找到将下一个进程添加到run que (CPU的交换控制)的函数。
环顾四周,我“认为”这将是sched.c
,我“认为”prepare_task_switch
是基于它的描述的这个函数,但我不能百分之百肯定。
概览定义是有意义的,但是我似乎找不到关于它的内部方法的任何其他信息:fire_sched_out_preempt_notifiers(prev,next)、prepare_lock_switch(rq,next)、prepare_arch_switch(next),,因此我怀疑这些函数是否是导致任务交换发生的原因(更改CPU所有权)。
如果我在正确的代码位置,有人能给我提供一些明确的这三个功能做什么吗?否则,有人能提供一些关于我应该在哪里寻找调度程序在哪里切换任务控制CPU的运行que吗?
位置:/source/linux/内核/linux.c
/**
* prepare_task_switch - prepare to switch tasks
* @rq: the runqueue preparing to switch
* @prev: the current task that is being switched out
* @next: the task we are going to switch to.
*
* This is called with the rq lock held and interrupts off. It must
* be paired with a subsequent finish_task_switch after the context
* switch.
*
* prepare_task_switch sets up locking and calls architecture specific
* hooks.
*/
2770 static inline void
2771 prepare_task_switch(struct rq *rq, struct task_struct *prev,
2772 struct task_struct *next)
2773 {
2774 fire_sched_out_preempt_notifiers(prev, next);
2775 prepare_lock_switch(rq, next);
2776 prepare_arch_switch(next);
2777 }
发布于 2014-10-14 14:33:20
在C中,函数被标记为“静态”这一事实告诉您,它只能从同一个文件中调用。因此,如果在内核中搜索prepare_task_switch,您会发现它是从context_switch调用的。(prepare_task_switch本身并不是在做有趣的事情:它只是“准备”。)
如果您依次搜索context_switch (也标记为“静态”),您会发现它是从__schedule调用的,这是调度程序的实际核心。实际上,在调用context_switch时,__schedule已经计算出要运行的下一个任务,因为这将作为参数传递给context_switch。
请注意,要运行的任务实际上是在context_switch中控制CPU (从这里调用,从技术上讲是在switch_to()中)。确切的工作方式有点棘手,因为当switch_to返回时,您将不再在同一任务中执行。CPU现在实际上正在运行一个不同的任务。堆栈指针和整个调用堆栈与输入context_switch时不同,因此“当前”调用堆栈中的每个局部变量都可能与以前不同。
至于选择下一个要运行的任务,pick_next_task会这样做。__schedule()中的关键位是:
put_prev_task(rq, prev); /* Put old task at end of queue */
next = pick_next_task(rq); /* Get next task to run */
...
if (prev != next) { /* Nothing to do if it's the same task */
...
context_switch(rq, prev, next); /* Switch to new task */
[now running in new task]
}
其余的是锁定细节和簿记(尽管非常重要)。
https://stackoverflow.com/questions/26341120
复制相似问题