2013-03-25 138 views

回答

8

如果你想使用定时器中断,使用信号,特别是SIGALRM。 您可以使用功能alarm()来要求超时。如果你想要使用粒度,你可以使用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,一个有趣的软件看门狗守护进程的实现。
+0

感谢您的宝贵答案。我想实现准确的毫秒延迟。这怎么可能? – VigneshK 2013-03-25 07:23:49

+0

没问题,我会编辑我的答案。 – stdcall 2013-03-25 07:31:26