WatchDog Timer In Linux

c++, embedded, embedded-linux, linux

Solution

If you want to use timer interrupts, use signals, and especially `SIGALRM`. You can use the function alarm() to ask for a timeout. if you want usec granularity you can use ualarm(). Once the timeout has reached it will call a Callback function you defined before.

Here's an example code:

#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) 
   ;
}

You have few other options for implementing a watchdog:

- Write / Use a kernel driver, which actually works as a watchdog, applying a hard reset to the device if the dog is not pet (or kicked)

- Use an watchdog, an interesting implementation of a software watchdog daemon.

Problem

I want to do software wathdog timer using timer interrupt in linux. How can i generate timer interrupt in linux?.

Original source