1、watchdog(看门狗)简介
最近由于业务需要需要一个watchdog来确保设备上运行的程序在崩溃后可以再次重启,所以进行了一些研究。
watchdog(看门狗)就是为了让自己的程序在运行时出现崩溃或跑飞后能够让该程序再次重启。
而Linux下使用watchdog的方法主要有三种:
1.编写一个watchdog可执行程序;
2.编写一个watchdog.sh脚本;
3.在可执行程序中包含watchdog。
本例使用第三种方法,通过父进程监控子进程(任务进程)的运行状态来判断子进程是否崩溃,父进程相当于watchdog。
2、代码实例
本例代码为测试当子进程出现错误崩溃后,父进程(看门狗)能够让子进程再次重启。
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#define true 1
#define false -1
void childProcessFunc()
{
int i = 0;
while (true)
{
++i;
printf("i: %d, pid: %d, ppid: %d\n", i, getpid(), getppid());
if (i == 1