4

I am not handling SIGCHLD in my code. Still my process is removed immediately after termination. I want it to become zombie process.

If I set SIGCHLD to SIG_DFL then, will it work? How do I set SIGCHLD to SIG_DFL?

I want process to become zombie, so I can read the child status in parent after waitpid.

Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • You should *always* read the child status with `waitpid` notably when you have installed a `SIGCHLD` handler (or without it). Read [signal(7)](http://man7.org/linux/man-pages/man7/signal.7.html), [signal(2)](http://man7.org/linux/man-pages/man2/signal.2.html), [sigaction(2)](http://man7.org/linux/man-pages/man2/sigaction.2.html) for details. – Basile Starynkevitch Apr 15 '14 at 05:30
  • waitpid returns -1 with errno = ECHILD. So child status is not valid and some garbage only. – kapilddit Apr 15 '14 at 05:31

2 Answers2

10

From your question history you seem to be tying yourself in knots over this. Here is the outline on how this works:

  1. The default disposition of SIGCHLD is ignore. In other words, if you do nothing, the signal is ignored but the zombie exists in the process table. This why you can wait on it at any time after the child dies.

  2. If you set up a signal handler then the signal is delivered and you can reap it as appropriate but the (former) child is still a zombie between the time it dies and the time you reap it.

  3. If you manually set SIGCHLD's disposition to SIG_IGN via signal then the semantics are a little different than they are in item 1. When you manually set this disposition the OS immediately removes the child from the process table when it dies and does not create a zombie. Consequently there is no longer any status information to reap and wait will fail with ECHILD. (Linux kernels after 2.6.9 adhere to this behavior.)

Duck
  • 26,924
  • 5
  • 64
  • 92
0

So your final target is to read return code in parent process after your child process exit? I don't see this has any matter with signal. Some example code is:

short pid;
if((pid == fork()) == 0) {
// Child process do some thing here.
exit(n);
} else {
  int returnCode;
  while(pid != wait(&returnCode));
  // the child has terminated with returnCode
  // wait is blocking system call so u don't need to worry about busy waiting.
}
qqibrow
  • 2,942
  • 1
  • 24
  • 40
  • Yes, here I am always getting -1 (return value) from wait. I am expecting to get child pid as return value from wait. So I can proceed with child status. Which is not happening in my case.. When I check errno it is ECHILD. So How to check SIGCHLD flag? – kapilddit Apr 15 '14 at 05:50
  • see [link](http://stackoverflow.com/questions/14266485/understanding-sigchld-when-the-child-process-terminates) bounds the signal with a handler, see what's going on. – qqibrow Apr 15 '14 at 15:58