Don't want to remove terminated child process immediately, need to become zombie

无人久伴 提交于 2019-12-11 10:04: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:


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() .



来源:https://stackoverflow.com/questions/23064988/dont-want-to-remove-terminated-child-process-immediately-need-to-become-zombie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!