




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
LinuxASL1233Threadswithinathreadgroupsharealltheirglobalvariablesandhavethesameheap.Butthethreadshavedifferentstacks(theydon’tsharelocalvariable)Threadsareprocessesthathappentosharetheglobalmemoryspace.4 dividestimeintoallocatestheslicestoprocessesaccordingtosome進(jìn)程IDEveryLinuxprocesshasauniqueidentifierwhichiscalleditsprocessidentifier(PID) ReferenceIngeneralterms,oneormore“holder”objectscarryapointertoashareddataobjectthatincludesanintegercalleditsreference 56include/lisched.hkernelsoftirq. fork. signal.
iutil.arch/i386/kerneprocess.entry. traps. irq. cessor. irq. hardirq. softirq. spinlock. ......7有進(jìn) 在內(nèi)核中有一個(gè)task_struct數(shù)據(jù)結(jié)構(gòu),即通常所說(shuō)的“進(jìn)有獨(dú)立的空間,這意味著擁有專有的用戶空間;進(jìn)一步,還意味著除前述的內(nèi)核空間堆棧外還有其的用戶空間堆棧。有一點(diǎn)必須,內(nèi)核空間是不能獨(dú)立的,任899ProcessID(PID每個(gè)進(jìn)程以一幾個(gè)相關(guān)的用 PIDhash0Struct Structstructtask_struct{volatilelongstate;…structlist_head…structtask_struct*next_task,…pid_t…structtask_struct*p_opptr,…structtask_struct*pidhash_nextstructtask_struct**pidhash_ppr…}
通過(guò)信號(hào)(signal)或定時(shí)中斷喚醒后進(jìn)入就緒隊(duì)列run-queue。#defineTASK_UNINTERRUPTIBLE #defineTASK_STOPPED ,, sleep_on() 擁有執(zhí)行
或 后
Structstructtask_struct{volatilelongstate;…structlist_head…structtask_struct*next_task,…pid_t…structtask_struct*p_opptr,…structtask_struct
structlist_headstructlist_head*next,#defineLIST_HEAD(name)\structlist_headname={……}staticstructtask_struct…}Structtaskstaticinlinevoidadd_to_runqueue(structtask_struct*{struct
volatilelong…structlist_head…structtask_struct*next_task,…pid_t…structtask_struct*p_opptr,…structtask_struct*pidhash_nextstructtask_struct**pidhash_ppr…}
staticinlinevoidmove_last_runqueue(structtask_struct*{}staticinlinevoiddel_from_runqueue(structtask_struct*{p->run_list.next=}staticinlineinttask_on_runqueue(structtask_struct{return(p->run_list.next!=}Structstructtask_struct{volatilelongstate;………pid_t………}Structstructtask_struct{volatilelongstate;…structlist_head…structtask_struct*next_task,*prev …pid_t…
typedef typedef kernel_pid_t;structtask_struct*p_opptr,…structtask_struct*pidhash_next;structtask_struct**pidhash_pprev;…}
Structstructtask_struct{volatilelongstate;………pid_t……} }Structstructtask_struct{volatilelongstate;…structlist_head…structtask_struct*next_task,…pid_t…structtask_struct*p_opptr,*p_pptr,…structtask_struct*pidhash_next;structtask_struct**pidhash_pprev;…
}ProcessID(PID#definePIDHASH_SZ(4096>>#definepid_hashfn(x)((((x)>>8)^(x))&(PIDHASH_SZ-staticinlinevoidhash_pid(structtask_struct{if((p->pidhash_next=*htable)!=*htable=}staticinlinevoidunhash_pid(structtask_struct{*p->pidhash_pprev=p-}{for(p=*htable;p&&p->pid!=pid;p=p-;return}ProcessID(PIDintlast_pid;staticintget_pid(unsignedlong{staticintnext_safe=PID_MAX;structtask_struct*p;intpid,if(flags&beginpid=last_pid;if((++last_pid)&0xffff8000){last_pid=300;gotoinside;}
Thenext_safevariableisaspeedhack;itkeepstrackofthenext-lowestcandidatePIDthatmightbeThelast_pidvariableisthePIDthatwastakenbylasttask如果last_pid超過(guò)最大允許值回滾到義。ProcessID(PID
if(last_pid>=next_safe)next_safe=PID_MAX;
for(p=&init_task;(p=p->next_task)!=&init_task;
if(p->pid== p->pgrp== p->tgid==last_pid p->session==last_pid){if(++last_pid>=next_safe)if(last_pid&last_pid=300;next_safe=PID_MAX;}gotogoto}…}}pid=returnpid;…}
IfanewtaskisbeingcreatedandtheonlyavailablePIDsarebelow300,thisloopwillsimplycontinueuntilsomeprocesswithahigher-numberedPIDexits. 先進(jìn)先出(first in first out)按照在調(diào)度隊(duì)列中的順序運(yùn)行, /kernel/sched.h/kernel/sched.h#defineSCHED_FIFO#defineSCHED_RR012 Linux1,
schedule()。Linux2(),在schedule中,先檢查是否是中斷服務(wù)程序調(diào)用了schedule(這是不允許的),如果是則退出schedule。若不是,則檢查是否有bottomhalf服務(wù)請(qǐng)求,若有則執(zhí)行do_half_bottom。 接下來(lái),便是調(diào)度正文。通過(guò)函數(shù)goodness()遍歷運(yùn)行隊(duì)列中所有的進(jìn)最后通過(guò)宏witch_to()切換堆棧,從而達(dá)到從當(dāng)前進(jìn)程切換到選中的進(jìn)
/*needcontext/*needcontextswitch*/if(save_context()){/*pickanothertasktorunfromrun_queue*//*Thecontroldoesnotarrivehere,NEVER!!!*/}/*resumingprocessexecutesfromhere!!!*/fork(、clone和vfork(遞給do_fork()的參數(shù)不同,因此創(chuàng)建出的進(jìn)程是不同的。() ()asmlinkageintsys_fork(structpt_regs{新建子進(jìn)程分配task_struct新建子進(jìn)程分配task_struct,}asmlinkageintsys_clone(structpt_regs{unsignedlongclone_flags;unsignedlongnewsp;clone_flags=regs.ebx;newsp=regs.ecx;ifnewsp=returndo_fork(clone_flags,newsp,®s,}asmlinkageintsys_vfork(structpt_regs{returndo_fork(CLONE_VFORK|CLONE_VM|SIGCHLD,regs.esp,®s,}intdo_fork(unsignedlongclone_flags,unsignedlongstack_start,structpt_regs*regs,unsignedlongstack_size){intstructtask_struct*p;
CLONE_PIDisonlyallowedfortheinitialSMPswappercallsif((clone_flags&(CLONE_NEWNS|CLONE_FS)) return-retval=-if(clone_flagsif}retval=-
#definealloc_task_struct()((structtask_struct*) if(!p)goto*p=intdo_fork(unsignedlongclone_flags,unsignestructpt_regs*regs,unsignedlong{intstructtask_struct*p;if((clone_flags&retval=-EPERM;if(clone_flags&CLONE_PID)
Thecurrent,running,processispointedtobythecurrentstaticinlinestructtask_struct*{structtask_structasm("andl%%esp,%0;":"=r"(current):"0"(~8191UL));returncurrent;}}retval=-p=if
gotogoto*p=p->pid=…
GetaPIDbelongtothisp->run_list.next=NULL;p->run_list.prev=…p->p_pptr=current->p_pptr;if(!(clone_flags&CLONE_PARENT)){p->p_opptr=current;if(!(p->ptrace&PT_PTRACED))p->p_pptr=current;}if(clone_flags&CLONE_THREAD){p->tgid=current->tgid;
Initially,thenewprocessisnot cedintherunqueue.It’sstillpossiblethatdo_forkwillfail,anditwouldbewastefultoputthenewprocessintherunqueue,onlytotakeitrightbackoutifsomethinggoeswrong.Moreseriously,thenewtaskisnot yinitializedyet,andwewouldn’twantanotherCPUtohandcontroltothisprocessprematurely.}……
/*dothislastp->pid=…p->run_list.next=NULL;p->run_list.prev=…p->p_pptr=current->p_pptr;if(!(clone_flags&CLONE_PARENT)){p->p_opptr=current;if(!(p->ptrace&PT_PTRACED))p->p_pptr=current;
Normally,do_fork’scallershouldberegisteredastheparentofthenewprocess.TheonlyexceptioniswhentheCLONE_PARENTflagisset.Thisflagmeansthatthenewprocessshouldhavethesameparentasdo_fork’scaller.}if(clone_flags&CLONE_THREAD){p->tgid=current->tgid;}……
Ifthecallingprocessisnotbeingtraced,thecallerisalsomadethenewprocess’slogicalparent–theonetowhichsignalnotificationsaresent.However,ifthecallerisbeingtraced,thechild’slogicalparentwillremainthesameasitsparent’slogicalparent,thedebuggerprocess./*dothis p->pid=…p->run_list.next=NULL;p->run_list.prev=…p->p_opptr=current->p_opptr;>p_pptr=current-if(!(clone_flags&CLONE_PARENT)){p->p_opptr=current;if(!(p->ptrace&PT_PTRACED))>p_pptr=}if(clone_flags&CLONE_THREAD){>tgid=current-}……if(clone_flags&CLONE_VFORK)
#defineSET_LINKS(p)do{=\(p)->prev_task=init_task.prev_task;\pinit_task.prev_task->next_task=(p);\進(jìn)程p的進(jìn)一進(jìn)程指向pinit_task.prev_task=(p);\ ;if(((p)->p_osptr=(p)->p_pptr->p_cptr)!=NULL)(p)->p_osptr->p_ysptr=p;\}whilep->pid=…p->run_list.next=NULL;p->run_list.prev=…p->p_opptr=current->p_opptr;>p_pptr=current-if(!(clone_flags&CLONE_PARENT)){p->p_opptr=current;if(!(p->ptrace&PT_PTRACED))>p_pptr=}if(clone_flags&CLONE_THREAD){>tgid=current-}……if(clone_flags&CLONE_VFORK)
staticinlineinttry_to_wake_up(structtask_struct*p,int{intsuccess=0;*Wewantthecommoncasefallthroughstraight,thusthep->state=TASK_RUNNING;ifif(!synchronous||!(p->cpus_allowed&(1<<success=1;returnsuccess;}inlineintwake_up_process(structtask_struct*{}VirtualFileSystem&structfs_structstructfs_struct/*checkinginformationusedwhenaccessingfilesstructfiles_struct*files/*pointertothefiledescriptorsopenedtt:00do_sigaction(sig,new_sa,do_sigaction(sig,new_sa,old_s*t->sigpending=1;Virtual:structmm_struct:structmm_struct /*controlinformationusedformemorymanagement:vm
vmvmvmvmThreadThread unsignedlongunsignedlongesp0;unsignedshortss0;unsignedlongesp1;unsignedshortss1;unsignedlongesp2;unsignedshortss2;unsignedlongcr3;unsignedlongeip,eflags;unsignedlongeax,ecx,edx,ebx;unsignedlongesp;unsignedlongebp,esi,edi;unsignedshortes,cs,ss,ds,fs,gs;unsignedshortldt;要讓若干新進(jìn)程按照需要處理不同的事情,就必須通過(guò)系統(tǒng)調(diào)用exec(這實(shí)際上不止是一個(gè)名為exec的函數(shù);而是exec通常用作一個(gè)一系列函數(shù)的通用術(shù)語(yǔ),所有這些do_execve()是實(shí)現(xiàn)所有exec函數(shù)的底層內(nèi)核函數(shù)。獲取參數(shù)區(qū)長(zhǎng)度,調(diào)用memset()將存放參數(shù)的頁(yè)面。memset()屬于管理部分的函數(shù)。linux_binprm結(jié)構(gòu)用來(lái)并第一種情況是:在執(zhí)行do_fork時(shí),若函數(shù)的參數(shù)中的位置一,即父子進(jìn)程共戶空間。這時(shí),必須先運(yùn)行子進(jìn)程。在do_fork中系統(tǒng)會(huì)對(duì)父進(jìn)程執(zhí)行一個(gè)down()操作,使父進(jìn)程進(jìn)入臨界區(qū)并因?yàn)榈貌坏脚R界資源而轉(zhuǎn)入睡眠,從而達(dá)到等待子進(jìn)程的目第二種情況是在應(yīng)用程序中直接利用調(diào)用系統(tǒng)調(diào)用wait4( )來(lái)達(dá)到讓父進(jìn)程轉(zhuǎn)入睡眠而等待子進(jìn)程的目gcc在編連程序的時(shí),會(huì)自動(dòng)加入exit系統(tǒng)調(diào)用。這樣,任何 sys_exit()是系統(tǒng)調(diào)用exit對(duì)應(yīng)的內(nèi)核服務(wù)程序。它的函數(shù)體內(nèi)僅有一條語(yǔ)句,即調(diào)用do_exit(完 程在do_exit()中調(diào)用exit_notify( fork(從系統(tǒng)調(diào)用ENTRY(system_call)執(zhí)行系統(tǒng)調(diào)用總控部分的代碼進(jìn)入內(nèi)核,然后調(diào)用sys_fork(),進(jìn)而調(diào)用do_fork()創(chuàng)建一個(gè)子 得CPU,都會(huì)回到系統(tǒng)調(diào)用總控部分的代碼的ret_from_sys_call,從若從程返回,則返回值為0,繼續(xù)執(zhí)行系統(tǒng)調(diào)用execve()按前述方式進(jìn)入內(nèi)核執(zhí)行sys_execve(),進(jìn)而調(diào)用do_execve()裝入可執(zhí)行文件并執(zhí)行若從父進(jìn)程返回,則返回值為程pid,繼續(xù)執(zhí)行系統(tǒng)調(diào)用wait()述方式進(jìn)入內(nèi)核執(zhí)行sys_wait轉(zhuǎn)入睡眠,等待 父進(jìn)程喚醒后,在sys_wait()中清除 asmlinkagelongsys_exit(int{}NORET_TYPEvoiddo_exit(long{structtask_struct*tsk=ifif(!tsk->pid)panic("Attemptedtokilltheidleif(tsk->pid==tsk->flags|=PF_EXITING
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 栗子銷(xiāo)售合同
- 車(chē)間材料供貨合同樣本2
- 車(chē)輛維修合同書(shū)范本
- 毽球運(yùn)動(dòng) 教學(xué)設(shè)計(jì)-2023-2024學(xué)年高一上學(xué)期體育與健康人教版必修第一冊(cè)
- 第一單元第五課三、《利用函數(shù)計(jì)算》教學(xué)設(shè)計(jì) 2023-2024學(xué)年新世紀(jì)版(2018)初中信息技術(shù)七年級(jí)下冊(cè)
- 全國(guó)青島版信息技術(shù)八年級(jí)上冊(cè)專題五第1課二、《問(wèn)問(wèn)題》教學(xué)設(shè)計(jì)
- 2025年保健品銷(xiāo)售代理合同格式
- 2025合作伙伴年度服務(wù)合同
- 2025年供暖管道安裝與維護(hù)合同
- 2025年高級(jí)職員合同翻譯合約
- 設(shè)備損壞評(píng)估報(bào)告范文
- 標(biāo)準(zhǔn)和計(jì)量管理制度范文(2篇)
- 透析患者心理問(wèn)題護(hù)理干預(yù)
- 孕前口腔護(hù)理保健
- 《民航服務(wù)與溝通學(xué)》課件-第1講 服務(wù)與民航服務(wù)的概念
- 《大學(xué)生安全教育》課件 項(xiàng)目四 軍事安全
- 10KV電力配電工程施工方案
- 智能感知工程基礎(chǔ)知識(shí)單選題100道及答案解析
- 肌肉注射藥物不良反應(yīng)及預(yù)防措施研究
- 人教版數(shù)學(xué)六年級(jí)上冊(cè)第一單元測(cè)試卷
- 大型養(yǎng)路機(jī)械司機(jī)(打磨車(chē))高級(jí)工技能鑒定考試題庫(kù)(含答案)
評(píng)論
0/150
提交評(píng)論