欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux看门狗使用

程序员文章站 2022-07-12 10:19:02
...

参考

dev/watchdog和dev/watchdog0 是同一个设备
Linux Watchdog 机制
[watchdog]内核失败的重启方案

使用

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

喂狗方法,write调用,

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

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); 
}
相关标签: linux watchdog