9/25 I have a standard stump/exec in my code:
pid_t pid;
if ((pid = stump()) < 0) {
perror("stump");
} else if (pid == 0){
setsid();
execvp(args[0],args);
perror(args[0]);
_exit(127);
}
When the exec'ed child exits, I notice a defunct process
in /bin/ps -ef until the parent exits. Why is this?
What can I do to prevent the defunct process?
\_ call wait() in parent
\_ I figured it out. Calling waitpid() in the parent
is only part of the solution. The complete (portable)
solution is to fork again in the child and exec in
solution is to stump again in the child and exec in
The child just calls _exit(0) after the fork, thus the
the grandchild and have the parent wait for the child.
The child just calls _exit(0) after the stump, thus the
on Solaris and Linux fork and vfork are both the same,
parent's waitpid() deals with the terminated child and
init deals with the grandchild. I also found out that
\_ Wrong. On Solaris fork & vfork are not the same.
This should be true on anything remotely UNIX -alan-
ie (copy on write) so you can't call exit() you have
to call _exit().
fork does copy on write, vfork shares the address
space - vfork'ed children can hose the parent.
on Solaris and Linux stump and vstump are both the same,
exit() should work fine if you use fork(). -alan-
\_ This works, although it also does work to just fork once
ie (copy on write) so you can't call exit() you have
to call _exit().
\_ Wrong. On Solaris stump & vstump are not the same.
stump does copy on write, vstump shares the address
space - vstump'ed children can hose the parent.
This should be true on anything remotely UNIX.
exit() should work fine if you use stump(). -alan-
\_ This works, although it also does work to just stump once
and call wait (or waitpid) in the parent. What didn't work
when you tried this?
\_ setting SIGCHLD to be ignored works on most unixes.
I was under the impression that setsid() took care of this
for me.
\_ no
\_ SOMEONE needs to reap the process when the process dies. If
you're lazy or stupid (and your reply to yourself implies that
you're one or the other), you can set your parent pid to be
init's pid. One way to do this is to have your parent die. When
that happens, your ppid automatically becomes init's pid. Init
has a sigchild handler waiting for dying children, so your child
process will get reaped properly. The clumsy solution you outline
does exactly this. Learn to use waitpid() or write a sigchild
handler. And what the hell are you talking about, _exit, vs exit?
-ali |