Linux之watchdog

时间:2025-02-26 06:59:20

Linux之watchdog | DD'NotesLinux之watchdog使用都是通过/dev/watchdog设备节点来操作使用 通过命令# 写入除大写字母‘V’外的任意字符,开启看门狗,每 44 秒内需要写入一次(喂狗) echo A > /dev/watchdog # 开启看门狗,并且内核会每隔 22 秒自动喂一次狗 echo V > /dev/watchdog 通过应用程序示例如下: #include #ihttps:///2022/09/10/Linux%E4%B9%8Bwatchdog/

使用

都是通过/dev/watchdog设备节点来操作使用
通过命令

# 写入除大写字母‘V’外的任意字符,开启看门狗,每 44 秒内需要写入一次(喂狗)
echo A > /dev/watchdog
# 开启看门狗,并且内核会每隔 22 秒自动喂一次狗
echo V > /dev/watchdog

通过应用程序

示例如下:

#include <>
#include <>
#include <>
#include <>

int main(void)
{
    /*通过open来启动watchdog*/
    int fd = open("/dev/watchdog", O_WRONLY);

    int ret = 0;
    if (fd == -1) {
        perror("watchdog");
        exit(EXIT_FAILURE);
    }
    while (1) {
        /*通过write来喂狗  等同于 ioctl(fd, WDIOC_KEEPALIVE, 0);*/
        ret = write(fd, "\0", 1);
    
        if (ret != 1) {
            ret = -1;
            break;
        }
        sleep(10);
    }

    close(fd);
    return ret;
}

....