【发布时间】:2014-09-23 13:08:00
【问题描述】:
我遇到了如下代码的问题:
void sighandler(int signo)
{
printf("sighandler() called\n");
pid_t pid;
pid = waitpid(-1, NULL, WNOHANG);
if(pid >= 0)
{
printf("Caught by sighandler(): pid = %d\n", pid);
}
else
{
perror("sighandler pid failed");
}
}
int main(void)
{
int ret = 0;
pid_t pid;
signal(SIGCHLD, sighandler);
ret = system("ls -al");
if(ret < 0)
{
perror("system failed");
printf("return value is %d\n", ret);
}
return 0;
}
在Linux(Cent OS)环境下,
sighandler()会在system()完成后触发。但是在 Mac OS X 上,sighandler()不会在相同的条件下被调用。这是 Linux 和 BSD/UNIX 系统之间已知的区别吗?真正的问题是,在 GTK(C 语言)程序中,
SIGCHLD与主例程中的sighandler()绑定。但是后来我发现在子窗口GTK Button的回调函数中调用的system()总是返回-1。我完全确定SIGCHLD没有与SIG_IGN绑定,它仍然绑定sighandler()。sighandler()中的waitpid是否有可能在system()中的waitpid()之前捕获死子进程,这是为了处理死子进程?
【问题讨论】:
-
叹气,这真的很难用谷歌搜索,因为“系统”匹配“操作系统”和“系统调用”,而这些经常与“SIGCHLD”在同一页面上找到
-
@Barmar 你是绝对正确的。即使我可以通过在调用它之前将 SIGCHLD 与 SIG_DFL 绑定来使 system() 正常工作,我也花了几个小时在它上面。但这真的让我很困惑,因为我无法通过自己编写程序来重现问题:每次死进程都由 system() 本身处理并且根本没有任何问题。