实时感知 sleep() 调用?

Real-time aware sleep() call?

我已经编写了一个与 TomTom GPS watches over Bluetooth 通信的实用程序,并且正在努力使其 运行 很好地作为 运行-and-forget 后台守护程序。

程序定期与 GPS 设备通信,然后 sleeps 一段时间,直到再次需要它。

我注意到 sleep() 与系统挂起有奇怪的交互:当我进入系统挂起(笔记本电脑 运行ning Linux 3.16.0 内核)然后唤醒计算机向上,sleep 似乎没有注意到暂停时间。例如,采用以下 sleep.c:

#include <time.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char**argv)
{
    time_t t=time(NULL);
    printf("sleep at: %s", ctime(&t));

    sleep(atoi(argv[1]));

    t=time(NULL);
    printf("wake at: %s", ctime(&t));
}
dlenski@dlenski-ultra:~$ 

编译,运行,中途休眠;

$ gcc sleep.c -o sleep
$ ./sleep 30
sleep at: Fri Aug 21 21:05:36 2015
<suspend computer for 17 seconds>
wake at: Fri Aug 21 21:06:23 2015

是否有以 时钟时间 感知而不是 的方式暂停程序执行的正确方法系统正常运行时间-感知?

(我也尝试了 usleepnanosleepalarm,发现它们的行为相似。)

更新: setitimer, as suggested by @HuStmpHrrr,听起来很有前途......但似乎有同样的问题。

当我在中间暂停时,此版本暂停超过请求的 30 秒...

#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>

void sighandler() {}

int main(int argc, char**argv)
{
    time_t t=time(NULL);
    printf("sleep at: %s", ctime(&t));

    signal(SIGALRM, sighandler);
    struct itimerval timer = {.it_interval={0,0},
                              .it_value={atoi(argv[1]),0}};
    setitimer(ITIMER_REAL, &timer, NULL);
    pause();

    t=time(NULL);
    printf("wake at: %s", ctime(&t));
}

一些解决方案和可能的解决方案:

循环休眠查看真实经过时间

几乎肯定有更好的方法——或者应该有!——但目前我的应用程序可以接受以下解决方案:

  • 当需要长时间睡眠(>30s)时,我重复sleep(30),检查真实经过的时间(time(NULL)-start_time) 不超过所需的总暂停时间。

  • 这样,如果系统挂起,比如说,3 小时,而所需的程序暂停是 1 小时,挂起造成的额外延迟不会超过 30 秒。

执行代码:

void nullhandler(int signal) {}

int isleep(int seconds, int verbose)
{
    if (verbose) {
        fprintf(stderr, "Sleeping for %d seconds...", seconds);
        fflush(stderr);
    }
    signal(SIGALRM, nullhandler);

    // weird workaround for non-realtime-awareness of sleep:
    // 
    int res=0, elapsed=0;
    for (time_t t=time(NULL); (elapsed<seconds) && (res<=0); elapsed=time(NULL)-t)
        res = sleep((seconds-elapsed > 30) ? 30 : seconds-elapsed);

    signal(SIGALRM, SIG_IGN);
    if (res && verbose)
        fprintf(stderr, "%s\n\n", res ? " woken by signal!" : "");
    return (res>0);
}

唤醒回调

@Speed8ump suggested registering a callback for system-wakeup notifications. This thread over at AskUbuntu 显示了如何操作。一个很好的解决方案,但我现在想避免将我相对较低级别的代码过于强烈地绑定到特定的桌面环境。

timer_settime

在我看来,这是最优雅、最正确的解决方案,由 @NominalAnimal. It uses timer_settime() 和来自 librt 的朋友建议。

请注意,为了使它在系统挂起时正常工作,您必须根据 timer_settime 文档将 CLOCK_REALTIMETIMER_ABSTIME 一起使用:

If the value of the CLOCK_REALTIME clock is adjusted while an absolute timer based on that clock is armed, then the expiration of the timer will be appropriately adjusted. Adjustments to the CLOCK_REALTIME clock have no effect on relative timers based on that clock.

这是一个演示(link 和 librt,例如 gcc -lrt -o sleep sleep.c):

#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>

void sighandler(int signal) {}

void isleep(int seconds)
{
    timer_t timerid;
    struct sigevent sev = { .sigev_notify=SIGEV_SIGNAL,
                            .sigev_signo=SIGALRM,
                            .sigev_value=(union sigval){ .sival_ptr = &timerid } };
    signal(SIGALRM, sighandler);
    timer_create(CLOCK_REALTIME, &sev, &timerid);

    struct itimerspec its = {.it_interval={0,0}};
    clock_gettime(CLOCK_REALTIME, &its.it_value);
    its.it_value.tv_sec += seconds;
    timer_settime(timerid, TIMER_ABSTIME, &its, NULL);
    pause();
    signal(SIGALRM, SIG_IGN);
}

int main(int argc, char**argv)
{
    time_t t=time(NULL);
    printf("sleep at: %s", ctime(&t));

    isleep(atoi(argv[1]));

    t=time(NULL);
    printf("wake at: %s", ctime(&t));
}