gpt4 book ai didi

使用 sigaction 捕获 SIGCHLD

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:52 27 4
gpt4 key购买 nike

我正在阅读一篇关于信号的文章,我不确定这部分代码在做什么。有人可以为我解释一下吗?这篇文章是关于创建 TCP un-blocked 服务器。

struct sigaction sigchldAction;
sigchldAction.sa_handler = SIG_IGN;
sigchldAction.sa_flags = 0;
sigemptyset(&sigchldAction.sa_mask);
if (sigaction(SIGCHLD, &sigchldAction, NULL) == -1) callError("sigaction()");

最佳答案

根据 sigaction 的 Linux 手册页:

POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)).

也就是说,使用 SIG_IGN 显式忽略 SIGCHLD 会导致操作系统在终止时自动销毁子进程,这样您就不必 wait* 等待他们。这通常是您想要的,尽管有时并非如此,因为它还阻止父进程使用wait* 来确定子进程的退出状态。

关于使用 sigaction 捕获 SIGCHLD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com