处理SIGCHLD,如何记录孩子死亡时的返回值
void childSignalHandler(int signo) {
int status;
pid_t pid = wait(&status);
struct PIDList* record = getRecordForPID(childlist, pid);
if (record != NULL)
record->returnValue = status;
}
简单的问题:
我希望这个处理程序在一个孩子死亡时(这个应用程序产生很多孩子)获取他们的返回值并记录它(最后三行)。这能做到吗,还是我把所有这些 API 业务搞错了?
谢谢您的宝贵时间!
(另外,linux API 术语非常令人毛骨悚然,检查一下垂死的孩子等等)
void childSignalHandler(int signo) {
int status;
pid_t pid = wait(&status);
struct PIDList* record = getRecordForPID(childlist, pid);
if (record != NULL)
record->returnValue = status;
}
Quick question:
I want this handler to, when a child dies (this app spawns lots of children), get their return value and record it (last three lines). Will this do it, or am I getting all this API business wrong?
Thank you for your time!
(also, linux API terminology is creepy as hell, check for dying children and whatnot)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将函数设置为
SIGCHLD
的处理程序,这应该可以完成工作。但是,
SIGCHLD
不仅可以在子进程退出后发送给父进程。其他一些事件也以这种方式发出信号(例如当孩子停止时)。请参阅 man wait(3) 了解详细说明。This should do the job, if you set your function as a handler for
SIGCHLD
.However,
SIGCHLD
can be send to parent process not only after the child exited. Some other events are signaled this way as well (for instance when the child is stopped). See man wait(3) for detailed explanation.注意信号不排队。如果两个孩子连续快速死亡,您可能只会收到一个
SIGCHLD
。因此,您实际上应该循环调用waitpid()
直到没有更多退出进程需要处理:Note signals are not queued. If two children die in quick succession, you may only recieve one
SIGCHLD
. So, you should actually loop around callingwaitpid()
until there are no more exiting processes to handle: