进程调度与进程切换
1. 基本概念
1.1 调度和切换的目的(1) 不同的进程的属性不同,如计算密集型和I/O密集型,对CPU的依赖程度也就不同。内核需要管理各个进程的执行流,考虑到进程调度的公平(分时)和效率(优先级),需要挂起正在CPU上执行的进程,选择一个优先级较高的进程把CPU资源分配给它,即这是一个CPU资源的再分配的需求和过程。
1.2 调度与切换的区别
(1) 调度侧重于选择,而切换则是行动,是在调度基础之上的切换;类比网络中的路由跟转发,路由侧重于选择路径,而转发是在选择好的路径上进行发送数据;
(2) 也可以这么理解,调度包含了切换,切换仅仅是调度的一个部分;切换是基于调度算法选择好下个进程之后的工作;
2. 进程调度
2.1 调度时机* Linux中进程的调度是通过schedule()函数来实现的,它会在运行队列中找到一个进程,然后把CPU分配给它;
* 调用schedule()的方式:中断和内核线程
* 中断:中断之中可以直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule();中断包括时钟中断、I/O中断、系统调用和异常;
* 内核线程:内核线程可以直接调用schedule()进行进程切换,也可以在中断处理过程中进行调度,也就是说内核线程作为一类的特殊的进程可以主动调度,也可以被动调度;
* 用户态进程无法实现主动调度,仅能通过陷入内核态后的某个时机点进行调度,即在中断处理过程中进行调度。
3. 进程切换
3.1 切换基本概念(1) 为了控制进程的执行,内核必须有能力挂起正在CPU上执行的进程,并恢复以前挂起的某个进程的执行(进程切换、任务切换、上下文切换)。进程切换是站在CPU的角度来说的,是内核决定哪个进程具有获得CPU的优先权,并把CPU分配给该进程;
(2) 挂起正在CPU上执行的进程,与中断时保存现场是不同的,中断前后是在同一个进程上下文中,只是由用户态转向内核态执行,而进程的上下文切换则是在两个不同的进程间进行的。需注意的是,中断可以引发调度,从而导致上下文切换;
3.2 上下文切换的信息
(1) 用户地址空间:包括程序代码,数据,用户堆栈等;
(2) 控制信息:进程描述符,内核堆栈等;
(3) 硬件上下文;
4. 调度切换流程
4.1 一般的情况(1) 正在运行的用户态进程X;
(2) 发生中断——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).
(3) SAVE_ALL //保存现场;
(4) 中断处理过程中或中断返回前调用了schedule(),其中的switch_to做了关键的进程上下文切换;
(5) 标号1之后开始运行用户态进程Y(这里Y曾经通过以上步骤被切换出去过因此可以从标号1继续执行);
(6) restore_all //恢复现场;
(7) iret - pop cs:eip/ss:esp/eflags from kernel stack;
(8) 继续运行用户态进程Y;
4.2 特殊情况
(1) 通过中断处理过程中的调度时机,用户态进程与内核线程之间互相切换和内核线程之间互相切换,与最一般的情况非常类似,只是内核线程运行过程中发生中断没有进程用户态和内核态的转换;
(2) 内核线程主动调用schedule(),只有进程上下文的切换,没有发生中断上下文的切换,与最一般的情况略简略;
(3) 创建子进程的系统调用在子进程中的执行起点及返回用户态,如fork;
(4) 加载一个新的可执行程序后返回到用户态的情况,如execve;
5. 实验
* 调度时机的验证
(1) schedule()函数及其他代码分析
asmlinkage __visible void __sched schedule(void)
{
struct task_struct *tsk = current;
sched_submit_work(tsk);
__schedule();
}
static void __sched __schedule(void)
{
struct task_struct *prev, *next;
unsigned long *switch_count;
struct rq *rq;
int cpu;
need_resched:
preempt_disable();
cpu = smp_processor_id();
rq = cpu_rq(cpu);
rcu_note_context_switch(cpu);
prev = rq->curr;
schedule_debug(prev);
if (sched_feat(HRTICK))
hrtick_clear(rq);
/*
* Make sure that signal_pending_state()->signal_pending() below
* can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
* done by the caller to avoid the race with signal_wake_up().
*/
smp_mb__before_spinlock();
raw_spin_lock_irq(&rq->lock);
switch_count = &prev->nivcsw;
if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
if (unlikely(signal_pending_state(prev->state, prev))) {
prev->state = TASK_RUNNING;
} else {
deactivate_task(rq, prev, DEQUEUE_SLEEP);
prev->on_rq = 0;
/*
* If a worker went to sleep, notify and ask workqueue
* whether it wants to wake up a task to maintain
* concurrency.
*/
if (prev->flags & PF_WQ_WORKER) {
struct task_struct *to_wakeup;
to_wakeup = wq_worker_sleeping(prev, cpu);
if (to_wakeup)
try_to_wake_up_local(to_wakeup);
}
}
switch_count = &prev->nvcsw;
}
if (task_on_rq_queued(prev) || rq->skip_clock_update < 0)
update_rq_clock(rq);
next = pick_next_task(rq, prev);
clear_tsk_need_resched(prev);
clear_preempt_need_resched();
rq->skip_clock_update = 0;
if (likely(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);
post_schedule(rq);
sched_preempt_enable_no_resched();
if (need_resched())
goto need_resched;
}
/*
* context_switch - switch to the new MM and the new
* thread's register state.
*/
static inline void
context_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next)
{
struct mm_struct *mm, *oldmm;
prepare_task_switch(rq, prev, next);
mm = next->mm;
oldmm = prev->active_mm;
/*
* For paravirt, this is coupled with an exit in switch_to to
* combine the page table reload and the switch backend into
* one hypercall.
*/
arch_start_context_switch(prev);
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;
}
/*
* Since the runqueue lock will be released by the next
* task (which is an invalid locking op but in the case
* of the scheduler it's an obvious special-case), so we
* do an early lockdep release here:
*/
spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
context_tracking_task_switch(prev, next);
/* Here we just switch the register state and the stack. */
switch_to(prev, next, prev);
barrier();
/*
* this_rq must be evaluated again because prev may have moved
* CPUs since it called schedule(), thus the 'rq' on its stack
* frame will be invalid.
*/
finish_task_switch(this_rq(), prev);
}
/*
* Saving eflags is important. It switches not only IOPL between tasks,
* it also protects other tasks from NT leaking through sysenter etc.
*/
#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)
(2)总的流程
6. 总结
6.1 Linux运行的一般过程
每个进程都有一个用户栈和内核栈,而所有的进程都共享一个内核栈,每当发生中断时,进程先保存中断上下文再保存进程上下文。然后通过switch_to()函数进行进程的切换;若仅仅是进程的切换,则只需要保存进程上下文。内核在这过程中相当于是进程切换的一个中转站,只要需要切换进程,就需要陷入内核,通过内核代表进程来进行进程切换。
6.2 实验心得
了解了Linux运行的基本过程——是一个进程不断切换的集合。通过进程的切换,来达到调度的目的,使得操作系统在效率和公平之间达到一个很好的平衡。
陈金雷 + 原创作品转载请注明出处 + 《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000