忽略带有sigaction(2)的SIGCHLD信号的用途是什么?

编程入门 行业动态 更新时间:2024-10-11 17:30:17
本文介绍了忽略带有sigaction(2)的SIGCHLD信号的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

事实证明,通过指定要被其父级使用sigaction()忽略的SIGCHLD信号,我们可以防止出现僵尸进程(即其父级不是wait()的父级). .但是,看起来SIGCHLD还是默认情况下会被忽略.怎么运作的?

It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't wait() for it to _exit()) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD is ignored by default anyway. How come does this work?

int main (void) { struct sigaction sa; sa.sa_handler = SIG_IGN; //handle signal by ignoring sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) == -1) { perror(0); exit(1); } int pid = fork(); if (pid == 0) { //child process _exit(0); } do_something(); //parent process return 0; }

推荐答案

SIGCHLD的默认行为是丢弃信号,但子进程将保持为僵尸状态,直到父进程调用wait()(或变体)为止. )以获取其终止状态.

The default behavior of SIGCHLD is to discard the signal, but the child process is kept as a zombie until the parent calls wait() (or a variant) to get its termination status.

但是,如果您使用处置SIG_IGN显式调用sigaction(),这将导致它不会将孩子变成僵尸-当孩子退出时,它会立即被收割.参见 stackoverflow/a/7171836/1491895

But if you explicitly call sigaction() with the disposition SIG_IGN, that causes it not to turn the child into a zombie -- when the child exits it is reaped immediately. See stackoverflow/a/7171836/1491895

获得此行为的POSIX方法是通过将sigaction与handler = SIG_DFL和flags包含SA_NOCLDWAIT一起调用.从2.6开始,这是在Linux中.

The POSIX way to get this behavior is by calling sigaction with handler = SIG_DFL and flags containing SA_NOCLDWAIT. This is in Linux since 2.6.

更多推荐

忽略带有sigaction(2)的SIGCHLD信号的用途是什么?

本文发布于:2023-10-09 05:29:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号   用途   sigaction   SIGCHLD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!