I need your esteemed help on some wierd behaviour of alarms in ActivePerl. I am using v5.12.2 on Windows 2008 SP1 32bit.

I want to read from a long-running co-process until I am tired, then kill the co-process. I boiled down my code to the following snippet, replacing the real co-process with a dummy command:

#! /usr/bin/perl -w use strict; # Starting a co-process to read from. my $procid = open(READ, '-|', 'perl -e "$|=1; for ($i=0;$i<10;$i++) {p +rint \"Line $i\n\"; sleep 1;}"'); my $timeout = 0; $SIG{ALRM} = sub { $timeout = 1; }; alarm(3); # Reading from co-process. while (!$timeout) { my $line = <READ>; last unless defined $line; print $line; # for (my $i=0;$i<5000;$i++) {} } kill('INT', $procid) if $timeout;

I could not make the alarm work. I even tried unsafe signals. However, putting some tedious calculations somewhere inside the loop, will strangely solve the problem. Must this be considered as a bug?

I also experimented with threads and semaphores and finally found a work-around. But that would be an awful bunch of code for something quite obvious. Is there a state-of-the-art solution to this?

----------

Thank you, Monks, for all your helpful answers. I will have to study and try for a while to understand all the implications of your proposals. The next thing, I will probably do, is using Win32::Job and/or Win32::Process. These sound quite reasonable and easy to interface, although, alas, they will impair portability of my code.


In reply to Alarms with ActivePerl do not work properly by heiermann

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.