进程间有名管道通信,并可靠退出
我的学记|刘航宇的博客

进程间有名管道通信,并可靠退出

刘航宇
4年前发布 /正在检测是否收录...
温馨提示:
本文最后更新于2022年11月29日,已超过851天没有更新,若内容或图片失效,请留言反馈。

使用管道通信实现两个进程的单机双向通信,并保证可靠退出。免get函数,利用kill结束所有进程办法,小刘原创:
管道程序:
1.随意命名比如 vim g.c复制下面程序,在Linux系统浏览器可以输入本站网址sciarm.com,可以直接复制,不用敲代码。
2.然后 gcc g.c
3../a.out

#include "stdio.h"
int main()
{
   int ret;
   ret = mkfifo("./first",0777);
   if(ret == -1)
   {
      printf("create first error\n");
      return -1;
   }
   ret = mkfifo("./second",0777);
   if(ret == -1)
   {
       printf("create second error\n");
       return -2;
   }
   return 0;
}

A端程序
1.同理创建 vim A.c 复制下面程序
2.gcc A.c -o A
3.然后 ./A

#include "stdio.h"
#include "fcntl.h"
#include "string.h"
int main()
{
   int pid;
   int fd;
   int i;
   char buf[32] = {0};
   pid = fork();
   if(pid > 0) // write first
   {
       fd = open("./first",O_WRONLY,0777);
       if(fd == -1)
       { 
          printf("open first error\n");
          return -1;
       }
       while(1)
       {
          printf("please input send data\n");
          scanf("%s",buf);
          write(fd,buf,strlen(buf));
          if(buf[0] == 'q')
          {
          kill(pid,9);
          waitpid(pid,NULL,0);
              break;
          }
          for(i = 0 ;i < 32 ;i ++)
          {
              buf[i] = 0;
          } 
       }
       close(fd);
   }
   if(pid == 0) // read second
   {
       fd = open("./second",O_RDONLY,0777);
       if(fd == -1)
       {
           printf("open error\n");
           return -2;
       }
       while(1)
       {
           read(fd,buf,32) ; 
           if(buf[0] == 'q')
           {
           kill(pid,9);
              waitpid(pid,NULL,0);
               break;
           }
           printf("recv from B %s\n",buf);
           for(i = 0 ;i < 32; i ++)
           {
              buf[i] = 0;
           }
       }
       close(fd);
   }
   return 0;
}

B端程序
打开另一个TM命令窗口,也要root模式下
1.同理创建 vim B.c 复制下面程序
2.gcc B.c -o B
3.然后 ./B

#include "stdio.h"
#include "fcntl.h"
#include "string.h"
int main()
{
   int pid;
   int fd;
   int i;
   char buf[32] = {0};
   pid = fork();
   if(pid > 0) // write first
   {
       fd = open("./first",O_RDONLY,0777);
       if(fd == -1)
       { 
          printf("open first error\n");
          return -1;
       }
       while(1)
       {
          read(fd,buf,32);
          if(buf[0] == 'q')
          {
             kill(pid,9);
              waitpid(pid,NULL,0);
              break;
          }
          printf("recv from A %s\n",buf);
          for(i = 0 ;i < 32 ;i ++)
          {
              buf[i] = 0;
          } 
       }
       close(fd);
   }
   if(pid == 0) // read second
   {
       fd = open("./second",O_WRONLY,0777);
       if(fd == -1)
       {
           printf("open error\n");
           return -2;
       }
       while(1)
       {
           printf("please input send data\n");
           scanf("%s",buf);
           write(fd,buf,strlen(buf)) ; 
           if(buf[0] == 'q')
           {
              kill(pid,9);
              waitpid(pid,NULL,0);
              break;
           }
           for(i = 0 ;i < 32; i ++)
           {
              buf[i] = 0;
           }
       }
       close(fd);
   }
   return 0;
}

打开第三个窗口进入超管模式输入: ps -axj
可以看到4个进程
图片[1] - 进程间有名管道通信,并可靠退出 - 我的学记|刘航宇的博客
A端:随便输入一些字符,B就能接收到,输入q,两者4个进程可以全面杀掉
图片[2] - 进程间有名管道通信,并可靠退出 - 我的学记|刘航宇的博客
B端:
图片[3] - 进程间有名管道通信,并可靠退出 - 我的学记|刘航宇的博客
再次输入: ps -axj
全部杀掉进程图:
图片[4] - 进程间有名管道通信,并可靠退出 - 我的学记|刘航宇的博客

© 版权声明
THE END
喜欢就支持一下吧
点赞 12 分享 赞赏
评论 共3条
取消
  1. 头像
    烧饼夹菜
     · 
    回复

    服务器是一个月多少钱的?

    1. 头像
      刘航宇 作者
       · 
      回复

      @烧饼夹菜

      这个只做网站服务器,比较便宜,一年60元,域名35,一年不到100。::(滑稽)评论会推送至邮件,方便查阅回复哦::(你懂的)服务器超快,访问速度完胜某工大教务处,下载速度完胜某度云