linux看门狗使用

参考

dev/watchdog和dev/watchdog0 是同一个设备
Linux Watchdog 机制
[watchdog]内核失败的重启方案
LINUX 看门狗
linux-wdt 原理和任意超时时间设置的解决方法
linux watchdog看门狗编程
linux看门狗

使用

  1. dev/watchdog和dev/watchdog0是同一个设备,dev/watchdog来兼容老的接口
  2. Magic关闭特性,关掉看门狗文件句柄前如果写入字母V,则关掉句柄后自动关闭看门狗使用echo –n V >/dev/watchdog,-n使echo不在结尾发送回车

喂狗方法,write调用,

#include 
#include 
#include 
#include 

int main(void)
{
	int fd = open("/dev/watchdog", O_WRONLY);
	int ret = 0;
	if (fd == -1) {
		perror("watchdog");
		exit(EXIT_FAILURE);
	}
	while (1) {
		ret = write(fd, "\0", 1);
		if (ret != 1) {
			ret = -1;
			break;
		}
		sleep(10);
	}
	close(fd);
	return ret;
}

或者ioctl调用,

while (1) { 
	ioctl(fd, WDIOC_KEEPALIVE, 0);
	sleep(10); 
}

设置重启时间,

int timeout;
timeout = 15;
ioctl(fd, WDIOC_SETTIMEOUT, &timeout); //设置超时
printf("The timeout was set to %d seconds\n", timeout);

脚本控制

喂狗与关闭,不加-n好像也行。

$ echo 0 >/dev/watchdog
$ echo -n V > /dev/watchdog

你可能感兴趣的:(linux应用开发)