首先你可以看下《linux內(nèi)核設計與實現(xiàn)》大概前幾章,有一章講這個的。另外,下面
這篇文章說的也很好。 http://hi.baidu.com/kobetec/blog ... dca883e950cdf4.html
怎樣避免產(chǎn)生僵尸進程2008-02-20 17:09怎樣避免產(chǎn)生僵尸進程
摘錄于: <<Advanced Programming in the UNIX® Environment: Second Edition>> By W. Richard Stevens, Stephen
1.什么是僵尸進程?
In UNIX System terminology, a process that has terminated,but whose
parent has not yet waited for it, is called a zombie.
還要自己做。 下面就是Stevens給的采用兩次folk避免僵尸進程的示例.
Example
Recall our discussion in Section 8.5 about zombie processes. If we want to write a process so that it forks a child but we don't want to wait for the child to complete and we don't want the child to become a zombie until we terminate, the trick is to call fork twice. The program in Figure 8.8 does this.
We call sleep in the second child to ensure that the first child terminates before printing the parent process ID. After a fork, either the parent or the child can continue executing; we never know which will resume execution first. If we didn't put the second child to sleep, and if it resumed execution after the fork before its parent, the parent process ID that it printed would be that of its parent, not process ID 1.
#include "apue.h"
#include <sys/wait.h>
int
main(void)
...{
pid_t pid;
if ((pid = fork()) < 0) ...{
err_sys("fork error");
} else if (pid == 0) ...{ /**//* first child */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)
exit(0); /**//* parent from second fork == first child */
/**//*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when