zoukankan      html  css  js  c++  java
  • 嵌入式喂狗

    什么是watchdog:在一般公司的板子下面,有个字符设备,/dev/watchdog,如果程序员写了一个应用程序,在此程序中open看门狗,然后每隔几秒向看门狗(此时就是个文件)里写任何数据,如果程序意外崩溃了,就不能向看门狗写数据了,等待了30秒或者1分钟,系统就会reboot。(所以看门狗是用来在程序意外崩溃时重启系统的,使崩溃的程序继续运行)。

    有一篇一看就懂的文章:http://blog.csdn.net/liigo/article/details/9227205

    Linux 自带了一个 watchdog 的实现,用于监视系统的运行,包括一个内核 watchdog module 和一个用户空间的 watchdog 程序。内核 watchdog 模块通过 /dev/watchdog 这个字符设备与用户空间通信。用户空间程序一旦打开 /dev/watchdog 设备(俗称“开门放狗”),就会导致在内核中启动一个1分钟的定时器(系统默认时间),此后,用户空间程序需要保证在1分钟之内向这个设备写入数据(俗称“定期喂狗”),每次写操作会导致重新设定定时器。如果用户空间程序在1分钟之内没有写操作,定时器到期会导致一次系统 reboot 操作(“狗咬人了”呵呵)。通过这种机制,我们可以保证系统核心进程大部分时间都处于运行状态,即使特定情形下进程崩溃,因无法正常定时“喂狗”,linux系统在看门狗作用下重新启动(reboot),核心进程又运行起来了。多用于嵌入式系统。

    打开 /dev/watchdog 设备(“开门放狗”):

    1. int fd_watchdog = open("/dev/watchdog", O_WRONLY);  
    2. if(fd_watchdog == -1) {  
    3.     int err = errno;  
    4.     printf(" !!! FAILED to open /dev/watchdog, errno: %d, %s ", err, strerror(err));  
    5.     syslog(LOG_WARNING, "FAILED to open /dev/watchdog, errno: %d, %s", err, strerror(err));  
    6. }  

    每隔一段时间向 /dev/watchdog 设备写入数据(“定期喂狗”):

    1. //feed the watchdog  
    2. if(fd_watchdog >= 0) {  
    3.     static unsigned char food = 0;  
    4.     ssize_t eaten = write(fd_watchdog, &food, 1);  
    5.     if(eaten != 1) {  
    6.         puts(" !!! FAILED feeding watchdog");  
    7.         syslog(LOG_WARNING, "FAILED feeding watchdog");  
    8.     }  
    9. }  


    关闭 /dev/watchdog 设备,通常不需要这个步骤:

    1. close(fd_watchdog);  

    所需头文件:

      1. #include <unistd.h>  
      2. #include <sys/stat.h>  
      3. #include <syslog.h>  
      4. #include <errno.h>  
  • 相关阅读:
    mybatis 源码分析(四)一二级缓存分析
    mybatis 源码分析(三)Executor 详解
    mybatis 源码分析(二)mapper 初始化
    mybatis 源码分析(一)框架结构概览
    Disruptor 详解 二
    Disruptor 详解 一
    JDK源码分析(12)之 ConcurrentHashMap 详解
    并发系列(7)之 ScheduledThreadPoolExecutor 详解
    数据结构系列(6)之 完全二叉堆
    并发系列(6)之 ThreadPoolExecutor 详解
  • 原文地址:https://www.cnblogs.com/judes/p/7086267.html
Copyright © 2011-2022 走看看