How's that supposed to work? If you exec another program, the running perl is terminated so can't send the alarm.

So I think that a SIGALRM is delivered to the process started via exec(). Unless the process changes its signal handler for SIGALRM, that signal will kill the process.

Let's test that:

#!/usr/bin/perl use strict; use warnings; sub helper { # forked process, wastes 10 seconds for (1..10) { print "helper: start of second $_\n"; select(undef,undef,undef,1); # poor man's sleep, witho +ut messing with alarm print "helper: end of second $_\n"; } } sub main { # main process print "Helper will die in 5 seconds\n"; alarm(5); # kill me in five seconds ... exec($^X,$0,"dummy argument") # start perl with this script an +d a parameter or die "Could not start helper: $!"; } if (@ARGV) { helper(); } else { main(); }

Output:

>perl alarmtest.pl Helper will die in 5 seconds helper: start of second 1 helper: end of second 1 helper: start of second 2 helper: end of second 2 helper: start of second 3 helper: end of second 3 helper: start of second 4 helper: end of second 4 helper: start of second 5 Alarm clock >

Just for fun, let's add a signal handler for SIGALRM in the helper process:

sub helper { $SIG{'ALRM'}=sub { print "I am immortal, you fool!\n" }; # forked process, wastes 10 seconds for (1..10) { print "helper: start of second $_\n"; select(undef,undef,undef,1); # poor man's sleep, witho +ut messing with alarm print "helper: end of second $_\n"; } }

Output:

>perl alarmtest.pl Helper will die in 5 seconds helper: start of second 1 helper: end of second 1 helper: start of second 2 helper: end of second 2 helper: start of second 3 helper: end of second 3 helper: start of second 4 helper: end of second 4 helper: start of second 5 I am immortal, you fool! helper: end of second 5 helper: start of second 6 helper: end of second 6 helper: start of second 7 helper: end of second 7 helper: start of second 8 helper: end of second 8 helper: start of second 9 helper: end of second 9 helper: start of second 10 helper: end of second 10 >

Alexander

Updates:

  1. changed links from [man://...] (FreeBSD) to http://linux.die.net/... (Linux)
  2. added second example with non-default signal handler
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: Parallel::ForkManager and wait_all_children by afoken
in thread Parallel::ForkManager and wait_all_children by rgren925

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.