gpt4 book ai didi

c - 实时感知 sleep() 调用?

转载 作者:IT王子 更新时间:2023-10-29 00:14:10 25 4
gpt4 key购买 nike

我写了一个实用程序来与 TomTom GPS watches over Bluetooth 对话,并试图让它作为一个运行后忘记后台守护进程很好地运行。

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

我注意到 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:~$

编译运行,中途休眠;

$ 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 , 正如 @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));
}

最佳答案

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

循环休眠,查看真实耗时

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

  • 当需要长时间休眠(>30 秒)时,我重复执行 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);
}

唤醒回调

@Speed8ump建议为系统唤醒通知注册回调。 This thread over at AskUbuntu展示了如何去做。一个很好的解决方案,但我现在想避免将相对较低级别的代码过于强烈地绑定(bind)到特定的桌面环境。

timer_settime

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

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

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

这是一个演示(链接到 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));
}

关于c - 实时感知 sleep() 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32152276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com