Actually, your code does have a bug. I think you wanted to do something along the lines of

eval{ $SIG{ALRM} = sub {die "DING-DING"}; alarm(10); sleep(20); # <-- This line is missing print "I am frustrated."; alarm(0); };
Otherwise, you're telling Perl to schedule an alarm() for ten seconds later, print the message and the cancel the alarm. Unless your machine is exceptionally slow, this would not work on *nix either because the print should take much less than 10 seconds, so the alarm gets cancelled.

As a side note, it is a very good idea to try to keep your signal handlers small and simple. Perl's instructions are quite complex in terms of the processing involved.

Signals (the result of an alarm, for example) can arrive at any point in time. In particular, they can arrive just in the middle of adding an entry to a hash. This has the potential to call your signal handler with a corrupt state in part of your program's data.

With recent versions of Perl, this is less of an issue but I thought in throwing this piece of advice just in case.

An alternative you can use, is what is sometimes called a post-file. Your expensive function can check periodically wether this file exists. If it does, it can abort itself gracefully. Just remember to remove the file when your program starts. The code to do this would be something along the lines of

sub long_running_function { ... while ($your_condition) { ... return undef if -f $post_file; # Or die() if you want ... } } unlink $post_file; # May fail silently, but who cares? ... &long_running_function(); ...
You can refine this technique and create the file when you enter the long_running_function() and then check its age periodically until it reaches a threshold, at which you bail out.

Regards.


In reply to Re: Problem with alarm() by fokat
in thread Problem with alarm() by Chris_Hayes

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.