开发者

How can I tell if sleep is implemented using alarm on my system in perl?

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开发者_如何学C", ["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:

  • This question isn't looking for how to keep alarm from interrupting sleep. That has been asked and answered.
  • Not looking for workarounds for when sleep is implemented via alarm. Perhaps you can ask and answer that on another question.
  • This isn't a perl-golfing question. I'm not looking for the MOST concise way.
  • I'm not doing this for any production code it's just a curiosity.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜