Alarm has only recently been implemented on Win32, and it still seems to have some limitations.

Here's another way of achieving this that should be portable back to at least 5.6.1.

#! perl -slw use strict; sub systemWithTimeout { my( $timeout, $command ) = @_; my $pid = open my $in, "$command |" or die $!; my $endtime = time() + $timeout; Win32::Sleep 100 while time() < $endtime and kill 0, $pid; Win32::Sleep 0; ## Give the other process a timeslot to go away my $wasKilled = kill 9, $pid if kill 0, $pid; return wantarray ? ( $wasKilled , <$in> ): $wasKilled; } for ( 4 .. 6 ) { print "5 second timeout on a process that runs for $_ seconds"; my( $timeout, @results ) = systemWithTimeout 5, qq[ perl -wle" sleep $_; print $$, ' ', scalar localtime;" ]; printf "The process %s timeout\n", $timeout ? 'did' : 'did not'; print "it returned\n @results" if @results; } print "\nscalar context"; if( systemWithTimeout 5, qq[ perl -wle" sleep 3; print for 1 .. 100" ] + ) { print "command timed out"; } else { print "command completed"; } if( systemWithTimeout 5, qq[ perl -wle" sleep 7; print for 1 .. 100" ] + ) { print "command timed out"; } else { print "command completed"; } __END__ C:\test>539889 5 second timeout on a process that runs for 4 seconds The process did not timeout it returned 1896 Wed Mar 29 11:50:25 2006 5 second timeout on a process that runs for 5 seconds The process did not timeout it returned 1896 Wed Mar 29 11:50:31 2006 5 second timeout on a process that runs for 6 seconds The process did timeout scalar context command completed command timed out

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: system call with time out by BrowserUk
in thread system call with time out by Win

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.