- 论坛徽章:
- 0
|
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<sys/wait.h>
void sig_child(int signo)
{
int status,res;
if((waitpid(-1,&status,WNOHANG))<0)
{
perror("wait");
exit(1);
}
else
{
printf("process exit status %d\n",WEXITSTATUS(status));
}
// printf("child has finished\n");
}
int main()
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("fork");
exit(1);
}
else if(pid==0)
{
int i;
for(i=0;i<5;i++)
printf("child:%d\n",getpid());
// sleep(1);
// printf("\n");
exit(2);
}
else
{
int i;
sigset_t newmask;
struct sigaction newact,oldact;
newact.sa_handler=sig_child;
sigemptyset(&newact.sa_mask);
newact.sa_flags=0;
sigaction(SIGCHLD,&newact,&oldact);
for(i=0;i<6;i++)
{
printf("parent:%d\n",getpid());
sleep(1);
}
sigaction(SIGCHLD,&oldact,NULL);
exit(0);
}
}
题目如上:运行结果:
[root@localhost 33]# ./a.out
child:10795
child:10795
child:10795
child:10795
child:10795
parent:10794
parent:10794
parent:10794
parent:10794
parent:10794
parent:10794
明明有SIGCHLD的信号处理为什么还是没有捕获该信号?而且我只fork出一个子进程,为什么还会丢失呢?
如果子进程中有sleep(),则结果如下:
[root@localhost 33]# ./a.out
child:9204
child:9204
child:9204
child:9204
child:9204
parent:9203
parent:9203
process exit status 2
parent:9203
parent:9203
parent:9203
parent:9203
结果是能捕获SIGCHLD?
到底什么情况下能捕获SIGCHLD |
|