Two main problems that I see:
1) A faulty assumption that the process exit code covers the 'death by signal' case.
2) Missing parentheses on the exists statements, giving the wrong precedence.

Here is code which covers all cases hopefully:

#!/usr/bin/perl use strict; use warnings; use diagnostics; use POSIX ":sys_wait_h"; use Data::Dumper; my %child_status; sub reaper { my $child; while (($child=waitpid(-1,WNOHANG))>0) { # See waitpid(2) and POSIX(3perl) my $status = $?; my $wifexited = WIFEXITED($status); my $wexitstatus = $wifexited ? WEXITSTATUS($status) : undef; my $wifsignaled = WIFSIGNALED($status); my $wtermsig = $wifsignaled ? WTERMSIG($status) : undef; my $wifstopped = WIFSTOPPED($status); my $wstopsig = $wifstopped ? WSTOPSIG($status) : undef; $child_status{$child} = { status => $status, wifexited => $wifexited, wexitstatus => $wexitstatus, wifsignaled => $wifsignaled, wtermsig => $wtermsig, wifstopped => $wifstopped, wstopsig => $wstopsig, }; print STDERR "reaper: reaped child=$child" ." status=$status" ." wifexited=$wifexited" ." wexitstatus=".(defined($wexitstatus) ? $wexitstatus : " +undef") ." wifsignaled=$wifsignaled" ." wtermsig=".(defined($wtermsig) ? $wtermsig : "undef") ." wifstopped=$wifstopped" ." wstopsig=".(defined($wstopsig) ? $wstopsig : "undef") ."\n"; } } sub copy { my $file = shift; my $dir = shift; local $SIG{CHLD} = \&reaper; my $reader_pid = open( my $reader, '-|' ); if ($reader_pid) { print STDERR "copy: spawned reader child=\"$reader_pid\"\n"; } else { exec '/bin/cat', $file; exit; } my $writer_pid; if ($writer_pid = fork()) { print STDERR "copy: spawned writer child=\"$writer_pid\"\n"; } else { chdir($dir); open( STDIN, "<&=" . fileno($reader) ); exec '/bin/tar', '-x', '-p', '-f', '-'; exit; } while (1) { sleep 1; next if !%child_status; foreach my $pid ($reader_pid, $writer_pid) { if (exists $child_status{$pid}) { my $st = $child_status{$pid}; # check for non-zero exit status # check for death by signal if (($st->{wifexited} && $st->{wexitstatus} != 0) || $st->{wifsignaled}) { print Dumper \%child_status; die "failed ".($pid == $writer_pid ? "tar" : "rea +der"); } } } if (exists($child_status{$writer_pid}) && exists($child_status +{$reader_pid})) { last; } } } copy("/home/tmp/quotes.tar","."); exit 0;


In reply to Re: Detecting when a child process is killed unexpectedly by gmargo
in thread Detecting when a child process is killed unexpectedly by rlb3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.