Chinaunix

标题: 信号SIGCHLD为什么没有被捕获 [打印本页]

作者: libing741799085    时间: 2012-05-31 08:53
标题: 信号SIGCHLD为什么没有被捕获
#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
作者: dhevil_zhi    时间: 2012-05-31 13:17
有没有可能是 子进程结束时,父进程还没有注册SIGCHLD信号的处理函数,因此就走的默认动作(默认忽略SIGCHLD???)
作者: fdl19881    时间: 2012-05-31 13:20
信号只发送一次,, 从你的输出可看出,fork后子进程先执行了。而此时你的sighandl还没设置好时,子进程退出了!!
SIGCHLD信号已经被默认忽略了。


sigset_t newmask;
      struct sigaction newact,oldact;
      newact.sa_handler=sig_child;
      sigemptyset(&newact.sa_mask);
      newact.sa_flags=0;
      sigaction(SIGCHLD,&newact,&oldact);
放到fork前就行了。
作者: libing741799085    时间: 2012-05-31 14:32
回复 3# fdl19881


    确实是这样,非常感谢!
作者: libing741799085    时间: 2012-05-31 14:33
回复 2# dhevil_zhi


    对,就是这样的
作者: dhevil_zhi    时间: 2012-06-04 10:25
子进程退出最好使用_exit()函数.
我遇到过一个问题就是子进程使用exit()函数退出,不知道为什么就是没退出.
我怀疑是子进程退出的时候释放什么资源的时候被阻塞了.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2