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.
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<syslog.h>#include<errno.h>voidMainvoid) { intFd_watchdog = open ("/dev/watchdog", o_wronly); if(Fd_watchdog = =-1) { intErr =errno; printf ("failed to Open/dev/watchdog, errno:%d,%s\n", err, strerror (err)); return; } Else{printf ("Open watchdog success!\n"); } //write data to the/dev/watchdog device every time ("Feed the Dog regularly") if(Fd_watchdog >=0) { StaticUnsignedCharFood =0; ssize_t eaten= Write (Fd_watchdog, &food,1); if(Eaten! =1) {printf ("failed feeding watchdog\n"); } Else{printf ("Success feeded watchdog\n"); } } //Close (Fd_watchdog); }
Linux software watchdog watchdog feeding Dog