本文介绍了如何处理SIGCHLD用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要妥善处理SIGCHLD。我如何使用它与我现有的code。此刻我不能等待子进程,除非我用0代替 WNOHANG的| WUNTRACED

i need to handle sigchld properly. How can I use it with my existing code. at the moment i cant wait for the child process unless i use 0 instead of WNOHANG|WUNTRACED.

status = 0;
        pid_t child, endID;
        if(amp == 1)
                signal( SIGCHLD, SIG_IGN );

        child = fork();

        if (child  <  0) {
                perror("fork() error\n");
                exit(EXIT_FAILURE);

        } else if (child == 0) {
                // do sth here
                perror("error\n");

        } else {
                //sleep(1)

如果我删除的睡眠,然后再执行父1。为什么?

if i remove sleep then parent is executed 1st.. why?

推荐答案

下面是一个开始(但低于读​​):

Here is a start (but read below):

static void
child_handler(int sig)
{
    pid_t pid;
    int status;

    /* EEEEXTEERMINAAATE! */
    while((pid = waitpid(-1, &status, WNOHANG)) > 0)
        ;
}

/* Establish handler. */
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;

sigaction(SIGCHLD, &sa, NULL);


当然,这是所有的无谓。如果家长只是忽略 SIGCHLD ,孩子们都默默地收获并不会变成僵尸。


Of course, this is all pointless. If the parent simply ignores SIGCHLD, the children are silently reaped and won't turn into zombies.

引用TLPI:

设置明确SIGCHLD的配置为SIG_IGN导致任何
  子进程结束之后将立即删除
  从系统的而不是被转换成一个僵尸

所以,这样的事情应该为你做的伎俩:

So something like this should do the trick for you:

signal(SIGCHLD, SIG_IGN); /* Silently (and portably) reap children. */

这篇关于如何处理SIGCHLD用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 23:48