1.进程的销毁流程
1.1exit是销毁函数------------一个系统调用-------do exit
首先该函数会释放进程的代码段和数据段占用的内存
1.2关闭进程打开的所有文件,对当前的目录和i结点进行同步(文件操作)
1.3如果当前要销毁的进程有子进程,那么就让1号进程作为新的父进程(init进程)
1.4如果当前进程是一个会话头进程,则会终止会话中的所有进程
1.5改变当前进程的运行状态,变成TASK_ZOMBIE僵死状态,并向父进程发送SIGCHLD信号
2.1父进程在运行子进程的时候 一般都会运行wait waitpid这两个函数(父进程等待某个子进程终止的),当父进程收到SIGCHLD信号时父进程会终止僵死状态的子进程
2.2首先父进程会把子进程的运行时间累加到自己的进程变量中
3.3把对应的子进程的进程描述结构体进行释放,置空任务数组TASK[]中的空槽
以上是对进程销毁的流程文字描述,下面用代码证明,也就是exit.c文件
void release (struct task_struct *p)
完成清空了任务描述表中的对应进程表项,释放对应的内存页(代码段 数据段 堆栈)
int do_exit (long code) // code 是错误码。
{
int i;
// 释放当前进程代码段和数据段所占的内存页(free_page_tables()在mm/memory.c,105 行)。
free_page_tables (get_base (current->ldt[1]), get_limit (0x0f));
free_page_tables (get_base (current->ldt[2]), get_limit (0x17));
// 如果当前进程有子进程,就将子进程的father 置为1(其父进程改为进程1)。如果该子进程已经
// 处于僵死(ZOMBIE)状态,则向进程1 发送子进程终止信号SIGCHLD。
for (i = 0; i < NR_TASKS; i++)
if (task[i] && task[i]->father == current->pid)
{
task[i]->father = 1;
if (task[i]->state == TASK_ZOMBIE)
/* assumption task[1] is always init */
(void) send_sig (SIGCHLD, task[1], 1);
}
// 关闭当前进程打开着的所有文件。
for (i = 0; i < NR_OPEN; i++)
if (current->filp[i])
sys_close (i);
// 对当前进程工作目录pwd、根目录root 以及运行程序的i 节点进行同步操作,并分别置空。
iput (current->pwd);
current->pwd = NULL;
iput (current->root);
current->root = NULL;
iput (current->executable);
current->executable = NULL;
// 如果当前进程是领头(leader)进程并且其有控制的终端,则释放该终端。
if (current->leader && current->tty >= 0)
tty_table[current->tty].pgrp = 0;
// 如果当前进程上次使用过协处理器,则将last_task_used_math 置空。
if (last_task_used_math == current)
last_task_used_math = NULL;
// 如果当前进程是leader 进程,则终止所有相关进程。
if (current->leader)
kill_session ();
// 把当前进程置为僵死状态,并设置退出码。
current->state = TASK_ZOMBIE;
current->exit_code = code;
// 通知父进程,也即向父进程发送信号SIGCHLD -- 子进程将停止或终止。
tell_father (current->father);
schedule (); // 重新调度进程的运行。
return (-1); /* just to suppress warnings */
}
1.1首先该函数会释放进程的代码段和数据段占用的内存,也就是ldt[1]和ldt[2];
free_page_tables (get_base (current->ldt[1]), get_limit (0x0f));
free_page_tables (get_base (current->ldt[2]), get_limit (0x17));
1.2关闭进程打开的所有文件,对当前目录和i节点进行同步(文件操作)
for (i = 0; i < NR_OPEN; i++)
if (current->filp[i])
sys_close (i);
// 对当前进程工作目录pwd、根目录root 以及运行程序的i 节点进行同步操作,并分别置空。
iput (current->pwd);
current->pwd = NULL;
iput (current->root);
current->root = NULL;
iput (current->executable);
current->executable = NULL;
1.3如果当前要销毁的进程有子进程,那么就让1号进程作为新的父进程(init进程)
for (i = 0; i < NR_TASKS; i++)
if (task[i] && task[i]->father == current->pid)
{
task[i]->father = 1;
if (task[i]->state == TASK_ZOMBIE)
/* assumption task[1] is always init */
(void) send_sig (SIGCHLD, task[1], 1);
}
1.4如果当前进程是一个会话头进程,则会终止会话中的所有进程
if (current->leader)
kill_session ();
1.5改变当前进程的运行状态,变成TASK_ZOMBIE僵死状态,并向父进程发送SIGCHLD信号
current->state = TASK_ZOMBIE;
current->exit_code = code;
// 通知父进程,也即向父进程发送信号SIGCHLD -- 子进程将停止或终止。
tell_father (current->father);
1.6重新调度进程
schedule (); // 重新调度进程的运行。
2.父进程
int sys_waitpid (pid_t pid, unsigned long *stat_addr, int options)
{
int flag, code;
struct task_struct **p;
verify_area (stat_addr, 4);
repeat:
flag = 0;
for (p = &LAST_TASK; p > &FIRST_TASK; --p)
{ // 从任务数组末端开始扫描所有任务。
if (!*p || *p == current) // 跳过空项和本进程项。
continue;
if ((*p)->father != current->pid) // 如果不是当前进程的子进程则跳过。
continue;
if (pid > 0)
{ // 如果指定的pid>0,但扫描的进程pid
if ((*p)->pid != pid) // 与之不等,则跳过。
continue;
}
else if (!pid)
{ // 如果指定的pid=0,但扫描的进程组号
if ((*p)->pgrp != current->pgrp) // 与当前进程的组号不等,则跳过。
continue;
}
else if (pid != -1)
{ // 如果指定的pid<-1,但扫描的进程组号
if ((*p)->pgrp != -pid) // 与其绝对值不等,则跳过。
continue;
}
switch ((*p)->state)
{
case TASK_STOPPED:
if (!(options & WUNTRACED))
continue;
put_fs_long (0x7f, stat_addr); // 置状态信息为0x7f。
return (*p)->pid; // 退出,返回子进程的进程号。
case TASK_ZOMBIE:
current->cutime += (*p)->utime; // 更新当前进程的子进程用户
current->cstime += (*p)->stime; // 态和核心态运行时间。
flag = (*p)->pid;
code = (*p)->exit_code; // 取子进程的退出码。
release (*p); // 释放该子进程。
put_fs_long (code, stat_addr); // 置状态信息为退出码值。
return flag; // 退出,返回子进程的pid.
default:
flag = 1; // 如果子进程不在停止或僵死状态,则flag=1。
continue;
}
}
if (flag)
{ // 如果子进程没有处于退出或僵死状态,
if (options & WNOHANG) // 并且options = WNOHANG,则立刻返回。
return 0;
current->state = TASK_INTERRUPTIBLE; // 置当前进程为可中断等待状态。
schedule (); // 重新调度。
if (!(current->signal &= ~(1 << (SIGCHLD - 1)))) // 又开始执行本进程时,
goto repeat; // 如果进程没有收到除SIGCHLD 的信号,则还是重复处理。
else
return -EINTR; // 退出,返回出错码。
}
return -ECHILD;
}
释放指定进程(任务)。
void release (struct task_struct *p)
{
int i;
if (!p)
return;
for (i = 1; i < NR_TASKS; i++) // 扫描任务数组,寻找指定任务。
if (task[i] == p)
{
task[i] = NULL; // 置空该任务项并释放相关内存页。
free_page ((long) p);
schedule (); // 重新调度。
return;
}
panic ("trying to release non-existent task"); // 指定任务若不存在则死机。
}
代码很简单,看注释就看得懂