Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Strawberry Perl and alarm() on Windows

by bloonix (Monk)
on May 22, 2015 at 15:35 UTC ( [id://1127475]=perlquestion: print w/replies, xml ) Need Help??

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

Hello together,

today I tested Strawberry Perl 5.20.2.1 under Windows 8 and it seems that alarm() works. I am confused.
#!perl.exe use strict; use warnings; print scalar localtime, "\n"; eval { $SIG{__DIE__} = sub { alarm(0) }; $SIG{ALRM} = sub { die "timeout" }; alarm(3); sleep 5; alarm(0); }; print $@ if $@; print scalar localtime, "\n";
The output:
Fri May 22 17:21:27 2015
timeout at c:\test\test.pl line 9.
Fri May 22 17:21:30 2015
Is that a new feature in newer Windows versions or does Strawberry Perl implements a workaround for alarm?

Regards, Jonny

Replies are listed 'Best First'.
Re: Strawberry Perl and alarm() on Windows
by afoken (Chancellor) on May 22, 2015 at 16:28 UTC

    Also works in Strawberry 5.14.2 on Win7-64.

    alarm says: Emulated using timers that must be explicitly polled whenever Perl wants to dispatch "safe signals" and therefore cannot interrupt blocking system calls. (Win32). And sleep says: Emulated using synchronization functions such that it can be interrupted by alarm(), and limited to a maximum of 4294967 seconds, approximately 49 days. (Win32)

    So, yes, mixing alarm and sleep works on Win32. Replace sleep with something that blocks for 5 seconds outside perl (i.e. in the system) and alarm will no longer work.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Uhh, am I missing something?

      use strict; use warnings; print scalar localtime, "\n"; eval { $SIG{__DIE__} = sub { alarm(0) }; $SIG{ALRM} = sub { die "timeout" }; alarm(2); system('sleep 5'); }; print $@ if $@; print scalar localtime, "\n";

      Output:

      Fri May 22 17:46:53 2015
      timeout at C:\Temp\b.plx line 8.
      Fri May 22 17:46:55 2015

      Alarm appears to work just fine even when calling out to system

        am I missing something?

        Yes. I did not write about the system function, but about the system (as in "operating system"). I cited alarm in perlport: "Emulated using timers that must be explicitly polled whenever Perl wants to dispatch "safe signals" and therefore cannot interrupt blocking system calls. (Win32)" (Emphasis mine) I wrote " Replace sleep with something that blocks for 5 seconds outside perl (i.e. in the system) and alarm will no longer work."

        Demo, using flock, a function using a system call that may block for a long time:

        #!/usr/bin/perl use strict; use warnings; use v5.10; use Fcntl qw( LOCK_EX LOCK_UN ); use autodie qw( open flock close ); sub main { # create a tempfile open my $h,'>>','tempfile.tmp'; close $h; # start a background process that locks the tempfile for 10 second +s if ($^O eq 'MSWin32') { system 1,$^X,$0,'locker'; } else { my $pid=fork() // die "Can't fork: $!"; unless ($pid) { exec $^X,$0,'locker' or die "Can't exec: $!"; } } sleep 1; # wait one second for the helper process; open $h,'>>','tempfile.tmp'; $@=''; my $start=time(); eval { local $SIG{'ALRM'}=sub { die "timeout" }; say 'main: alarm 5'; alarm(5); say 'main: flock LOCK_EX'; flock($h,LOCK_EX); say 'main: alarm 0'; alarm(0); }; my $err=$@ || 'successfully locked'; my $stop=time(); say "main: $err"; say 'main: ',$stop-$start,' seconds have passed'; close $h; # allow locker() to finish before returning to command prompt ($^O eq 'MSWin32') ? sleep 1 : wait; } sub locker { open my $h,'>>','tempfile.tmp'; say 'locker: flock LOCK_EX'; flock($h,LOCK_EX); say 'locker: locked'; say 'locker: sleep 10'; sleep 10; say 'locker: flock LOCK_UN'; flock($h,LOCK_UN); say 'locker: unlocked'; close $h; } @ARGV ? locker() : main();

        How the demo works:

        Without arguments, main() is invoked, main() creates a background process (using system(1,@args) on Windows, fork() and exec() on Linux), waits a second, then tries to lock a temp file with a classic timeout construct (eval, $SIG{'ALRM'}=sub { die }, alarm($timeout), system call, alarm(0)). Time for this is measured. The background process is the same script, but invoked with an argument, so that locker() is invoked instead of main(). Locker locks the temp file for 10 seconds, and because main() waits a second, it will succeed. In main(), the temp file is already locked, so flock() will block until locker() releases the lock OR the system call is interrupted by the ALRM signal caused by alarm().

        On Linux, signals just work. flock() in main() is interrupted by the ARLM signal, the signal handler in $SIG{'ALRM'} is invoked, that terminales the eval block using die() after 5 seconds.

        On Windows, signals are emulated. alarm($timeout) sets up a timer that must be polled. This is impossible during a blocking system call like flock(). So flock() blocks until it can aquire the lock after about 9 seconds (locker() waits 10 seconds after locking, main() waits 1 second before trying to lock, 9 seconds remain). $SIG{'ALRM'} is not invoked. alarm(0) disables the timer. The eval block terminates without an error.

        Output on Linux:

        >perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX main: timeout at lockdemo.pl line 30. main: 5 seconds have passed locker: flock LOCK_UN locker: unlocked >

        Output on Windows:

        H:\tmp>perl lockdemo.pl locker: flock LOCK_EX locker: locked locker: sleep 10 main: alarm 5 main: flock LOCK_EX locker: flock LOCK_UN main: alarm 0 main: successfully locked main: 10 seconds have passed locker: unlocked H:\tmp>

        So why did alarm($timeout) "work" with system "sleep 10"?

        system @args (unlike system 1,@args) waits for the sub-process to terminate in perl. (flock() waits in the operating system!) During that time, perl can poll the timer and thus can emulate the ARLM signal.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Strawberry Perl and alarm() on Windows
by BrowserUk (Patriarch) on May 22, 2015 at 16:44 UTC
    Is that a new feature in newer Windows versions or does Strawberry Perl implements a workaround for alarm?

    Why did you think alarm wouldn't work on window's?

    Or is your surprise confined to Strawberry Perl?

    Because to my knowledge, alarm has worked on Windows going back to at least 5.8.0 and I'm fairly sure it work on 5.6.1.

    That's AS perl, and before SP existed, but I'm puzzled why you think it wouldn't work?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      Maybe that's the reason why I'm confused. I believed that alarm() + sigalrm doesn't work on Windows, but I don't know why anymore. I am getting old.

      Thanks for the quick responses.

        This thread from circa 5.8.1 days gives a little more information about the subject.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127475]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-24 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found