博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux之看门狗 (转)
阅读量:4559 次
发布时间:2019-06-08

本文共 2281 字,大约阅读时间需要 7 分钟。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct watchdog_info{ unsigned int options; //options the card/driver supprots 19 unsigned int firmware_version; //firmcard version of the card unsigned char identity[32]; //identity of the board 21 };#define WATCHDOG_IOCTL_BASE 'W'#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) 27 #define WDIOS_DISABLECARD 0x0001 /* Turn off the watchdog timer */#define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)int Getch (void) //无回显的从屏幕输入字符,来达到喂狗的目的{ int ch; struct termios oldt, newt; //终端设备结构体 tcgetattr(STDIN_FILENO, &oldt); //获得终端属性 newt = oldt; newt.c_lflag &= ~(ECHO|ICANON); //设置无回显属性 tcsetattr(STDIN_FILENO, TCSANOW, &newt); //设置新的终端属性 ch = getchar(); //从键盘输入一个数据 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); //恢复终端设备初始设置 return ch;} //suspend some secondsint zsleep(int millisecond){ unsigned long usec; usec=1000*millisecond; usleep(usec); //睡眠usec秒}int Init(){ int fd; //open device file fd = open("/dev/watchdog",O_RDWR); //打开看门狗设备 if(fd < 0) { printf("device open fail\n"); return -1; } return fd;}int main(int argc,char **argv){ int fd,ch; int i,j; char c; struct watchdog_info wi; fd=Init(); //打开终端看门狗设备 //读板卡信息,但不常用 ioctl(fd,WDIOC_GETSUPPORT,&wi); printf("%d,%s\n",wi.options,wi.identity); //读看门狗溢出时间,默认是5s //重新设置时间为10s i=5; printf("%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i)); //读新的设置时间 printf("%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i)); printf("%d\n",i); //看门狗开始和停止工作,打开和关闭设备具有同样的功能 //关闭 i=WDIOS_DISABLECARD; printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i)); //打开 i=WDIOS_ENABLECARD; printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i)); while(1) { zsleep(100); if((c=Getch())!=27){ //输入如果不是ESC,就喂狗,否则不喂狗,到时间后系统重启 ioctl(fd,WDIOC_KEEPALIVE,NULL); //write(fd,NULL,1); //同样是喂狗 } } close(fd); //关闭设备 return 0;}

 

转载于:https://www.cnblogs.com/sankye/archive/2012/12/10/2811260.html

你可能感兴趣的文章
理解class.forName()
查看>>
每日一小练——数值自乘递归解
查看>>
二叉搜索树 (BST) 的创建以及遍历
查看>>
MyBatis/Ibatis中#和$的区别
查看>>
【JAVASCRIPT】React学习-组件生命周期
查看>>
win 64 文件操作
查看>>
Java范例集锦(二)
查看>>
C语言变量和常量
查看>>
LInuxDay8——shell脚本编程基础
查看>>
topcoder 673
查看>>
Java中一些常用的类,包,接口
查看>>
下载特定区域内街景照片数据 | Download Street View Photos within Selected Region
查看>>
StarUML 破解方法
查看>>
C语言结构体
查看>>
[转]Tribon船体生产设计应用
查看>>
easy ui datagrid 让某行复选框不能选中
查看>>
第六周作业
查看>>
关于adb端口被占用的解决办法
查看>>
php 部分内置函数的使用
查看>>
字符串处理技巧
查看>>