owen has asked for the wisdom of the Perl Monks concerning the following question:

Hello So I'm on a solaris box and am writing a program that uses fork and is causing defunct processes to spawn. The program has to spawn a SAS process which does funny things to the terminal and so SAS is initiated in the parent process as opposed to the child ( which I think is what most people would expect. ) Anyways the child waits for the parent process to end and then does some logging of its own, while it's waiting it is just sleep(10). When the whole process, both child & parent, are finished the defunct process goes away. I know having the parent process wait for the child is a way to prevent defunct processes, would that make sense in this context however? Is there another way to make the child process non-defunct, at least so that it looks better on the ps - ptree table? Thanks!

Replies are listed 'Best First'.
Re: Fork Causing <defunct>
by ikegami (Patriarch) on Mar 18, 2010 at 16:03 UTC

    You're not reaping your children (using wait, waitpid or $SIG{CHLD} = 'IGNORE';). A process continues to exist after it exits so its parent can be notified of the exit and so the parent can fetch its exit code.

    The program has to spawn a SAS process which does funny things to the terminal and so SAS is initiated in the parent process as opposed to the child

    I don't follow. The parent and child should have equal access to the terminal.

    Anyways the child waits for the parent process to end

    Apparently not, since you said it's defunct.

Re: Fork Causing <defunct>
by almut (Canon) on Mar 18, 2010 at 16:04 UTC
    ...would that make sense in this context however?

    Why not? What's wrong with having the parent wait for its children?

    In case of doubt where/when to wait, set up a $SIG{CHLD} handler that reaps any child as it dies.

      It seems odd as the child is already waiting for the parent process to die. If they both wait for each other, won't that be infinite?

        The child cannot wait for the parent. That is not how it works.

        The child can, however, "divorce" itself from its parent and be adopted by init (process 1), as is stated in another part of this thread.

        If you are getting a defunct process, either you are not waiting for your spawned process, or your program has grandchildren that the child process is not reaping.

        When the process shows up as defunct in the process table, that process has finished, and the operating system is just keeping a data structure around with the exit information so that the parent process can read it if it cares. The parent process must deal with its children, either by waiting for it, informing perl / the os to ignore it (%SIG and perlipc), or exiting your program and letting the OS handle it.

        It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis

        A child can't wait(2) for its parent...

Re: Fork Causing <defunct>
by cdarke (Prior) on Mar 18, 2010 at 16:52 UTC