Reputation: 803
I want to do software wathdog timer using timer interrupt in linux. How can i generate timer interrupt in linux?.
Upvotes: 3
Views: 7934
Reputation: 28920
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:
Upvotes: 8
Reputation: 1
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...
Upvotes: 1