Reputation: 1576
There are a number of places in the perl documentation that warn against using sleep in conjunction with alarm since sleep may be implemented via alarm. However, the documentation doesn't say what symptoms to expect if this is the case on your system.
I can detect this using this shell pipeline (idea from this other question):
strace perl -e 'sleep 1' 2>&1 | grep 'sleep\|alarm'
If sleep is implemented via alarm you'll see an alarm system call in the output. Otherwise you'll see a different sleep system call like the following from Ubuntu 10.04.2:
execve("/usr/bin/perl", ["perl", "-e", "sleep 1"], [/* 26 vars */]) = 0
nanosleep({1, 0}, 0xbfacba54) = 0
However, I'm looking for concise pure perl code (preferably a one liner) to detect it along with what you should expect to see if alarm underlies sleep and what to expect if it doesn't.
Note:
Upvotes: 3
Views: 410
Reputation: 1576
The wikipedia entry on SIGALRM states:
[SIGALRM] is sometimes used to implement the sleep function; consequently, programs cannot reliably use alarm to "wake up" from a delay caused by sleep.
Based on this I'm guessing that the following will detect if sleep is implemented via alarm:
perl -e 'alarm 1; sleep 2; print "slept\n"';
If sleep is implemented via alarm the output will be "slept" because the call to sleep will reset the alarm and take over handling the new SIGALRM when it arrives. If sleep is implemented another way the output will be "Alarm clock" showing that the default alarm handler was fired.
Unfortunately, I can't test this case because I haven't found a system that implements sleep via alarm.
(Interestingly the Wikipedia entry cites perl documentation for the sleep function)
Upvotes: 6