zoukankan      html  css  js  c++  java
  • Linux 软件看门狗 watchdog 喂狗

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

    #include <stdio.h>  
    #include <unistd.h>  
    #include <sys/types.h>  
    #include <sys/stat.h>  
    #include <fcntl.h>  
    #include <syslog.h>  
    #include <errno.h>  
      
    void main(void)  
    {  
        int fd_watchdog = open("/dev/watchdog", O_WRONLY);  
        if(fd_watchdog == -1)  
        {  
            int err = errno;  
            printf("failed to open /dev/watchdog, errno: %d, %s
    ", err, strerror(err));  
            return;  
        }  
        else  
        {  
            printf("open watchdog success!
    ");  
        }  
      
        // 每个一段时间向/dev/watchdog 设备写入数据(“定期喂狗”)  
        if(fd_watchdog >= 0)  
        {  
            static unsigned char food = 0;  
            ssize_t eaten = write(fd_watchdog, &food, 1);  
            if(eaten != 1)  
            {  
                printf("failed feeding watchdog
    ");  
            }  
            else  
            {  
                printf("success feeded watchdog
    ");  
            }  
        }  
      
        //close(fd_watchdog);  
    }  
  • 相关阅读:
    正则表达式与领域特定语言(DSL)
    css兼容性问题的整理
    ASP.NET Web API 2.0新特性:Attribute Routing1
    架构讨论
    用Jekyll在github上写博客
    日历插件(beta)
    SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
    Maven中有三大模块
    hdu 2444
    4.4 从一个表向另外的表中复制行
  • 原文地址:https://www.cnblogs.com/Ph-one/p/8586514.html
Copyright © 2011-2022 走看看