Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

alarm not triggering SIGALRM on Windows 2003

by perlofwisdom (Pilgrim)
on Sep 23, 2009 at 17:16 UTC ( [id://797036]=perlquestion: print w/replies, xml ) Need Help??

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

I just learning how to use SIGALRM, and have run into a little problem that I need your help with. The following code runs as expected on Unix (solaris) and on an MS Win XP desktop, but fails on MS Win Server 2003 R2. I understand that it's a mistake to intermix sleep and alarm, but it's sufficient for this example (the "real" code doesn't "sleep", and has the same problem).

#!/usr/bin/perl -w use strict; use warnings; $| = 1; $SIG{'ALRM'} = '_ALARM'; $SIG{'QUIT'} = '_QUIT'; my $s_parent = 0; my $patience_level = 5; my $doddling_time = 10; my $child_pid = fork(); if ($child_pid) { # If parent process $s_parent = 1; print "Parent: [tapping toe] You have $patience_level seconds to f +inish your homework ($child_pid)!\n"; alarm $patience_level; my $job_well_done = wait(); print "Parent: Well, that's over ($job_well_done).\n"; } elsif (defined $child_pid) { # If child process print "Child.: [doddling...] OK, OK, just $doddling_time more seco +nds, ...please!?!\n"; sleep $doddling_time; # The following is unlikely if patience_level < doddling_time print "Child.: Nana-nana-na-na - all finished ($$).\n"; exit; } else { # Error on fork die "!!! Failed to fork !!!..: $!\n"; } print "The end.\n"; exit; sub _ALARM { if ($s_parent) { print "Parent: Time's up kiddo...\n"; if (kill 0, $child_pid) { print "Parent: $child_pid is gonna get spanked!\n"; my $spank = kill 'QUIT', $child_pid; print "Pain level: $spank\n"; } else { warn "$child_pid has hidden!\n"; } } else { print "Child.: Nobody expects the Spanish Inquisition!\n"; } } sub _QUIT { print "Child.: Argh! You didn't have to spank me! ($$)\n"; exit; }

I expect to see this output:

Parent: [tapping toe] You have 5 seconds to finish your homework (-670 +8)! Child.: [doddling...] OK, OK, just 10 more seconds, ...please!?! Parent: Time's up kiddo... Parent: -6708 is gonna get spanked! Pain level: 1 Child.: Argh! You didn't have to spank me! (-6708) Parent: Well, that's over (-6708). The end.

But on the server in question, I see:

Parent: [tapping toe] You have 5 seconds to finish your homework (-588 +0)! Child.: [doddling...] OK, OK, just 10 more seconds, ...please!?! Child.: Nana-nana-na-na - all finished (-5880). Parent: Well, that's over (-5880). The end.

My apologies in advance to the non-spankers out there - this was just an effort to make light of a situation that's been frustrating. No children were actually harmed in writing (or executing) this code ;).

Thanks again for your help!

Replies are listed 'Best First'.
Re: alarm not triggering SIGALRM on Windows 2003
by thunders (Priest) on Sep 23, 2009 at 18:38 UTC

    Are you using an older version of perl on the Windows 2003 server? According to perlport for perl 5.8.0 alarm() was not implemented on Win32 systems.

    But by perl 5.8.4 it appears to no longer be an portability issue.

      From the perl -V command:

      Summary of my perl5 (revision 5 version 8 subversion 4) configuration: Platform: osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread : :
        I'd highly recommend you consider changing your Win2003 (production?) box to use the same Perl version as your winXP (development?) box.

        Not only will you run into little quirks with Perl itself (which it sounds like you already have) but you'll also run into quirks and differences in the Core Perl modules...

        Most module maintainers try to keep a fair amount of backward compatibility, but eventually the deprecated methods get turfed...
Re: alarm not triggering SIGALRM on Windows 2003
by thunders (Priest) on Sep 24, 2009 at 02:05 UTC

    This thread has some relevant background, though it doesn't look like there's a solution. I found it in perlbug as well, it's a Windows 2003 specific bug, and appears to still be an open issue.

    Update: Perhaps the alarm() or ualarm() functions in Time::HiRes will allow you to work around the issue?

Re: alarm not triggering SIGALRM on Windows 2003
by afoken (Chancellor) on Sep 24, 2009 at 05:05 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2025-03-20 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (61 votes). Check out past polls.