gpt4 book ai didi

c++ - Linux 中的看门狗定时器

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

我想在 linux 中使用定时器中断来做软件看门狗定时器。我怎样才能在 linux 中生成定时器中断?。

最佳答案

如果您想使用定时器中断,请使用信号,尤其是 SIGALRM。您可以使用函数 alarm()请求超时。如果你想要 usec 粒度,你可以使用 ualarm() .一旦达到超时,它将调用您之前定义的回调函数。

这是一个示例代码:

#include <signal.h>

void watchdog(int sig)
{
printf("Pet the dog\r\n");
/* reset the timer so we get called again in 5 seconds */
alarm(5);
}


/* start the timer - we want to wake up in 5 seconds */
int main()
{
/* set up our signal handler to catch SIGALRM */
signal(SIGALRM, watchdog);
alarm(5);
while (true)
;
}

您几乎没有其他选择来实现看门狗:

  1. 编写/使用内核驱动程序,它实际上充当看门狗,如果狗不是宠物(或被踢),则对设备应用硬重置
  2. 使用watchdog ,一个有趣的软件看门狗守护进程实现。

关于c++ - Linux 中的看门狗定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15607933/

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