实时感知sleep()调用?

Dan*_*ski 9 c linux sleep suspend clock

我已经写了一个实用工具,通过蓝牙TomTom GPS手表交谈,并且我试图让它作为一个即时可待的背景守护程序运行良好.

程序定期与GPS设备通信,然后暂时与sleeps 通信,直到再次需要它为止.

我注意到sleep()与系统挂起奇怪地交互:当我进入系统挂起(运行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:~$ 
Run Code Online (Sandbox Code Playgroud)

中途编译,运行和睡眠;

$ 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
Run Code Online (Sandbox Code Playgroud)

有没有一种正确的方法来暂停程序执行的方式是时钟 - 时间 -意识而不是系统正常运行时间 -意识到?

(我也试过usleep,nanosleepalarm发现他们的行为类似.)

更新: setitimer,正如@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));
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ski 5

一些解决方案和潜在解决方案:

在循环中睡觉并检查实际经过的时间

几乎可以肯定有更好的方法 - 或者应该有! - 但这是我的应用暂时可接受的解决方案:

  • 当需要长时间睡眠(> 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:
    // https://stackoverflow.com/questions/32152276/real-time-aware-sleep-call
    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);
}
Run Code Online (Sandbox Code Playgroud)

唤醒回调

@ Speed8ump建议注册系统唤醒通知的回调.AskUbuntu上的这个帖子展示了如何做到这一点.一个很好的解决方案,但我现在想避免将我的相对较低级别的代码强烈地绑定到特定的桌面环境.

timer_settime

在我看来,这是最优雅和正确的解决方案,由@NominalAnimal建议.它使用time_settime()和来自的朋友librt.

需要注意的是,为了使这项工作正确与系统暂停,必须使用CLOCK_REALTIMETIMER_ABSTIME,按照timer_settime文档:

如果在基于该时钟的绝对定时器被启用的同时调整CLOCK_REALTIME时钟的值,则将适当地调整定时器的期满.对CLOCK_REALTIME时钟的调整对基于该时钟的相对定时器没有影响.

这是一个演示(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));
}
Run Code Online (Sandbox Code Playgroud)