关于SIGCHLD和sigaction的疑惑
梁宏 2009-07-22 03:24:57 各位大哥,小弟搞了好久也没整明白,代码如下:
#include "apue.h"
#include<pthread.h>
void show_handler(struct sigaction * act)
{
printf("sigchld catched!\n");
}
main()
{
int i;
pid_t pid;
struct sigaction act,oldact;
act.sa_handler = show_handler;
pid = fork();
if(pid < 0)
printf("fork err!\n");
if(pid == 0)
{
sleep(5);
printf("child process\n");
}
else
sigaction(SIGCHLD,&act,&oldact);
printf("parent process\n");
}
输出:
parent process
[root@localhost threadctl]# child process
parent process
为什么在父进程里面设置了sigaction动作,但却没有执行,并且执行了两次printf("parent process\n");
。
而:
void show_handler(struct sigaction * act)
{
printf("sigchld catched!\n");
}
main()
{
int i;
pid_t pid;
struct sigaction act,oldact;
act.sa_handler = show_handler;
sigaction(SIGCHLD,&act,&oldact);
pid = fork();
if(pid < 0)
printf("fork err!\n");
if(pid == 0)
{
printf("child process\n");
}
else
printf("parent process\n");
}
确实正常的输出:
child process
sigchld catched!
parent process