Linux 看门狗

Linux看门狗使用案例

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/watchdog.h>
#include <sys/ioctl.h>
#include <unistd.h>

void help_info(void)
{
	printf("Usage: wdt_driver_test <timeout> <sleep> <test> <dev>\n");
	printf("    timeout: value in seconds to cause wdt timeout/reset\n");
	printf("    sleep: value in seconds to service the wdt\n");
	printf("    test: 0 - Service wdt with ioctl(), 1 - with write()\n");
}

int main(int argc, const char *argv[])
{
	int fd, timeout, sleep_sec, test, i;
    char*  fstr;
	if (argc < 2) {
		help_info();
		return 1;
	}
    
	timeout = atoi(argv[1]);
	sleep_sec = atoi(argv[2]);
	if (sleep_sec <= 0) {
		sleep_sec = 1;
		printf("correct 0 or negative sleep time to %d seconds\n",
		       sleep_sec);
	}
	test = atoi(argv[3]);
	printf("Starting wdt_driver (timeout: %d, sleep: %d, test: %s)\n",
	       timeout, sleep_sec, (test == 0) ? "ioctl" : "write");
    fstr = argv[4];
	//fd = open("/dev/watchdog0", O_WRONLY);
	fd = open(fstr, O_WRONLY);
	if (fd == -1) {
		perror("watchdog");
		exit(1);
	}
	printf("Trying to set timeout value=%d seconds\n", timeout);
	ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
	printf("The actual timeout was set to %d seconds\n", timeout);
	ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
	printf("Now reading back -- The timeout is %d seconds\n", timeout);
	while (true) {
		if (test == 0) {
			ioctl(fd, WDIOC_KEEPALIVE, 0);
		} else {
			write(fd, "\0", 1);
		}
		sleep(sleep_sec);
	}

	ioctl(fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD);
	printf("disable dog\n");
	close(fd);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值