signaton 捕获两个子进程信号后只执行了一次为何处理函数只执行一次

cuishaosha 2016-07-09 07:56:59
/* multi_proc.c文件 */
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>


void delsignal()
{
int status=-1;
pid_t child;
printf("ssssss");
while((child=waitpid(-1,&status,WNOHANG))!=-1)
{
if(child==0)
{
printf("I am father progress.no exited!\n");
break;
}
printf("I am father progress.pid =%d exited!\n",child);
}


}


int main()
{
struct sigaction action,oldaction ;
pid_t child1,child2,child;
printf("I am father progress\n");

action.sa_flags = 0;
action.sa_handler = delsignal;
sigemptyset(&action.sa_mask);
sigfillset (&action.sa_mask);
sigdelset (&action.sa_mask, SIGCHLD);
if(sigaction(SIGCHLD, &action, &oldaction) < 0)
{
printf("Set signal handler error");
exit(-1);
}
/*先创建子进程1*/
child1=fork();
/*子进程1的出错处理*/
if(child1==-1)
{
printf("Child1 fork error\n");
exit(1); /*异常退出*/
}
/*在子进程1中调用execlp()函数*/
else if(child1==0)
{
printf("I am child1 and I execute 'ls -l'\n");
sleep(5);
if(execlp("ls","ls","-l",NULL)<0)
{
printf("Child1 execlp error\n");
}
exit(0);
}
/*在父进程中再创建进程2,然后等待两个子进程的退出*/
else
{


child2=fork();
/*子进程2的出错处理*/
if(child2==-1)
{
printf("Child2 fork error\n");
exit(1);
}
/*在子进程2中使其暂停5s*/
else if(child2==0)
{
printf("I am child2.I will sleep for 5 seconds!\n");
sleep(2);
printf("I am child2.I have awaked and I will exit!\n");
exit(0);
}
sleep(10);


}
}

为何delsignal 函数只执行了一次
...全文
309 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

18,773

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧