【益西拉姆 原创做品转载请注明出处 《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;
本周主要就是课本的进程一章的拓展,经过实践来更加的运用完整,颇有趣。调试