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

This works fine when run interactively but hangs with a bunch of defunct children when the same user schedules an AT job to run the same script. Has anyone encountered this issue with fork() on RHEL AS 4?
#!/usr/bin/perl ….. my @children = (); …… sub mysub { my $pid = fork(); if ($pid) { push(@children, $pid); } if( $pid == 0 ){ # child process code here exit 0; } } …… foreach (@children) { waitpid($_, 0); }

Replies are listed 'Best First'.
Re: fork() and wiatpid() quirk on RHEL AS 4 and PERL version 5.8.5
by jeffa (Bishop) on Jan 15, 2009 at 18:41 UTC

    How much time passes between the moment one of your child processes exits and when it is reaped? If they are not reaped in a timely manner, they will fill up the process table with defunct zombies. Have you tried this instead:

    $SIG{CHLD} = sub { wait() }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Or even:
      $SIG{CHLD} = 'IGNORE'; # Children are automatically reaped.
        Thanks for your advice. I will test this and report the result.
        Your advice was spot on and resolved the issue. Thank you!