in reply to Perl IPC: Checking Exit Status of Child Process

You could waitpid() in parent and then check the return value stored in $?
my $pid = fork(); die "Can't fork() : $!\n" unless defined $pid; if ($pid){ if (waitpid($pid, 0) > 0); my ($rc, $sig, $core) = ($? >> 8, $? & 127, $? & 128); if ($core){ print "$pid dumped core\n"; }elsif($sig == 9){ print "$pid was murdered!\n"; }else{ print "$pid returned $rc"; print ($sig?" after receiving signal $sig":"\n"); } }else{ print "$pid... um... disappeared...\n"; } }else{ unless (run_code()){ exit 1; } exit 0; }
--perlplexer

Replies are listed 'Best First'.
Re^2: Perl IPC: Checking Exit Status of Child Process
by FreeBeerReekingMonk (Deacon) on Aug 11, 2016 at 14:31 UTC
    Sorry for necroposting, just pointing out that if (waitpid($pid, 0) > 0); should be: if (waitpid($pid, 0) > 0){

    Here is the same code, with an additional timeout option from the parent:

    #!/usr/bin/perl use strict; use warnings; my $timeout = 10; my $parent_pid = $$; my $child_pid = fork(); die "Can't fork() : $!\n" unless defined $child_pid; if ($child_pid){ # parent eval { local $SIG{ALRM} = sub { die "timed-out\n" }; alarm $timeout; if (waitpid($child_pid, 0) > 0){ my ($rc, $sig, $core) = ($? >> 8, $? & 127, $? & 128); if ($core){ print "$child_pid dumped core\n"; }elsif($sig == 9){ print "$child_pid was murdered!\n"; }else{ print "$child_pid returned $rc"; print ($sig?" after receiving signal $sig":"\n"); } }else{ print "$child_pid... um... disappeared...\n"; } alarm 0; }; if($@ eq "timed-out\n"){ print "$child_pid... parent got bored waiting for it...\n"; }else{ print "$child_pid ran SUCCESSFULLY!!!\n"; }; }else{ # child unless (run_code()){ exit 1; } exit 0; }
Re: Re: Perl IPC: Checking Exit Status of Child Process
by bayruds (Acolyte) on May 19, 2003 at 00:22 UTC

    Thanks for replying back. I had a question though:

    How do I create a fan of n children out of the same parent.

    That is, 1 parent with n children.

    Thanks a lot for replying back.
      Just call fork() as many times as you need...
      # Create 10 child processes for (1..10){ my $pid = fork(); last unless defined $pid; # Too many processes already? unless($pid){ # Child code here exit; } } # wait() for kids while(($pid = wait()) > 0){ # Check $? here }
      --perlplexer
        Hi
        Thank you very much for replying back.

        When I fork in a loop, how do I ensure that all the child

        processes are executing concurrently. My main aim to use

        fork is to execute different subroutines in a cincurrent

        fashion. Many thanks for your help.
      Here's an example of one parent forking multiple children.

      The Perl Cookbook has great examples about this topic.

      You'd really want to make sure you're looping around the fork() and keeping track of the number of living children you have at the moment. Other than that and IPC, it's mostly just everyday programming.

      Christopher E. Stith
      use coffee;

        Hi
        Thank you very much for replying back. I had a question
        regarding multiple forks in a loop:
        1. How do I ensure that all child processes are running conurrently
        2. Also, How can I execute different subroutines for each child
        Looking forward to hear from you.
        Thanks