Linux进程调度时机和进程切换

进程的调度时机与进程的切换
操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已。
对于理解操作系统的工作机制,反而是进程的调度时机与进程的切换机制更为关键。
进程调度的时机
中断处理过程(包括时钟中断、I/O中断、系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();
内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;
用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。
进程的切换
为了控制进程的执行,内核必须有能力挂起正在CPU上执行的进程,并恢复以前挂起的某个进程的执行,这叫做进程切换、任务切换、上下文切换;
挂起正在CPU上执行的进程,与中断时保存现场是不同的,中断前后是在同一个进程上下文中,只是由用户态转向内核态执行;
进程上下文包含了进程执行需要的所有信息
用户地址空间: 包括程序代码,数据,用户堆栈等
控制信息 :进程描述符,内核堆栈等
硬件上下文(注意中断也要保存硬件上下文只是保存的方法不同)
schedule()函数选择一个新的进程来运行,并调用context_switch进行上下文的切换,这个宏调用switch_to来进行关键上下文切换
next = pick_next_task(rq, prev);//进程调度算法都封装这个函数内部
context_switch(rq, prev, next);//进程上下文切换
switch_to利用了prev和next两个参数:prev指向当前进程,next指向被调度的进程

Linux系统的一般执行过程
最一般的情况:正在运行的用户态进程X切换到运行用户态进程Y的过程
正在运行的用户态进程X
发生中断——save cs:eip/esp/eflags(current) to kernel stack,then load cs:eip(entry of a specific ISR) and ss:esp(point to kernel stack).
SAVE_ALL //保存现场
中断处理过程中或中断返回前调用了schedule(),其中的switch_to做了关键的进程上下文切换
标号1之后开始运行用户态进程Y(这里Y曾经通过以上步骤被切换出去过因此可以从标号1继续执行)
restore_all //恢复现场
iret - pop cs:eip/ss:esp/eflags from kernel stack
继续运行用户态进程Y
几种特殊情况
通过中断处理过程中的调度时机,用户态进程与内核线程之间互相切换和内核线程之间互相切换,与最一般的情况非常类似,只是内核线程运行过程中发生中断没有进程用户态和内核态的转换;
内核线程主动调用schedule(),只有进程上下文的切换,没有发生中断上下文的切换,与最一般的情况略简略;
创建子进程的系统调用在子进程中的执行起点及返回用户态,如fork;
加载一个新的可执行程序后返回到用户态的情况,如execve;
以下是schedule()函数的源码:
2770static void __sched __schedule(void)
2771{
2772	struct task_struct *prev, *next;
2773	unsigned long *switch_count;
2774	struct rq *rq;
2775	int cpu;
2776
2777need_resched:
2778	preempt_disable();
2779	cpu = smp_processor_id();
2780	rq = cpu_rq(cpu);
2781	rcu_note_context_switch(cpu);
2782	prev = rq->curr;
2783
2784	schedule_debug(prev);
2785
2786	if (sched_feat(HRTICK))
2787		hrtick_clear(rq);
2788
2789	/*
2790	 * Make sure that signal_pending_state()->signal_pending() below
2791	 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
2792	 * done by the caller to avoid the race with signal_wake_up().
2793	 */
2794	smp_mb__before_spinlock();
2795	raw_spin_lock_irq(&rq->lock);
2796
2797	switch_count = &prev->nivcsw;
2798	if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
2799		if (unlikely(signal_pending_state(prev->state, prev))) {
2800			prev->state = TASK_RUNNING;
2801		} else {
2802			deactivate_task(rq, prev, DEQUEUE_SLEEP);
2803			prev->on_rq = 0;
2804
2805			/*
2806			 * If a worker went to sleep, notify and ask workqueue
2807			 * whether it wants to wake up a task to maintain
2808			 * concurrency.
2809			 */
2810			if (prev->flags & PF_WQ_WORKER) {
2811				struct task_struct *to_wakeup;
2812
2813				to_wakeup = wq_worker_sleeping(prev, cpu);
2814				if (to_wakeup)
2815					try_to_wake_up_local(to_wakeup);
2816			}
2817		}
2818		switch_count = &prev->nvcsw;
2819	}
2820
2821	if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)
2822		update_rq_clock(rq);
2823
2824	next = pick_next_task(rq, prev);
2825	clear_tsk_need_resched(prev);
2826	clear_preempt_need_resched();
2827	rq->skip_clock_update = 0;
2828
2829	if (likely(prev != next)) {
2830		rq->nr_switches++;
2831		rq->curr = next;
2832		++*switch_count;
2833
2834		context_switch(rq, prev, next); /* unlocks the rq */
2835		/*
2836		 * The context switch have flipped the stack from under us
2837		 * and restored the local variables which were saved when
2838		 * this task called schedule() in the past. prev == current
2839		 * is still correct, but it can be moved to another cpu/rq.
2840		 */
2841		cpu = smp_processor_id();
2842		rq = cpu_rq(cpu);
2843	} else
2844		raw_spin_unlock_irq(&rq->lock);
2845
2846	post_schedule(rq);
2847
2848	sched_preempt_enable_no_resched();
2849	if (need_resched())
2850		goto need_resched;
2851}
我们通过gdb跟踪schedule函数可以看到系统会通过context_switch函数进行上下文的切换,而在调用context_switch函数时又会调用prepare_task_switch来做一些准备工作,


进程切换中,有一个相当重要的函数switch_to,接下来我们看一下switch_to函数的源码。
31#define switch_to(prev, next, last)					\
32do {									\
33	/*								\
34	 * Context-switching clobbers all registers, so we clobber	\
35	 * them explicitly, via unused output variables.		\
36	 * (EAX and EBP is not listed because EBP is saved/restored	\
37	 * explicitly for wchan access and EAX is the return value of	\
38	 * __switch_to())						\
39	 */								\
40	unsigned long ebx, ecx, edx, esi, edi;				\
41									\
42	asm volatile("pushfl\n\t"		/* save    flags */	\
43		     "pushl %%ebp\n\t"		/* save    EBP   */	\
44		     "movl %%esp,%[prev_sp]\n\t"	/* save    ESP   */ \
45		     "movl %[next_sp],%%esp\n\t"	/* restore ESP   */ \
46		     "movl $1f,%[prev_ip]\n\t"	/* save    EIP   */	\
47		     "pushl %[next_ip]\n\t"	/* restore EIP   */	\
48		     __switch_canary					\
49		     "jmp __switch_to\n"	/* regparm call  */	\
50		     "1:\t"						\
51		     "popl %%ebp\n\t"		/* restore EBP   */	\
52		     "popfl\n"			/* restore flags */	\
53									\
54		     /* output parameters */				\
55		     : [prev_sp] "=m" (prev->thread.sp),		\
56		       [prev_ip] "=m" (prev->thread.ip),		\
57		       "=a" (last),					\
58									\
59		       /* clobbered output registers: */		\
60		       "=b" (ebx), "=c" (ecx), "=d" (edx),		\
61		       "=S" (esi), "=D" (edi)				\
62		       							\
63		       __switch_canary_oparam				\
64									\
65		       /* input parameters: */				\
66		     : [next_sp]  "m" (next->thread.sp),		\
67		       [next_ip]  "m" (next->thread.ip),		\
68		       							\
69		       /* regparm parameters for __switch_to(): */	\
70		       [prev]     "a" (prev),				\
71		       [next]     "d" (next)				\
72									\
73		       __switch_canary_iparam				\
74									\
75		     : /* reloaded segment registers */			\
76			"memory");					\
77} while (0)
我们可以从这段汇编代码中看到,先是保存当前进程的上下文,也就是flags,EBP和ESP,然后修改EIP进行进程切换,等进程运行完了再恢复原来的上下文。
实验总结:
通过本次实验我们可以知道内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;而用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。
进程上下文包含了进程执行需要的所有信息:
1、用户地址空间:包括程序代码,数据,用户堆栈等
2、控制信息:进程描述符,内核堆栈等
3、硬件上下文(注意中断也要保存硬件上下文只是保存的方法不同)
系统通过调用schedule()函数选择一个新的进程来运行,并调用context_switch进行上下文的切换,这个宏调用switch_to来进行关键上下文切换。

作者:叶涛
《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值