您的位置:首页 > 运维架构 > Linux

20135327郭皓--Linux内核分析第六周 进程的描述和进程的创建

2016-04-03 16:03 531 查看

进程的描述和进程的创建

一、进程的描述

操作系统三大功能:

进程管理

内存管理

文件系统

进程描述符task_struct数据结构

task _ struct:为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息。

进程的状态:Linux进程的状态(就绪态、运行态、阻塞态)

进程的标示pid:用来标示进程



进程描述符task_struct数据结构(重要部分):

struct task_struct {
volatile long state;    /* 进程的运行状态-1 unrunnable, 0 runnable, >0 stopped */
void *stack;    /*指定了进程的内核堆栈*/
atomic_t usage;
unsigned int flags; /* 每一个进程的标识符 */

int on_rq;  /*运行队列*/

pid_t pid; /*进程标识符*/

struck list_head task;  /*进程链表*/

/*父子进程*/
struct task_struct __rcu *real_parent; /* real parent process */
struct task_struct __rcu *parent;

struct list_head children;    /* list of my children */


二、进程的创建

start _ kernel代码中的rest _ init创建两个内核线程,kernel _ init和kthreadd。

kernel _ init将用户态进程init启动,是所有用户态进程的祖先。

kthreadd是所有内核线程的祖先。

0号进程是所有线程的祖先。(0号进程时手工写的)

fork一个子进程的代码:

#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");
}
}


这里特别说明一下:else if和if两个模块的代码都会被执行,子进程和父进程都会返回,子进程返回0,父进程返回子进程pid。

实验:

1. 删除menu,克隆新的menu。



2.查看help



3.gdb调试



4.设置断点



5.单步追踪









总结:

新的进程从哪里开始?

*childregs = *current_pt_regs(); //复制内核堆栈
childregs->ax = 0; //为什么子进程的fork返回0,这里就是原因!

p->thread.sp = (unsigned long) childregs; //调度到子进程时的内核栈顶
p->thread.ip = (unsigned long) ret_from_fork; //调度到子进程时的第一条指令地址


Linux通过复制父进程来创建一个新进程:复制父进程PCB--task_struct来创建一个新进程,要给新进程分配一个新的内核堆栈。

修改复制过来的进程数据,比如pid、进程链表等等执行copy_process和copy_thread。

设置sp调度到子进程时的内核栈顶,ip转到子进程时的第一条指令地址

之后,当子进程获得CPU的控制权开始运行的时候,ret _ form _ fork可以将后面的堆栈出栈,从iret返回到用户态,从而切换到子进程的用户空间,完成新进程的创建。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: