The Linux kernel has the option of integrating WD. After it is enabled, the system will have watchdog device drivers:/dev/watchdog.
This way, you can simply open the device in your application and use it:
#include <fcntl.h>
void Main (void)
{
int FDWD;
Char chrtmp;
FDWD = open ("/dev/watchdog", O_RDWR); As soon as the device is turned on, the watchdog is out of the hole.
for (;;)
{
Write (FDWD, &chrtmp, 1); Either hand, because the internal is not used, and the final data nonzero, because the interior is only to determine whether the zero dog as the initial value.
Sleep (10); On my board, the dog only came out after 16 seconds, so 10 to feed once is OK.
}
} from:http://blog.csdn.net/liigo/article/details/9227205
Linux comes with a watchdog implementation that monitors the operation of the system, including a kernel watchdog module and a user-space watchdog program. The kernel watchdog module communicates with the user space by/dev/watchdog this character device. User space program once opened/dev/watchdog device (commonly known as "open the Dog"), will cause a 1-minute timer in the kernel (system default time), after that, the user space program needs to ensure in 1 minutes to the device to write data (commonly known as "feed the Dog regularly"), Each write operation causes the timer to be reset. If the user space program does not write within 1 minutes, the timer expires will cause a system reboot operation ("Dog bites people" hehe). Through this mechanism, we can ensure that the core process of the system is running for most of the time, even if the process crashes in a particular situation, because it is not possible to "feed the dog" at regular intervals, and the Linux system restarts (reboot) under the watchdog function, the core process is running again. More for embedded systems.
Turn on the/dev/watchdog device ("Open the Dog"):
[CPP]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- int fd_watchdog = open ("/dev/watchdog", o_wronly);
- if (Fd_watchdog = =-1) {
- int err = errno;
- printf ("\ n!!! FAILED to Open/dev/watchdog, errno:%d,%s\n ", Err, strerror (err));
- Syslog (log_warning, "FAILED to Open/dev/watchdog, errno:%d,%s", Err, strerror (err));
- }
Write data to the/dev/watchdog device at regular intervals ("Feed the Dog regularly"):
[CPP]View Plaincopy
- Feed the watchdog
- if (fd_watchdog >= 0) {
- static unsigned char food = 0;
- ssize_t eaten = Write (Fd_watchdog, &food, 1);
- if (eaten! = 1) {
- Puts ("\ n!!! FAILED feeding watchdog ");
- Syslog (log_warning, "FAILED feeding watchdog");
- }
- }
This step is usually not required to turn off the/dev/watchdog device:
[CPP]View Plaincopy
- Close (Fd_watchdog);
Required header file:
[CPP]View Plaincopy
- #include <unistd.h>
- #include <sys/stat.h>
- #include <syslog.h>
- #include <errno.h>
How to use the watchdog under Linux