Don't want to remove terminated child process

2019-08-02 16:18发布

I got below information from SE QUE Explicitly setting the disposition of SIGCHLD to SIG_IGN causes any child process that subsequently terminates to be immediately removed from the system instead of being converted into a zombie.

As far I know, to read the child status it is required to have child pid in process table. So it is necessary to have zombie child process in process table to read the child status.

So I want to write signal handler which will remove "setting the disposition of SIGCHLD to SIG_IGN"

I used below code (before fork) to avoid removal of child process immediately after termination: but still I am not able to get child status and waitpid returns -1 with ECHILD.

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static siginfo_t sig_info;
static volatile sig_atomic_t sig_num;
static void *sig_ctxt;

static void catcher(int signum, siginfo_t *info, void *vp)
{
    sig_num = signum;
    sig_info = *info;
    sig_ctxt = vp;
}

static void set_handler(int signum)
{
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = catcher;
    sigemptyset(&sa.sa_mask);

    if (sigaction(signum, &sa, 0) != 0)
    {
        int errnum = errno;
        fprintf(stderr, "Failed to set signal handler (%d: %s)\n", errnum, strerror(errnum));
        exit(1);
    }
}

static void prt_interrupt(FILE *fp)
{
    if (sig_num != 0)
    {
        fprintf(fp, "Signal %d from PID %d\n", sig_info.si_signo, (int)sig_info.si_pid);
        sig_num = 0;
    }
}

Please suggest some idea, I am blocked with this.

1条回答
Fickle 薄情
2楼-- · 2019-08-02 16:43

Instead of catching the signal ,just use this code in main :

signal(SIGCHLD,SIG_IGN);

Instead of using the handler, use SIG_IGN (as shown above) . This ignores the signal and process pid would remain in the process table .Parent can then claim the status of this zombie process using waitpid() or wait() .

查看更多
登录 后发表回答