生成的进程不再存在
我使用 posix_spawnp 从主进程中生成子进程。
int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);
if (iRet != 0)
{
return false;
}
有时,在没有错误地生成子进程后,它会突然失效。 怎么会出现这种情况呢?
我使用信号处理程序来获取子进程:
void SigCatcher(int n)
{
while(waitpid( -1, NULL, WNOHANG ) > 0);
}
每当我杀死一个子进程时,我都会手动调用它。
kill(oProcID, SIGKILL);
signal (SIGCHLD, SigCatcher);
这是否会导致生成的子项失效(无需我调用kill)?
I use posix_spawnp to spawn child processes from my main process.
int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);
if (iRet != 0)
{
return false;
}
Sometimes, after a child process is spawned without errors, it suddenly becomes defunct. How could this occur?
I use a signal handler to reap child processes:
void SigCatcher(int n)
{
while(waitpid( -1, NULL, WNOHANG ) > 0);
}
and I manually call it whenever I kill a child process.
kill(oProcID, SIGKILL);
signal (SIGCHLD, SigCatcher);
Could this cause spawned children to go defunct (without me calling kill)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这:
看起来像一个竞争条件。 您需要在终止子进程之前安装信号处理程序,否则您可能会丢失信号。
This:
looks like a race condition. You need to install the signal handler before killing the child process, otherwise you risk missing the signal.
您是否曾致电:
其他地方?
如果没有,那么您需要在生成任何子进程之前执行此操作,以确保这些子进程在终止时被收获。
正如 Unwind 所指出的,您当前对
kill
和signal
的调用是错误的。典型用途是:
Have you called:
anywhere else?
If you haven't, then you need to do so before any child processes are even spawned to ensure that those children are reaped when they terminate.
As Unwind points out, your current calls to
kill
andsignal
are the wrong way around.Typical use would be: