2013-03-25 140 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