Linux进程调度——schedule()函数分析
来源:互联网 发布:ios 可变字典添加数据 编辑:程序博客网 时间:2024/06/06 06:31
刘森林 原创作品转载请注明出处 《Linux内核分析》MOOC课程
打开终端中输入qemu –kernel linux-3.18.6/arch/x86/boot/bzImage –initrd rootfs.img –S –s
然后打开另一个终端输入
gdb(gdb)file linux-3.18.6/vmlinux(gdb)target remote:1234(gdb)b schedule(gdb)c
进行调试跟踪schedule的执行过程。
进程调度时,首先进入schedule()函数,将一个task_struct结构体的指针tsk赋值为当前进程。
然后调用sched_submit_work(tsk)
我们进入这个函数,查看一下做了什么工作
我们在执行到sched_submit_work时,输入si进入函数。
可以看到这个函数时检测tsk->state是否为0 (runnable)若为运行态时则返回,
tsk_is_pi_blocked(tsk),检测tsk的死锁检测器是否为空,若非空的话就return。
然后检测是否需要刷新plug队列,用来避免死锁。
sched_submit_work主要是来避免死锁。
然后我们进入__schedule()函数。
__schedule()是切换进程的真正代码,我们来分析一下具体的关键代码
- 1.创建一些局部变量
struct task_struct *prev, *next;//当前进程和一下个进程的进程结构体unsigned long *switch_count;//进程切换次数struct rq *rq;//就绪队列int cpu;
- 2.关闭内核抢占,初始化一部分变量
need_resched:preempt_disable();//关闭内核抢占cpu = smp_processor_id();rq = cpu_rq(cpu);//与CPU相关的runqueue保存在rq中rcu_note_context_switch(cpu);prev = rq->curr;//将runqueue当前的值赋给prev
- 3.选择next进程
next = pick_next_task(rq, prev);//挑选一个优先级最高的任务排进队列clear_tsk_need_resched(prev);//清除prev的TIF_NEED_RESCHED标志。clear_preempt_need_resched();
- 4.完成进程的调度
if (likely(prev != next)) {//如果prev和next是不同进程 rq->nr_switches++;//队列切换次数更新 rq->curr = next; ++*switch_count;//进程切换次数更新 context_switch(rq, prev, next); /* unlocks the rq *///进程上下文的切换 /* * The context switch have flipped the stack from under us * and restored the local variables which were saved when * this task called schedule() in the past. prev == current * is still correct, but it can be moved to another cpu/rq. */cpu = smp_processor_id(); rq = cpu_rq(cpu); } else//如果是同一个进程不需要切换 raw_spin_unlock_irq(&rq->lock);
这段代码中context_switch(rq,prev,next)完成了从prev到next的进程上下文的切换。我们进入这个函数查看
static inline voidcontext_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next){ struct mm_struct *mm, *oldmm;//初始化进程地址管理结构体mm和oldmm prepare_task_switch(rq, prev, next);//完成进程切换的准备工作 mm = next->mm; oldmm = prev->active_mm; /*完成mm_struct的切换*/if (!mm) { next->active_mm = oldmm; atomic_inc(&oldmm->mm_count); enter_lazy_tlb(oldmm, next); } else switch_mm(oldmm, mm, next); if (!prev->mm) { prev->active_mm = NULL; rq->prev_mm = oldmm; }switch_to(prev, next, prev);//进程切换的核心代码barrier();finish_task_switch(this_rq(), prev);}
我们看到在context_switch中使用switch_to(prev,next,prev)来切换进程。我们查看一下switch_to的代码。
switch_to是一个宏定义,完成进程从prev到next的切换,首先保存flags,然后保存当前进程的ebp,然后把当前进程的esp保存到prev->thread.sp中,然后把标号1:的地址保存到prev->thread.ip中。
然后把next->thread.ip压入堆栈。这里,如果之前B也被switch_to出去过,那么next->thread.ip里存的就是下面这个1f的标号,但如果next进程刚刚被创建,之前没有被switch_to出去过,那么next->thread.ip里存的将是ret_ftom_fork
__switch_canqry应该是现代操作系统防止栈溢出攻击的金丝雀技术。
jmp __switch_to使用regparm call, 参数不是压入堆栈,而是使用寄存器传值,来调用__switch_to
eax存放prev,edx存放next。这里为什么不用call __switch_to而用jmp,因为call会导致自动把下面这句话的地址(也就是1:)压栈,然后__switch_to()就必然只能ret到这里,而无法根据需要ret到ret_from_fork
当一个进程再次被调度时,会从1:开始执行,把ebp弹出,然后把flags弹出。
#define switch_to(prev, next, last) \do { \ /* \ * Context-switching clobbers all registers, so we clobber \ * them explicitly, via unused output variables. \ * (EAX and EBP is not listed because EBP is saved/restored \ * explicitly for wchan access and EAX is the return value of \ * __switch_to()) \ */ \ unsigned long ebx, ecx, edx, esi, edi; \ \ asm volatile("pushfl\n\t" /* save flags */ \ "pushl %%ebp\n\t" /* save EBP */ \ "movl %%esp,%[prev_sp]\n\t" /* save ESP */ \ "movl %[next_sp],%%esp\n\t" /* restore ESP */ \ "movl $1f,%[prev_ip]\n\t" /* save EIP */ \ "pushl %[next_ip]\n\t" /* restore EIP */ \ __switch_canary \ "jmp __switch_to\n" /* regparm call */ \ "1:\t" \ "popl %%ebp\n\t" /* restore EBP */ \ "popfl\n" /* restore flags */ \ \ /* output parameters */ \ : [prev_sp] "=m" (prev->thread.sp), \ [prev_ip] "=m" (prev->thread.ip), \ "=a" (last), \ \ /* clobbered output registers: */ \ "=b" (ebx), "=c" (ecx), "=d" (edx), \ "=S" (esi), "=D" (edi) \ \ __switch_canary_oparam \ \ /* input parameters: */ \ : [next_sp] "m" (next->thread.sp), \ [next_ip] "m" (next->thread.ip), \ \ /* regparm parameters for __switch_to(): */ \ [prev] "a" (prev), \ [next] "d" (next) \ \ __switch_canary_iparam \ \ : /* reloaded segment registers */ \ "memory"); \} while (0)
- 5.开启抢占
sched_preempt_enable_no_resched();if (need_resched()) goto need_resched;
整个schedule的执行过程可以用下面的流程图表示:
- Linux进程调度——schedule()函数分析
- 进程调度函数schedule()分析
- 【转】进程调度函数schedule()分析
- Linux进程调度时机Schedule函数解析
- linux 调度 schedule分析
- linux内核函数schedule()实现进程的调度
- linux内核函数schedule()实现进程的调度
- Linux的进程调度时机(Schedule函数何时调用)
- Linux的进程调度时机(Schedule函数何时调用)
- Linux的进程调度时机(Schedule函数何时调用)
- linux内核函数schedule()实现进程的调度
- 进程调度——schedule()(一)
- Linux内核进程调度schedule深入理解
- Linux内核进程调度schedule深入理解
- Linux内核进程调度schedule深入理解
- Linux内核进程调度schedule深入理解
- Linux内核进程调度schedule深入理解
- Linux内核进程调度schedule深入理解
- [SpringBoot实践]spring-data-mongo自定义Repository接口及其实现
- android 开发 时间的计算问题
- 25
- linux下的redis+spring集群整合(七)
- lightoj 1025 - The Specials Menu 【区间dp】
- Linux进程调度——schedule()函数分析
- java中文乱码解决之道(五)-----java是如何编码解码的
- poj题目分类
- P网 虚拟币评分
- 大数加法A+B
- Intent的常用Flag参数
- java中文乱码解决之道(六)-----javaWeb中的编码解码
- 查询每个用户最新的日志访问信息
- 非递归求解N皇后问题(回溯法)