Update In the code below the main difference with respect to johngg's version is the use of the straight pipe function instead of IO::Pipe. It turned out (thanks to johngg's tests) that using straight pipes allowed him to trap the PIPE signal, which he couldnt' using the module. I tried to peek into the module and found nothing about SIGPIPE trapping, but I didn't dig this up to the modules included by IO::Pipe.

Linux box with a slightly modified version of your script, and the parent signalled from another shell:

#!/usr/bin/perl use strict; use warnings; my $whoami; sub handler { print {*STDERR} "$whoami: handler [@_]\n"; exit 0; } $SIG{INT} = \&handler; $SIG{QUIT} = \&handler; $SIG{TERM} = \&handler; $SIG{PIPE} = \&handler; END { print {*STDERR} "$whoami in END block\n"; } print {*STDERR} qq{Parent PID $$\n}; pipe my $r, my $w; my $pid; if ($pid = fork) { $whoami = 'parent'; close $w; print {*STDERR} qq{Kid is PID $pid\n}; while (<$r>) { print {*STDERR} "parent received: $_"; } print {*STDERR} "parent exiting normally\n"; exit 0; } ## end if ($pid = fork) elsif (defined($pid)) { select $w; $|++; $whoami = 'kid'; close $r; print {$w} qq{Kid: Snoozing\n}; for my $heartbeat (1 .. 30) { print {$w} qq{Kid: Heartbeat $heartbeat\n}; sleep 1; } print {$w} qq{Kid: Waking\n}; print {*STDERR} qq{Kid: Quitting\n}; exit; } ## end elsif (defined($pid)) __END__ poletti@PolettiX:~/sviluppo/perl$ perl johngg.pl Parent PID 5336 Kid is PID 5337 parent received: Kid: Snoozing parent received: Kid: Heartbeat 1 parent received: Kid: Heartbeat 2 parent received: Kid: Heartbeat 3 parent received: Kid: Heartbeat 4 parent received: Kid: Heartbeat 5 parent: handler [TERM] parent in END block poletti@PolettiX:~/sviluppo/perl$ kid: handler [PIPE] kid in END block

Note that after the parent exits the shell regains control, hence the prompt. Within a second, the kid is trying to write to the pipe and gets signalled. Also note the following run (stopped with CTRL-C):

poletti@PolettiX:~/sviluppo/perl$ perl johngg.pl Parent PID 5348 Kid is PID 5349 parent received: Kid: Snoozing parent received: Kid: Heartbeat 1 parent received: Kid: Heartbeat 2 parent received: Kid: Heartbeat 3 parent: handler [INT] parent in END block kid: handler [INT] kid in END block

Both processes get the INT signal: probably the shell sends the signal to the whole process group.

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Re^5: Child process dies by polettix
in thread Child process dies by dejans

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.