watchdog

Linux 自带了一个 watchdog 的实现,用于监视系统的运行,包括一个内核 watchdog module 和一个用户空间的 watchdog 程序。
内核 watchdog 模块通过 /dev/watchdog 这个字符设备与用户空间通信。用户空间程序一旦打开 /dev/watchdog 设备,就会导致在内核中启动一个 1分钟的定时器,此后,用户空间程序需要保证在 1分钟之内向这个设备写入数据,每次写操作会导致重新设定定时器。如果用户空间程序在 1分钟之内没有写操作,定时器到期会导致一次系统 reboot 操作。
看门狗,又叫 watchdog timer,是一个 定时器电路, 一般有一个输入,叫 喂狗,一个输出到MCU的RST端,MCU正常工作的时候,每隔一段时间输出一个信号到喂狗端,给 WDT 清零,如果超过规定时间不喂狗(一般在程序跑飞时),WDT 定时超过,就会给出一个 复位信号到MCU,使MCU复位. 防止MCU死机. 看门狗的作用就是防止程序发生死循环或者说程序跑飞。
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <unistd.h>  
  7. #include <fcntl.h>  
  8. #include <sys/ioctl.h>  
  9. #include <errno.h>  
  10. #include <sys/time.h>  
  11. #include <unistd.h>  
  12. #include <time.h>  
  13. #include <getopt.h>  
  14. #include <sys/signal.h>  
  15. #include <termios.h>  
  16.   
  17. struct watchdog_info{  
  18.     unsigned int options;   //options the card/driver supprots 19           
  19.     unsigned int firmware_version;  //firmcard version of the card  
  20.     unsigned char identity[32];     //identity of the board 21  
  21.  };  
  22.   
  23. #define WATCHDOG_IOCTL_BASE 'W'  
  24. #define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)  
  25. #define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)  
  26. #define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int) 27   
  27. #define WDIOS_DISABLECARD 0x0001        /* Turn off the watchdog timer */  
  28. #define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */  
  29. #define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)  
  30. #define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)  
  31.   
  32. int Getch (void)   //无回显的从屏幕输入字符,来达到喂狗的目的  
  33.   
  34. {  
  35.   
  36.      int ch;  
  37.      struct termios oldt, newt;   //终端设备结构体  
  38.      tcgetattr(STDIN_FILENO, &oldt);   //获得终端属性  
  39.      newt = oldt;  
  40.      newt.c_lflag &= ~(ECHO|ICANON);   //设置无回显属性  
  41.      tcsetattr(STDIN_FILENO, TCSANOW, &newt);  //设置新的终端属性  
  42.      ch = getchar();   //从键盘输入一个数据  
  43.      tcsetattr(STDIN_FILENO, TCSANOW, &oldt);  //恢复终端设备初始设置  
  44.      return ch;  
  45.   
  46. }  
  47.  //suspend some seconds  
  48. int zsleep(int millisecond)  
  49.   
  50. {  
  51.      unsigned long usec;  
  52.      usec=1000*millisecond;  
  53.      usleep(usec); //睡眠usec秒  
  54. }  
  55. int Init()  
  56. {   
  57.      int fd;  
  58.      //open device file  
  59.      fd = open("/dev/watchdog",O_RDWR);   //打开看门狗设备  
  60.       if(fd < 0)  
  61.      {  
  62.          printf("device open fail\n");  
  63.          return -1;  
  64.      }  
  65.      return fd;  
  66. }  
  67.   
  68. int main(int argc,char **argv)  
  69. {  
  70.      int fd,ch;  
  71.      int i,j;  
  72.      char c;  
  73.      struct watchdog_info wi;  
  74.      fd=Init();  //打开终端看门狗设备  
  75.      //读板卡信息,但不常用  
  76.   
  77.       ioctl(fd,WDIOC_GETSUPPORT,&wi);  
  78.      printf("%d,%s\n",wi.options,wi.identity);  
  79.      //读看门狗溢出时间,默认是5s  
  80.   
  81.      //重新设置时间为10s  
  82.   
  83.      i=5;  
  84.      printf("%d\n",ioctl(fd,WDIOC_SETTIMEOUT,&i));  
  85.      //读新的设置时间  
  86.   
  87.       printf("%d\n",ioctl(fd,WDIOC_GETTIMEOUT,&i));  
  88.      printf("%d\n",i);   
  89.      //看门狗开始和停止工作,打开和关闭设备具有同样的功能  
  90.   
  91.      //关闭  
  92.       i=WDIOS_DISABLECARD;  
  93.      printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));  
  94.      //打开  
  95.       i=WDIOS_ENABLECARD;  
  96.      printf("%d\n",ioctl(fd,WDIOC_SETOPTIONS,&i));  
  97.      while(1)  
  98.      {  
  99.            zsleep(100);  
  100.            if((c=Getch())!=27){  
  101.                 //输入如果不是ESC,就喂狗,否则不喂狗,到时间后系统重启  
  102.   
  103.                 ioctl(fd,WDIOC_KEEPALIVE,NULL);  
  104.                 //write(fd,NULL,1);     //同样是喂狗  
  105.   
  106.            }  
  107.      }  
  108.     close(fd);   //关闭设备  
  109.      return 0;  
  110. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值