美文网首页
LInux进程之间的通信-有名管道(FIFO)

LInux进程之间的通信-有名管道(FIFO)

作者: 快扶我起来搬砖 | 来源:发表于2018-09-11 16:41 被阅读0次

Linux进程间的通信-有名管道FIFO

管道的通信只能存在于具有亲缘关系的进程之间,比如fork出来的子进程与父进程之间的通信可以通过管道(pipe)来实现。那么非亲缘进程之间怎么来通信呢?这次我们来看一看有名管道(FIFO)。

FIFO的出现克服了非亲缘进程的通信,只要可以访问路径就可以通信,并且FIFO采用了先进先出的原则工作,第一个写入的数据将首先被读出。

  • 创建

    //只有创建设备文件才使用mknod()函数
    int mknod(const char *path, mode_t mod, dev_t dev);
    //除设备文件之外的有名管道的创建
    int mkfifo(const char *path, mode_t mod);
    
    NO. 参数 含义
    1 path 创建有名管道的全路径名
    2 mod 创建有名管道的模式
    3 dev 创建设备文件的类型

    例如:mod参数为S_FIFO | 0666表示创建一个有名管道并且权限为0666

  • 使用

    FIFO的使用也是用open打开。使用open创建的有名管道的进程可能会被阻塞。

    打开方式
    O_RDWR 不会被阻塞
    O_RDONLY 阻塞,直到有写方式打开管道
    O_WRONLY 阻塞,直到有读方式打开管道
  • 示例

    //此进程运行后一直被阻塞,直到有读方式打开有名管道FIFO
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    #define fifo_name "fifo"
    #define buf_size 1024
    
    int main(void){
      int fd;
      char buf[buf_size] = "hello, wfifo!.";
    
      umask(0);
    
      if(mkfifo(fifo_name, S_IFIFO | 0666) == -1){
          printf("mkfifo error.\n");
          exit(1);
      }
    
      if(fd = open(fifo_name, O_WRONLY) == -1){
          printf("open error.\n");
          exit(1);
      }
      write(fd, buf, strlen(buf)+1);
    
      close(fd);
      exit(0);
    }
    
    //读取FIFO中的数据
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #define fifo_name "fifo"
    #define buf_size 1024
    
    int main(void){
      int fd;
      char buf[buf_size];
      
      umask(0);
      
      fd = open(fifo_name, O_RDONLY);
      read(fd, buf, buf_size);
      printf("read contect: %s\n", buf);
    
      close(fd);
    
      exit(0);
    }
    
  • 应用

    使用两个有名管道,实现对于进程之间的通信。

    sever.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>
    #include <unistd.h>
    
    #define RD_FIFO "readfifo"
    #define WR_FIFO "writefifo"
    #define buf_size 1024
    
    int main(void){
      int wfd,rfd;
      char buf[buf_size];
      int len;
    
      umask(0);
      if(mkfifo(WR_FIFO, S_IFIFO | 0666) == -1){
          printf("can't create fifo %s because %s.\n", WR_FIFO, strerror(errno));
          exit(1);
      }
      umask(0);
      wfd = open(WR_FIFO, O_WRONLY);
      if(wfd == -1){  
          printf("open fifo %s error because %s.\n", WR_FIFO, strerror(errno));
          exit(1);
      }
      while ((rfd = open(RD_FIFO, O_RDONLY)) == -1){sleep(1);}
      while (1){
          printf("Server: ");
          fgets(buf, buf_size, stdin);
          buf[strlen(buf)-1] = '\0';
          if(strncmp (buf, "quit", 4) == 0){
              close(wfd);
              unlink(WR_FIFO);
              close(rfd);
              exit(0);
          }
          write(wfd, buf, strlen(buf));
    
          len = read(rfd, buf, buf_size);
          if(len > 0){
              buf[len] = '\0';
              printf("Client: %s\n", buf);
          }
      }
    }
    

    client.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <errno.h>
    #include <unistd.h>
    
    #define RD_FIFO "writefifo"
    #define WR_FIFO "readfifo"
    #define buf_size 1024
    
    int main(void){
      int wfd,rfd;
      char buf[buf_size];
      int len;
    
      umask(0);
      if(mkfifo(WR_FIFO, S_IFIFO | 0666)){
          printf("can't create fifo %s because %s.\n", WR_FIFO, strerror(errno));
          exit(1);
      }
      
      while ((rfd = open(RD_FIFO, O_RDONLY)) == -1){sleep(1);}
    
      wfd = open(WR_FIFO, O_WRONLY);
      if(wfd == -1){  
          printf("open fifo %s error because %s.\n", WR_FIFO, strerror(errno));
          exit(1);
      }
      /*while ((rfd = open(RD_FIFO, O_RDONLY)) == -1){sleep(1);}*/
      while (1){
          len = read(rfd, buf, buf_size);
    
          if(len > 0){
              buf[len] = '\0';
              printf("Server: %s\n", buf);
          }
          printf("Client: ");
          fgets(buf, buf_size, stdin);
          buf[strlen(buf)-1] = 0;
          if(strncmp (buf, "quit", 4) == 0){
              
              close(wfd);
              unlink(WR_FIFO);
              close(rfd);
              exit(0);
    
          }
          write(wfd, buf, buf_size);
      }
    }
    

相关文章

  • LInux进程之间的通信-有名管道(FIFO)

    Linux进程间的通信-有名管道FIFO 管道的通信只能存在于具有亲缘关系的进程之间,比如fork出来的子进程与父...

  • Linux 进程之间的通信方式

    linux使用的进程间通信方式 管道(pipe)、流管道(s_pipe)、无名管道(FIFO)、 套接字 sock...

  • Instruments之Leaks

    1. 介绍 1. 进程 进程是系统资源分配的最小单位 进程结构 进程通信 pipe管道fifo有名管道内存共享映射...

  • Linux进程间通信

    Linux进程间通信的概念 linux下进程间通信的几种主要手段简介: 管道(Pipe)及有名管道(named p...

  • Linux 进程间通信(2) -- fifo有名管道

    进程间通信(IPC - InterProcess Communication) 通信的方式有很多: 文件, 管道,...

  • 进程间通信

    进程通信方式 管道/匿名管道(pipe) 管道的实质是一个内核缓冲区会有哪些问题?怎么解决? 有名管道(FIFO)...

  • Go:Unix域套接字

    关于同一个Linux主机上的进程之间的进程间通信(IPC)方式,有多个选择:例如FIFO、管道、共享内存、套接字等...

  • Linux进程间通信 -- 匿名管道和FIFO

    Linux进程间通信 -- 匿名管道和FIFO 匿名管道 管道包括三种: 匿名管道pipe. 特点:一是单工(单项...

  • Android 进程通信--Binder机制

    一、起源——为什么在Android中使用binder通信机制? linux中的进程通信 管道包含无名管道和有名管道...

  • 8.ipc

    进程间通信 Linux中的进程间通信主要有:管道、FIFO、消息队列、信号量、共享存储以及网络IPC中的套接字。 ...

网友评论

      本文标题:LInux进程之间的通信-有名管道(FIFO)

      本文链接:https://www.haomeiwen.com/subject/hwbugftx.html