Re: alarm() - SIGALRM sent too eariy?

On 2006-10-15 00:27, bwooster47@xxxxxxxxx <bwooster47@xxxxxxxxx> wrote:
anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:
Check the time in the signal handler and restart the alarm if the
expected time hasn't been reached. Set the timeout flag only if
it has.

But that seems to defeat the purpose of using alarm, have to take of it
exiting early!

Alarm and sleep don't make any timing guarantees. They may exit early or
late. You should always check how much time has really passed and act
accordingly. At least an early exit is easy to handle - just repeat the
operation for the remaining time.

I found another script on the web, with people reporting problems:
That page has a script called timed-copy.

I ran it on Fedora system (use -v -v to print debug statements)
timed-copy -v -v -m 110 < /dev/zero > /dev/null

and it invariably exits in 38.42 minutes (and not 110 minutes) - same
thing seen, in multiple runs.

110 minutes == 6_600 seconds == 6_600_000_000 µs.
6_600_000_000 % 2^32 == 2_305_032_704 µs == 38.417 minutes.

So my guess is that the time before the alarm is converted to
microseconds before being passed to the OS and that an integer overflow
happens during the conversion.

I can see this on Debian Linux, too: stracing your program (after fixing
the obvious errors and adding some read operations - please post test
programs which are ready to run, not just code snippets) yields:

setitimer(ITIMER_REAL, {it_interval={0, 0}, it_value={0, 2305032704}},
{it_interval={0, 0}, it_value={0, 0}}) = 0

The parameter it_value is wrong. It should be {6600, 0} instead of {0,

Probably a bug in Time::HiRes. Without use Time::HiRes, alarm(6600) is
called, which is correct.

I tried another script, removed the sysread/syswrite, and replaced the
while loop with just:
while (<>) { }
and then it seemed to work fine (test is still running, atleast it did
not alrm in 38.42 minutes).

Did you redirect the input for this script? If its just waiting for
input it may never process the signal. (Safe signals are a pain - maybe
a lesser pain than unsafe signals, but still a pain)


_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | hjp@xxxxxx | Was sonst wäre der Sinn des Erfindens?
__/ | | -- P. Einstein u. V. Gringmuth in desd