请教signal(SIGCHLD, SIG_IGN)的问题

slmax1 2016-03-02 09:05:10
我查了man手册,说SIGCHLD的默认动作是ign
SIGCHLD 20,17,18 Ign Child stopped or terminated


那为什么还要signal(SIGCHLD, SIG_IGN)
...全文
647 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nswcfd 2016-03-18
  • 打赏
  • 举报
回复
貌似是影响wait的行为,或者说zombie进程的行为。
void test_wait()
{
	printf("parent=%d\n", getpid());
	if (fork() == 0) {
		printf("child1=%d\n", getpid());
		sleep(3);
		printf("exit child1\n");
		exit(1);
	}
	if (fork() == 0) {
		printf("child2=%d\n", getpid());
		sleep(5);
		printf("exit child2\n");
		exit(2);
	}
	while (1) {
		int stat;
		int c = wait(&stat);
		int err = errno;
		if (c < 0) perror("wait");
		printf("child=%d, exit=%x, err=%d\n", c, stat, err);
		if (c < 0)
			break;
	}
}

int main()
{
	//test 1
	test_wait();
	//test 2
	signal(SIGCHLD, SIG_IGN);
	test_wait();
}
//test1 parent=876 child1=877 child2=878 exit child1 <-- 3秒 child=877, exit=100, err=0 <-- 3秒,父进程第一次wait返回 exit child2 <-- 5秒 child=878, exit=200, err=0 <--- 5秒,父进程第二次wait返回 wait: No child processes child=-1, exit=200, err=10 <--- 5秒,父进程第三次wait失败 //test2 parent=876 child1=881 child2=882 exit child1 <-- 3秒 exit child2 <-- 5秒 wait: No child processes child=-1, exit=0, err=10 <--- 5秒,父进程第一次wait失败 POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)), then children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated, and then fail with errno set to ECHILD.
nswcfd 2016-03-02
  • 打赏
  • 举报
回复
参考man waitpid的notes部分
slmax1 2016-03-02
  • 打赏
  • 举报
回复
在我不手动去回收僵尸子进程的时候.
mymtom 2016-03-02
  • 打赏
  • 举报
回复
1. 不要用signal函数,用sigaction, 至少要用sigset 2. 对于 SIGCHLD信号,建议用sigaction或sigset捕捉信号,然后wait
常书 2016-03-02
  • 打赏
  • 举报
回复
印像中,收到signal默认动作会结束程序

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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