Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchDog Timer In Linux

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

like image 275
VigneshK Avatar asked Jan 28 '26 07:01

VigneshK


2 Answers

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:

  1. 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)
  2. Use an watchdog, an interesting implementation of a software watchdog daemon.
like image 113
stdcall Avatar answered Jan 30 '26 22:01

stdcall


Interrupts do not exist at the application level (only the kernel manages them, and indeed it is getting a lot of timer interrupts already). You can have signals, timers, and delaying syscalls (notably poll or nanosleep). Read Advanced Linux Programming.

Read first the time(7) man page. Then timer_create(2), poll(2), timerfd_create(2), setitimer(2), sigaction(2), nanosleep(2), clock_gettime(2) etc....

Some kernels can also be configured to have watchdog timers...

like image 38
Basile Starynkevitch Avatar answered Jan 30 '26 21:01

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!