in reply to Re^2: Fork Causing <defunct>
in thread Fork Causing <defunct>

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

Replies are listed 'Best First'.
Re^4: Fork Causing <defunct>
by owen (Initiate) on Mar 18, 2010 at 17:25 UTC
    To have the child waiting on the parent looks like this:
    }elsif($pid == 0){ # This is the child, it will log when the parent is done + + while(!&parentIsDead){sleep(10);} # &finalLog($traceFileToLog); + + &finalLog($traceFileToLog, $initialString); exit(0); sub parentIsDead { if(getppid()==1){ return 1; } return 0; }

      I think there's some confusion as to what "wait" is referring to. You seem to be referring to the normal meaning of the English word, while the others are talking about the Unix system call of the same name...  Only the latter will prevent "<defunct>" processes.