资源描述:
《context_switch( )上下文切换》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、context_switch()上下文切换分类:linux_system2010-12-1422:092091人阅读评论(0)收藏举报structthreadloadrunnerdescriptoriolinux(一)http://blog.csdn.net/kernel_details/archive/2007/01/11/1479940.aspxcontext_switch()上下文切换:调用switch_mm(),把虚拟内存从一个进程映射切换到新进程中调用switch_to(),从上一个进程的处理器状态切换到新进程的处理器状态。这包括保存、恢复栈信息和寄存器信息 Theconte
2、xt_switch()functionsetsuptheaddressspaceofnext.theactive_mmfieldoftheprocessdescriptorpointstothememorydescriptorthatisusedbytheprocess,whilethemmfieldpointstothememorydescriptorownedbytheprocess.Fornormalprocesses,thetwofieldsholdthesameaddress;however,akernelthreaddoesnothaveitsownaddressspacean
3、ditsmmfieldisalwayssettoNULL.Thecontext_switch()functionensuresthatifnextisakernelthread,itusestheaddressspaceusedbyprev. context_switch()函数建立next的地址空间。进程描述符的active_mm字段指向进程所使用的内存描述符,而mm字段指向进程所拥有的内存描述符。对于一般的进程,这两个字段有相同的地址,但是,内核线程没有它自己的地址空间而且它的mm字段总是被设置为NULL。context_switch()函数保证:如果next是一个内核线程,它使用
4、prev所使用的地址空间:staticinlinetask_t*context_switch(runqueue_t*rq,task_t*prev,task_t*next){ structmm_struct*mm=next->mm; structmm_struct*oldmm=prev->active_mm;如果next是内核线程,则线程使用prev所使用的地址空;schedule()函数把该线程设置为懒惰TLB模式事实上,每个内核线程并不拥有自己的页表集(task_struct->mm=NULL);更确切地说,它使用一个普通进程的页表集。不过,没有必要使一个用户态线性地址对应的TL
5、B表项无效,因为内核线程不访问用户态地址空间。
6、--------------------------------------
7、
8、 if(unlikely(!mm)){
9、
10、 next->active_mm=oldmm;
11、
12、 atomic_inc(&oldmm->mm_count);
13、
14、 enter_lazy_tlb(oldmm,next);
15、
16、--------------------------------------
17、如果next是一个普通进程,schedule()函数用next的地址空间替换prev的地址空间
18、
19、--------------------------------------
20、
21、 }else
22、
23、 switch_mm(oldmm,mm,next);
24、
25、--------------------------------------
26、如果prev是内核线程或正在退出的进程,context_switch()函数就把指向prev内存描述符的指针保存到运行队列的prev_mm字段中,然后重新设置prev->active_mm
27、----------------------------------
28、
29、 if(unlikely(!
30、prev->mm)){
31、
32、 prev->active_mm=NULL;
33、
34、 WARN_ON(rq->prev_mm);
35、
36、 rq->prev_mm=oldmm;
37、
38、----------------------------------
39、 }context_switch()终于可以调用switch_to()执行prev和next之间的进程切换了
40、-----