用管道通信实现守护进程
来源:互联网 发布:英汉词典软件下载 编辑:程序博客网 时间:2024/05/29 16:18
创建两个进程,一个有名管道。一个进程如果正在运行就往管道里面发送运行信息,守护进程负责读取运行信息,一旦守护进程超过三秒读取不到运行信息,就自动将进程重启
守护进程代码:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <fcntl.h>#define FIFO "/root/linux/myfifo"main(int argc, char** argv){ char buf[100]; int fd; int nread; int count = 0; if((mkfifo(FIFO,O_CREAT|O_EXCL) < 0) &&(errno != EEXIST)) { printf("creat fifo error!\n"); } printf("perparing for reading bytes...\n"); memset(buf,0,sizeof(buf)); fd = open(FIFO,O_RDONLY|O_NONBLOCK,0); if(fd == -1) { perror("open"); exit(1); } while(1) { memset(buf,0,sizeof(buf)); if(nread = read(fd,buf,100) <= 0) { if(errno == EAGAIN) { printf("no data yet\n"); } count++; printf("read none\n"); sleep(1); if(count == 3) { printf("重新启动函数\n"); system("./write success"); count = 0; } } else { printf("read %s from FIFO\n",buf); sleep(1); } } pause(); unlink(FIFO);}守护进程代码:
#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define FIFO_SERVER "/root/linux/myfifo"main(int argc, char** argv){ int fd; char buf[100]; int nwrite; fd = open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0); if(argc == 1) { printf("please send st\n"); exit(-1); } strcpy(buf,argv[1]); if((nwrite = write(fd,buf,100)) == 1) { if(errno == EAGAIN) { printf("not been read yet\n"); } else { printf("write %s to FIFO\n",buf); } }}
0 0
- 用管道通信实现守护进程
- 守护进程(管道通信实现)
- Linux进程通信 有名管道实现守护进程
- 守护进程——管道通信
- 用管道写守护进程
- 用管道实现进程间通信
- 管道实现进程间通信
- Windows管道通信实现进程通信
- 进程间使用管道通信
- [Linux C编程]有名管道实现守护进程
- 使用命名管道实现进程间通信
- 使用命名管道实现进程间通信
- 命名管道实现跨语言进程通信
- 使用命名管道实现进程间通信
- 进程间通信 - 命名管道实现
- 通过匿名管道实现进程间通信
- 进程间通信 - 匿名管道实现
- 使用管道实现进程间的通信
- 织梦dedecms首页列表页ajax点击下拉加载更多文章瀑布流效果
- 正则表达式
- 安卓开发——设置EditText光标cursor的颜色及粗细
- [kuangbin带你飞]专题十二 基础DP1 G HDU 1176
- 段子
- 用管道通信实现守护进程
- 【bzoj 1079】[SCOI2008]着色方案 逆向定义
- 奔三奶爸初学php心得记录(第二天)
- beautifulsoup部分笔记(随时可能有更新)
- Java的反射机制学习2
- CMake添加头文件,库文件,链接库文件
- leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题
- 【POJ 3233】Matrix Power Series(矩阵快速幂)
- nihao, woshi mr jhon