1

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.

Community
  • 1
  • 1
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • 2
    If you read the note on wait(2), you'll see that explicitly setting SIG_IGN causes the immediate reaping of children. However, the default action, which is also "ignore" does not do this. So to get zombies, all you have to do is **not** set a signal action for CIGCHLD or, AFAIK, set it to SIG_DFL. – DoxyLover Apr 14 '14 at 16:26
  • How to not set a signal action for SIGCHLD or AFAIK? set it to SIG_DFL? Can you please provide sample implementation link? – kapilddit Apr 14 '14 at 17:06
  • 1
    Simply **don't** set a handler for SIGCHLD! In the code you show, never call set_handler(SIGCHLD). I can tell you from personal experience that if you don't monkey with SIGCHLD, you'll get zombies. – DoxyLover Apr 14 '14 at 18:59

1 Answers1

0

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

  • This is wrong. Explicitly ignoring SIGCHLD with the call signal(SIGCHLD,SIG_IGN) will make terminated processes to be automatically removed from the process table (at least on kernel 2.6). Please see the Notes section of the man waitpid. If you want them to stay in the table, just keep the default signal handler. – Alex Che Feb 08 '17 at 08:39