|
楼主 |
发表于 2011-6-27 09:13:26
|
显示全部楼层
问题解决了,在网上找到的如下:
原因好像是因为SIGCHLD的默认动作好像不是IGNORE。我写了个测试代码(在最后,a.c)。
gcc a.c
执行a.out DFL正常,执行a.out IGN就会返回-1,错误码解释为“No child processes”。
分别执行 strace -o DFL.log a.out DFL 和 strace -o IGN.log a.out IGN
比较两个文件,
前者为 rt_sigaction(SIGCHLD, {SIG_DFL}, {SIG_DFL}, 8) = 0
后者为 rt_sigaction(SIGCHLD, {SIG_IGN}, {SIG_DFL}, 8) = 0
这似乎是唯一区别。似乎能解释返回-1的原因。
但记得SIGCHLD的默认动作好像就是忽略。
并且,看不懂问题代码的那段。
rt_sigaction(SIGINT, {SIG_IGN}, {0xd92508, ~[ILL ABRT BUS FPE KILL SEGV STOP XCPU XFSZ SYS RTMIN RT_1], SA_RESTART|SA_SIGINFO}, 8) = 0
rt_sigaction(SIGQUIT, {SIG_IGN}, {SIG_IGN}, 8) = 0
/* ************************************************ */
/* test code a.c */
/* ************************************************ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
int main(int argc, char * argv[])
{
static struct sigaction act;
static struct sigaction old_act;
char command[60];
int ret = -1;
int i = 0;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGCHLD);
act.sa_flags = SA_RESTART;
if (argc == 2)
{
if (!memcmp(argv[1], "IGN", 3))
{
act.sa_handler = SIG_IGN;
}
else if (!memcmp(argv[1], "DFL", 3))
{
act.sa_handler = SIG_DFL;
}
else
{
printf("useage: %s IGN DFL \n", argv[0]);
return -1;
}
}
else
{
printf("useage: %s IGN | DFL \n", argv[0]);
return -1;
}
if (sigaction(SIGCHLD, &act, &old_act) == -1)
{
perror("sigaction");
return -1;
}
ret = sigismember(&act.sa_mask, SIGCHLD);
printf("ret = %d\n",ret);
memset(command, 0x00, sizeof(command));
strcpy(command, "ls -al");
ret = system(command);
printf("ret = %d\n",ret);
if (ret)
{
perror("system");
return -1;
}
return 0;
}
/* ************************************************ */ |
|