利用sigaction回收多个子进程

SIGCHLD触发sigaction,进入回调函数,回收子进程

#include <stdio.h>
#include <ctype.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <assert.h>
#include <sys/time.h>

void sig_catch(int signo){
	pid_t wid;
	int stat;
	while((wid = waitpid(-1, &stat, 0)) != -1){//循环回收,防止僵尸进程
		if(WIFEXITED(stat)){
			printf("wait : %d child\n", wid);
		}
	}
}

int main(int argc, char *argv[])
{
	struct sigaction act;
	act.sa_handler = sig_catch;  //回调函数
	sigemptyset(&(act.sa_mask)); //回调时的屏蔽字
	act.sa_flags = 0;            //默认屏蔽触发act的信号
	
	sigaction(SIGCHLD, &act, NULL);
	
	int i;
	for(i = 0; i < 10; ++i){
		if(fork() == 0){
			break;
		}
	}
	if(i == 10){
		sleep(1);
		printf("Daddy done\n");
	} else{
		printf("%d child done\n", getpid());
	}
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值