守护进程(管道通信实现)

创建两个进程,一个有名管道。一个进程如果正在运行就往管道里面发送运行信息,守护进程负责读取运行信息,一旦守护进程超过三秒读取不到运行信息,就自动将进程重启

守护进程代码:

[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. #include <stdlib.h>  
  4.   
  5. #include <string.h>  
  6.   
  7. #include <sys/types.h>  
  8.   
  9. #include <sys/stat.h>  
  10.   
  11. #include <errno.h>  
  12.   
  13. #include <fcntl.h>  
  14.   
  15. #define FIFO "/root/linux/myfifo"  
  16.   
  17.   
  18. main(int argc, char** argv)  
  19.   
  20. {  
  21.       
  22.     char buf[100];  
  23.       
  24.     int fd;  
  25.       
  26.     int nread;  
  27.       
  28.     int count = 0;  
  29.   
  30.       
  31.     if((mkfifo(FIFO,O_CREAT|O_EXCL) < 0) &&(errno != EEXIST))  
  32.       
  33.    {  
  34.           
  35.         printf("creat fifo error!\n");  
  36.       
  37.     }  
  38.   
  39.       
  40.     printf("perparing for reading bytes...\n");  
  41.   
  42.       
  43.     memset(buf,0,sizeof(buf));  
  44.   
  45.       
  46.       
  47.     fd = open(FIFO,O_RDONLY|O_NONBLOCK,0);  
  48.       
  49.     if(fd == -1)  
  50.       
  51.     {  
  52.           
  53.         perror("open");  
  54.       
  55.         exit(1);  
  56.       
  57.     }  
  58.       
  59.     while(1)  
  60.       
  61.    {  
  62.           
  63.         memset(buf,0,sizeof(buf));  
  64.   
  65.       
  66.         if(nread = read(fd,buf,100) <= 0)  
  67.       
  68.        {  
  69.           
  70.          
  71.              if(errno == EAGAIN)  
  72.           
  73.             {  
  74.               
  75.                   printf("no data yet\n");  
  76.           
  77.              }  
  78.           
  79.              count++;  
  80.               
  81.              printf("read none\n");  
  82.           
  83.              sleep(1);  
  84.           
  85.              if(count == 3)  
  86.           
  87.             {  
  88.               
  89.                  printf("重新启动函数\n");  
  90.               
  91.                  system("./write success");  
  92.           
  93.                  count = 0;  
  94.           
  95.             }  
  96.               
  97.         }  
  98.       
  99.         else  
  100.       
  101.        {  
  102.       
  103.              printf("read %s from FIFO\n",buf);  
  104.       
  105.              sleep(1);  
  106.       
  107.         }  
  108.       
  109.    }  
  110.       
  111.    pause();  
  112.       
  113.    unlink(FIFO);  
  114.   
  115. }  
守护进程代码:
[objc]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/types.h>  
  2.   
  3. #include <sys/stat.h>  
  4.   
  5. #include <errno.h>  
  6.   
  7. #include <fcntl.h>  
  8.   
  9. #include <stdio.h>  
  10.   
  11. #include <stdlib.h>  
  12.   
  13. #include <string.h>  
  14.   
  15. #define FIFO_SERVER "/root/linux/myfifo"  
  16.   
  17.   
  18. main(int argc, char** argv)  
  19.   
  20. {  
  21.       
  22.     int fd;  
  23.       
  24.     char buf[100];  
  25.       
  26.     int nwrite;  
  27.   
  28.       
  29.     fd = open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);  
  30.       
  31.     if(argc == 1)  
  32.       
  33.     {  
  34.   
  35.         printf("please send st\n");  
  36.   
  37.     exit(-1);  
  38.       
  39.     }  
  40.   
  41.       
  42.     strcpy(buf,argv[1]);  
  43.   
  44.       
  45.     if((nwrite = write(fd,buf,100)) == 1)  
  46.       
  47.     {  
  48.           
  49.         if(errno == EAGAIN)  
  50.       
  51.        {  
  52.           
  53.             printf("not been read yet\n");  
  54.       
  55.        }  
  56.           
  57.        else  
  58.           
  59.       {  
  60.               
  61.             printf("write %s to FIFO\n",buf);  
  62.           
  63.        }  
  64.       
  65.     }  
  66.   
  67. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值