[Pthread] Linux进程终止过程

 

分类: Linux 527人阅读 评论(0) 收藏 举报
进程终止过程:
进程终止的系统调用sys_exit()通过函数do_exit()实现。do_exit()调用
static inline void __exit_mm(struct task_struct * tsk)
{
struct mm_struct * mm = tsk->mm;

mm_release();
}
释放一部分资源。
然后调用
static void exit_notify(void)
{
/* Let father know we died
*
* Thread signals are configurable, but you aren't going to use
* that to send signals to arbitary processes.
* That stops right now.
*
* If the parent exec id doesn't match the exec id we saved
* when we started then we know the parent has changed security
* domain.
*
* If our self_exec id doesn't match our parent_exec_id then
* we have changed execution domain as these two values started
* the same after a fork.
*
*/

if(current->exit_signal != SIGCHLD &&
   ( current->parent_exec_id != t->self_exec_id  ||
     current->self_exec_id != current->parent_exec_id)
   && !capable(CAP_KILL))
current->exit_signal = SIGCHLD;


/*
* This loop does two things:
*
  * A.  Make init inherit all the child processes
* B.  Check to see if any process groups have become orphaned
* as a result of our exiting, and if they have any stopped
* jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
*/


}

通知父进程和子进程,让init接管所有的子进程。然后进程进入ZOMBIE状态。

进程在ZOMBIE状态时,内核仍然保存一些它的信息以便父进程会需要它 - 比如,父进程可能需要检查子进程的退出状态。这也是ZOMBIE态存在的原因。为了得到这些信息,父进程调用
asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
{
case TASK_ZOMBIE:
release_task(p);
goto end_wait4;
}
并释放剩下的进程资源。


当子进程在其父进程之前exit,而父进程又没有使用wait,子进程进入ZOMBIE态,并且会一直保持除非系统重启。这样子进程始终占着资源,并且减少了系统的最大进程数。

如果一个进程要fork一个子进程,但不要求它等待子进程终止,也不希望子进程处于僵死状态直到父进程终止
解决办法有两个:
一些UNIX使用了新的flag来指明进程不等待其子进程结束。当子进程结束时不创建ZOMBIE态。
还有就时调用fork()两次。 

Pthread 06/12/31
posted @ 2013-05-25 20:24  tangr206  阅读(384)  评论(0编辑  收藏  举报