{"id":5973,"date":"2023-04-25T20:37:58","date_gmt":"2023-04-26T03:37:58","guid":{"rendered":"https:\/\/blackcap.name\/blog\/new\/?p=5973"},"modified":"2023-04-25T20:37:58","modified_gmt":"2023-04-26T03:37:58","slug":"unix-the-alarm-system-call-and-power-management","status":"publish","type":"post","link":"https:\/\/blackcap.name\/blog\/new\/?p=5973","title":{"rendered":"Unix, the Alarm System Call, and Power Management"},"content":{"rendered":"\n<p>And by &#8220;Unix&#8221; I include Unix-like operating systems like Linux and MacOS. In fact, my experience is limited to Linux and MacOS in this regard, but I would be surprised if the various BSD and System V Unix systems out there with automatic power management differ much.<\/p>\n\n\n\n<p>I have a simple alarm clock\/reminder script I wrote in Python. The heart of it was the following logic:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def sleep_until(then):\n    delta = then - time.time()\n    if delta > 0.0:\n        time.sleep(delta)\n        return True\n    return False<\/code><\/pre>\n\n\n\n<p>Now, the <kbd>time.sleep<\/kbd> call in Python is implemented as a call to <kbd>sleep<\/kbd> in the C standard library, which in turn is implemented as via the <kbd>alarm<\/kbd> system call. All of these accept an offset in seconds, which in the former case specifies the amount of time to sleep, and in the latter the amount of time before an alarm signal is delivered to the process.<\/p>\n\n\n\n<p>The logic above is simplicity itself, yet from time to time my reminders would come in late! Eventually, I linked it to those times when the system suspended itself due to lack of activity for a while; and my alerts were late by an amount that corresponded with the time the system was suspended. Apparently, when Unix and  Unix-like systems suspend themselves, time as specified to <kbd>alarm<\/kbd> ceases to pass; that system call only counts seconds that transpire when the system is awake.<\/p>\n\n\n\n<p>The cure is to break up the sleeping into chunks, and to repeatedly check the system clock:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MAX_SLEEP = 60.0\ndef sleep_until(then):\n    delta = then - time.time()\n    if delta &lt;= 0.0:\n        return False\n    while delta > 0.0:\n        time.sleep(min(MAX_SLEEP, delta))\n        delta = then - time.time()\n    return True<\/code><\/pre>\n\n\n\n<p>At least, this seems to work. I implemented the change yesterday and alerts that spanned times when my computer was asleep got raised at the correct time. It&#8217;s a little ugly to replace a blocking with busy-waiting like this, but although the above logic busy-waits, it still spends most of its time blocked.<\/p>\n\n\n\n<p><em>Note that this seems to affect other programs as well.<\/em> In fact, one of my motives for writing this script was the frequent failure of the Gnome clock app to issue alarms at the proper time.<\/p>\n\n\n\n<p><em>Note also that this assumes the computer will be in an awake state at the time the alert is scheduled.<\/em> If the computer goes to sleep and stays asleep, <em>it will issue no alerts during the time it is asleep<\/em>. Remedying this state of affairs requires privileged system calls that one must be careful making. I decided that the safety of having a nonprivileged program was worth the slight chance of a missed alert; in my case, the problem almost always happens as a result of a system suspending itself on lunch break, with the alert time being while I am at my desk in the afternoon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And by &#8220;Unix&#8221; I include Unix-like operating systems like Linux and MacOS. In fact, my experience is limited to Linux and MacOS in this regard, but I would be surprised if the various BSD and System V Unix systems out there with automatic power management differ much. I have a simple alarm clock\/reminder script I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-5973","post","type-post","status-publish","format-standard","hentry","category-computers"],"_links":{"self":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5973"}],"version-history":[{"count":2,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5973\/revisions"}],"predecessor-version":[{"id":5975,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5973\/revisions\/5975"}],"wp:attachment":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}