【益西拉姆 原創做品轉載請註明出處 《Linux內核分析》MOOC課程http://mooc.study.163.com/course/USTC-1000029000】node
進程控制塊PCB——task_struct編程
爲了管理進程,內核必須對每一個進程進行清晰的描述,進程描述符提供了內核所需瞭解的進程信息。數據結構
`#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char * argv[]) { int pid; / * fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr,"Fork Failed!"); exit(-1); } else if (pid == 0) { /* child process */ printf("This is Child Process!\n"); } else { /* parent process */ printf("This is Parent Process!\n"); /* parent will wait for the child to complete*/ wait(NULL); printf("Child Complete!\n"); } }
`框架
Linux經過複製父進程來建立一個新進程,那麼這就給咱們理解這一個過程提供一個想象的框架:函數
err = arch_dup_task_struct(tsk, orig);
`ti = allocthreadinfo_node(tsk, node);this
tsk->stack = ti;操作系統
setupthreadstack(tsk, orig); //這裏只是複製,而非複製內核堆`線程
*childregs = *current_pt_regs(); //複製內核堆棧
childregs->ax = 0; //爲何子進程的fork返回0,這裏就是緣由!
p->thread.sp = (unsigned long) childregs; //調度到子進程時的內核棧頂
p->thread.ip = (unsigned long) ret_from_fork; //調度到子進程時的第一條指令地址
到了15九、160行的代碼就是把壓入的代碼再放到子進程中:3d
`*children = *current_pt_regs(); childregs->ax = 0;`
p->thread.ip = (unsigned long) ret_from_fork;
本週主要就是課本的進程一章的拓展,經過實踐來更加的運用完整,頗有趣。調試