我是靠谱客的博主 善良钢笔,最近开发中收集的这篇文章主要介绍Linux-看门狗应用程序watchdogLinux 看门应用程序书写,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Linux 看门应用程序书写

编写步骤

1、open设备(/dev/watchdog)
fd = open("/dev/watchdog", O_RDWR);
2、start watchdog
ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
3、set outtime
int timeout = 60;
ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
4、喂够
在循环体while(1)内喂
(1)、write。
write(fd, &arg, sizeof(arg));//arg必须是非0数,否则喂狗失败
(2)、ioctl。
ioctl(fd, WDIOC_KEEPALIVE,NULL);
5、close watchdog
ioctl(fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD)
6、记得关闭设备
close(fd);

测试程序

#include <linux/watchdog.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(void)
{
	int dog_fd_,ret;
	dog_fd_ = open("/dev/watchdog", O_WRONLY);
	if (dog_fd_ < 0) {
		  printf("open watchdog failedn");
		return -1;
	}

	int action = WDIOS_DISABLECARD;
	  ret = ioctl(dog_fd_, WDIOC_SETOPTIONS, &action);
	  if (ret < 0) {
		  printf("ioctl WDIOC_SETOPTIONS action:WDIOS_DISABLECARD failedn");
		  close(dog_fd_);
		  dog_fd_ = -1;
		  return -1;
	  }
	  
	int timeout_ = 10;
	ret = ioctl(dog_fd_, WDIOC_SETTIMEOUT, &timeout_);
	if (ret < 0) {
		printf("ioctl WDIOC_SETTIMEOUT failedn");
		close(dog_fd_);
		dog_fd_ = -1;
		return -1;
	}

	ret = ioctl(dog_fd_,WDIOC_GETTIMEOUT, &timeout_);
	if (ret < 0) {
		printf("ioctl WDIOC_GETTIMEOUT failedn");
		close(dog_fd_);
		dog_fd_ = -1;
		return -1;
	}
	printf("recheck timeout: %d", timeout_);
	action = WDIOS_ENABLECARD;
	ret = ioctl(dog_fd_, WDIOC_SETOPTIONS, &action);
	if (ret < 0) {
		printf("ioctl WDIOC_SETOPTIONS action:WDIOS_ENABLECARD failedn");
		close(dog_fd_);
		dog_fd_ = -1;
		return -1;
	}

	printf("feed dog 5s,dog timeout 10sn");
	while(1) {    
		sleep(5);
		ret = ioctl(dog_fd_, WDIOC_KEEPALIVE, NULL);
		if (ret < 0) {
			printf("ioctl WDIOC_SETOPTIONS action:WDIOS_DISABLECARD failedn");
			close(dog_fd_);
			dog_fd_ = -1;
			return -1;
		}
	}  
    close(dog_fd_);
	return  0;
}

最后

以上就是善良钢笔为你收集整理的Linux-看门狗应用程序watchdogLinux 看门应用程序书写的全部内容,希望文章能够帮你解决Linux-看门狗应用程序watchdogLinux 看门应用程序书写所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部