概述
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, %sn", err, strerror(err)); return; } else { printf("open watchdog success!n"); } // 每个一段时间向/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 watchdogn"); } else { printf("success feeded watchdogn"); } } //close(fd_watchdog); }
最后
以上就是暴躁百褶裙为你收集整理的Linux 软件看门狗 watchdog 喂狗的全部内容,希望文章能够帮你解决Linux 软件看门狗 watchdog 喂狗所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复