disciple01 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I was writing a script to 'probe' a number of servers one after another but found my script hung repeatedly on one particular server.

In order to find a workaround for this I found a section in the oreilly advancded perl programming book which outlined a method where the `eval` call could be used to simulate a timeout.

The following bit of code serves as an example :

$SIG{ALRM}=\&time_out; eval{ alarm(2); my $buf=<>; alarm(0); }; exit; sub time_out { die "FED UP WAITING"; }

The eval statement executes, when a die function is called within an eval, the eval section immediately ends. A die function can be called by use of the alarm function.

In other words, the eval starts, the alarm starts giving the user 2 seconds to enter data and hit carriage return, after 2 seconds the alarm kicks in which produces a die statement and causes the script to jump to the end of the eval statement and the script can then continue on and exit.

For my script I'd change the `my $buf=<>;` line for a line which probes a server and then throw a loop round the whole lot to iterate through each server in turn.....

The poblem is the above script works on unix but not on windows, on windows it just sits there forever. I think it's something to do with the way the alarm function is implemented on windows.

I like the above script because it's a simple implementation. Could anyone offer up any alternative methods or solutions on how to get such a timeout script to work on windows?

Replies are listed 'Best First'.
Re: windows eval-timeout with alarm
by BrowserUk (Patriarch) on Nov 05, 2004 at 13:33 UTC

    You may find something useful to you in the thread at Timeouts/timers on Win32 system.

    PodMaster reports and demonstrates that alarm does work on Win32 in 5.8.x, and indeed his demo code works for me also, but when I try using it in any of the classic ways, like inside an eval, it doesn't seem to do anything useful. I'm not sure what I'm doing wrong, but I'll try and work it out/

    There are a coupe of other solutions in that thread.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: windows eval-timeout with alarm
by bpphillips (Friar) on Nov 05, 2004 at 13:08 UTC
    I don't have much personal experience with perl on Win32 but in just looking around on the net, it appearsthe ActiveState didn't implement alarm() until version 5.8. An FAQ from version 5.6 lists alarm() as not being supported. The 5.8 version of that same FAQ still lists alarm() as not being supported but various posts on Google Groups indicate that this is incorrect. I don't have an easy way to test that but would be curious to know what version you're using.

    HTH -- Brian

      If alarm has worked on Win32, in any version 5.6.1 thru 5.8.4, I haven't managed to work out how.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Hi,

      Am running latest activestate perl, version 5.8.4.

      Using the alarm function doesn't throw up any errors or anyting. I know the fork function didn;t used to work properly in perl and now is which is why I thought the alarm function might be the issue.

      Still confused though, any simple alternatives to the above would be greatfully received :)

      According to the ActivePerl documentation, signals are only "crudely" supported on Win32, and alarm() is not implemented at all in ActivePerl. However, I have used the method from "Programming Perl" (also found in "Perl Cookbook"), which uses both alarm signals and alarm(), in some CGI code. It doesn't work, but otherwise seems to do no harm on WindowsXP, and works properly unchanged on a Linux/Apache web server.

      HTH