1819870 Members
2545 Online
109607 Solutions
New Discussion юеВ

SIGALRM question

 
SOLVED
Go to solution
Tim Leong_1
Advisor

SIGALRM question

I have a perl script that uses $SIG{ALRM} to exit the program:

$timeout = 10;
$SIG{INT} = 'wrapup';
$SIG{TERM} = 'wrapup';
$SIG{ALRM} = 'timeout';
alarm $timeout;
print ROOTLOG "Before while loop - timeout length=$timeout\n";
$i = 1;
while ($i < 20) {
sleep 2;
print ROOTLOG "Inside while loop: call $i\n";
&flush(ROOTLOG);
$i++;
}

However, the sigalrm isn't breaking out of the code. The loop should stop after its 5th iteration, but it continues to 20. I have ruled out any problem with Perl since it works fine on other HP-UX boxes with the same version. Is there anything that could be blocking SIGALRM? SIG{INT} and SIG{TERM} work fine.

Thanks
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: SIGALRM question

Basically because you are not quite playing by the rules. It appears that signal handling is a bit different in this particular Perl implementation but in any event you should never rely upon a SIGALRM handler to work when you are using a loop which includes sleep. Sleep uses SIGALRM as well.


I suggest that you test my theory by replacing your sleep with an artifical delay loop where the sleep 2 currently is.

e.g.
$j = 1;
while ($j <= 500000)
{
$x = ((1.5 * $j) + 3.568) / ($i + 4.67);
++$j;
}

(This is just a complex enough statement to require a bit of processing. You may need to play with the limits of the loop to approximate your 2 second sleep.)


I think that you will now find that the SIG{ALRM} now works as expected.


By the way, the better approach is to put the alarm($timeout), the call that does something, and the alarm(0) - to cancel the alarm inside an eval.

If it ain't broke, I can fix that.
Tim Leong_1
Advisor

Re: SIGALRM question

Ok, that worked. But here's something to cook your noodle. The script as it was originally written with the sleep command works on every other HP-UX box running the same OS. These are slower machines (C240 vs C3700). Any idea?
A. Clay Stephenson
Acclaimed Contributor

Re: SIGALRM question

It's almost certainly not the OS but the specific Perl port. My best guess is that that the alarm signal handler does not reset the previous signal handler but instead sets SIGALRM to be ignored but you would really have to look at the Perl source itself to know.

If it ain't broke, I can fix that.