当前位置:首页 > IT技术 > 系统服务 > 正文

Linux进程通信 管道
2021-11-16 11:39:52



Linux进程通信 管道_父进程



简介

管道是Unix系统IPC的最古老形式,所有Unix系统都提供这种形式。管道有以下两种局限性:

(1)历史上,通信方式为半双工。现在某些系统提供全双工管道。

(2)管道只能在具有公共祖先的两个进程之间使用。通常,一个管道由一个进程创建,在进程调用fork后,这个管道就能在父进程和子进程之间使用了。(FIFO无此局限)。

        --《Unix环境高级编程》

通俗理解: Linux的管道通信,通讯方式正如其名一样,如同一个大管道,一端流入,一端流出。半双工通信方式,即只能一端流入另一端流出;全双工通信方式,即一端可以流入也可以流出。

PIPE

PIPE是一种半双工管道,其中,fd[1]用来向管道写入数据,fd[0]用来从管道读出数据。若两个进程需要利用PIPE通信,就要保证一个进程使用fd[0],另一个进程使用fd[1]。

Code:

//参考Linux man手册#include <sys/types.h>#include <sys/wait.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>
int main(int argc, char *argv[]){ int pipe_fd[2]; pid_t child_id; char buf;
if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); }
if (pipe(pipe_fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
child_id = fork(); if (child_id == -1) { perror("fork"); exit(EXIT_FAILURE); }
if (child_id == 0) { /* Child reads from pipe */ close(pipe_fd[1]); /* Close unused write end */
while (read(pipe_fd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); /*Print to terminal*/
write(STDOUT_FILENO, " ", 1); close(pipe_fd[0]); _exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */ close(pipe_fd[0]); /* Close unused read end */ write(pipe_fd[1], argv[1], strlen(argv[1])); close(pipe_fd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); }}


测试:

./pipe HelloWorldHelloWorld


小结:

参考man中pipe的使用代码,大致了解pipe使用方法。即在父进程向管道写入终端输入的 “HelloWorld”,然后在子进程读取管道数据,并在终端输出。

在父子进程共享区,初始化pipe_fd后,即规定pipe_fd[0]为读取端,pipe_fd[1]为写入端。故pipe_fd必须在进程共享区初始化,也就能理解pipe存在开篇中第二个局限性的原因了。

FIFO

FIFO有时也会被称为命名管道,未命名的管道(PIPE)只能在两个相关的进程间使用,而且这个两个进程还要有共同的创建了它们的祖先进程。但是,通过FIFO,不相关的进程也能进行数据交换。

FIFO的使用方法与读写文件类似。先创建FIFO文件,再获取FIFO文件的句柄,然后open、write、read、close。

Code:

fifo写端口代码实现:

//fifo_write.cpp#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <iostream>
#define BUFF_SIZE 1024
int main(int argc, char *argv[]){ int ret = 0, fd = 0; char buff[1024] = {0};
if (argc < 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); }
ret = access(argv[1], F_OK); if (ret == -1) { ret = mkfifo(argv[1], 0664); if (ret == 0) { fprintf(stdout, "Create fifo named %s success. ", argv[1]); } else { fprintf(stderr, "Create fifo named %s failed! ", argv[1]); exit(EXIT_FAILURE); } }
fd =open(argv[1], O_RDWR); if (fd == -1) { fprintf(stderr, "Open fifo failed! "); exit(EXIT_FAILURE); }
while(1) { memset(buff, 0, BUFF_SIZE); fprintf(stdout, "input: "); fgets(buff, BUFF_SIZE, stdin);
write(fd, buff, sizeof(buff));
if (strncmp("end", buff, strlen(buff)-1) == 0) { close(fd); break; } }
return 0;}


fifo读端口代码实现:

//fifo_read.cpp#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <iostream>
#define BUFF_SIZE 1024
int main(int argc, char *argv[]){ int ret = 0, fd = 0; char buff[BUFF_SIZE] = {0};
if (argc < 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); }
ret = access(argv[1], F_OK); if (ret == -1) { ret = mkfifo(argv[1], 0664); if (ret == 0) { fprintf(stdout, "Create fifo named %s success. ", argv[1]); } else { fprintf(stderr, "Create fifo named %s failed! ", argv[1]); exit(EXIT_FAILURE); } }
fd =open(argv[1], O_RDWR); if (fd == -1) { fprintf(stderr, "Open fifo failed! "); exit(EXIT_FAILURE); }
while(1) { memset(buff, 0, BUFF_SIZE); read(fd, buff, sizeof(buff));
if (strncmp("end", buff, strlen(buff)-1) == 0) { close(fd); break; }
fprintf(stdout, "%s", buff); }
return 0;}


  • 当open一个FIFO时,非阻塞(O_NONBLOCK)会产生下列影响:
    (1) 一般情况下(未指定O_NONBLOCK),只读open要阻塞到某个进程为写而打开这个FIFO为止。类似的,只写open要阻塞到某个进程为读而打开这个FIFO为止。
    (2)若指定O_NONBLOCK,则只读open立即返回。但是,若没有进程为读而打开这个FIFO,那么只写open则会返回为-1,将effno设置为ENXIO。
  • 在调用mkfifo时,会创建一个fifo文件。其中第一个参数可为绝对路径或者相对路径。

测试Linux进程通信 管道_父进程_02

总结

对比以上两种管道的方式,可得出PIPE与FIFO的大致差异。

  • 工作方式。PIPE可称为“匿名管道”,无需命名,在具有亲属关系的进程中使用;FIFO又可称为“有名管道”,在使用过程中,其会在系统中创建FIFO类型文件,从而可通过此文件进行不相关进程间的通信。
  • 通信方式。PIPE为半双工通信,即在一次通讯中,数据只能在一个方向上流动。FIFO为全双工通信,在一次通讯中,两端可以同时收发数据。

最后

用心感悟,认真记录,写好每一篇文章,分享每一框干货。愿每一篇文章不负自己,不负看客!


 猜你喜欢

    详解 | Linux系统是如何实现存储并读写文件的?

    ​​C++打怪 之 vector​

    标准字符设备驱动模板


更多文章内容包括但不限于C/C++、Linux、开发常用神器等,可进入开源519公众号聊天界面回复“文章目录” 或者 菜单栏选择“文章目录”查看。

Linux进程通信 管道_数据_03


本文摘自 :https://blog.51cto.com/u