the*_*ejh 13 c posix zombie-process
我正在编写一个服务器,用于fork()为客户端连接生成处理程序.服务器不需要知道分叉进程会发生什么 - 它们自己工作,当它们完成时,它们应该只是死而不是变成僵尸.有什么简单的方法可以实现这一目标?
the*_*ejh 11
有几种方法,但使用sigaction与SA_NOCLDWAIT父进程可能是一个最简单的:
struct sigaction sigchld_action = {
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
};
sigaction(SIGCHLD, &sigchld_action, NULL);
Run Code Online (Sandbox Code Playgroud)
使用双叉.让您的孩子立即分叉另一个副本并让原始子进程退出.
http://thinkiii.blogspot.com/2009/12/double-fork-to-avoid-zombie-process.html
在我看来,这比使用信号更简单,更容易理解.
void safe_fork()
{
pid_t pid;
if (!pid=fork()) {
if (!fork()) {
/* this is the child that keeps going */
do_something(); /* or exec */
} else {
/* the first child process exits */
exit(0);
}
} else {
/* this is the original process */
/* wait for the first child to exit which it will immediately */
waitpid(pid);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20287 次 |
| 最近记录: |