📄 openwatchdog.c
字号:
/* *此程序是用于测试watchdog驱动程序wdt3210.c * *实现的功能如下switch语句中: * 1,喂狗 * 2,改timer时间,并喂狗 * 3,获得timer时间,并喂狗 * 4,关闭watchdog,并退出 * */#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <stdio.h>#include <sys/ioctl.h>#include <unistd.h>#include <signal.h>#include "watchdog.h"#define SECONDS 100000000int sleep_seconds(int seconds){ while(seconds>0) { sleep(1); printf("\n%d seconds",seconds--); } return 0;}int main(){ int wdt_fd=-1;//设备文件描述符 int selection;//用于switch语句中,选择case int sec; //睡眠的时间 int quit=0; //判断是否关闭设备文件,退出循环 int proId=-1; //fork Id,子进程和父进程的ID int sleepTime=20;//初始化的睡眠时间 wdt_fd=open("/dev/watchdog",O_RDWR); if(wdt_fd < 0) { perror("watchdog"); } else { printf("open watchdog successfully!\n"); while(1) { proId=fork();//创建进程,从而分成子进程和父进程 if(proId==0) {//子进程执行 sleep_seconds(sleepTime); printf("\n===sleep=over==\n"); break; } else {//父进程执行 printf("\ \n1,adopt watchdog.\ \n2,change the time (input seconds). \ \n3,get the time (seconds).\ \nother numbers: close the watchdog.\ "); scanf("%d",&selection); switch(selection) { case 1 : ioctl(wdt_fd,WDIOC_KEEPALIVE);// 喂狗 printf("\nadopt watchdog successfuly"); break; case 2 : printf("\ninput the time:"); scanf("%d",&sec); sleepTime=sec; sec *= SECONDS; ioctl(wdt_fd,WDIOC_SETTIMEOUT,&sec); //改timer时间 printf("\nset the timeout successfully"); ioctl(wdt_fd,WDIOC_KEEPALIVE);//并喂狗 printf("\nadopt watchdog successfuly"); break; case 3 : ioctl(wdt_fd,WDIOC_GETTIMEOUT,&sec);//获得timer时间 printf("\n=======<timer time=%d seconds>=======\n",sec/SECONDS); printf("\nget the timeout successfully"); ioctl(wdt_fd,WDIOC_KEEPALIVE);//并喂狗 printf("\nadopt watchdog successfuly"); break; default : ioctl(wdt_fd,WDIOS_DISABLECARD);//关闭watchdog printf("\ndisable the watchdog and quit\n"); quit=1;//并退出 break; } if(quit) {//关闭设备文件,退出循环 close(wdt_fd); break; } kill(proId,SIGSTOP);//由于将要再次创建一个进程或退出,所以杀死子进程 } } } printf("\n====end=====\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -