J1. 进程通信 管道

简介

管道是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>\n", 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, "\n", 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 HelloWorld
HelloWorld

小结:
参考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>\n", 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.\n", argv[1]);
        } else {
            fprintf(stderr, "Create fifo named %s failed!\n", argv[1]);
            exit(EXIT_FAILURE);
        }
    }

    fd =open(argv[1], O_RDWR);
    if (fd == -1) {
        fprintf(stderr, "Open fifo failed!\n");
        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>\n", 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.\n", argv[1]);
        } else {
            fprintf(stderr, "Create fifo named %s failed!\n", argv[1]);
            exit(EXIT_FAILURE);
        }
    }

    fd =open(argv[1], O_RDWR);
    if (fd == -1) {
        fprintf(stderr, "Open fifo failed!\n");
        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文件。其中第一个参数可为绝对路径或者相对路径。

测试

导出

 
FIFO测试.png

总结

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

  • 工作方式。PIPE可称为“匿名管道”,无需命名,在具有亲属关系的进程中使用;FIFO又可称为“有名管道”,在使用过程中,其会在系统中创建FIFO类型文件,从而可通过此文件进行不相关进程间的通信。

  • 通信方式。PIPE为半双工通信,即在一次通讯中,数据只能在一个方向上流动。FIFO为全双工通信,在一次通讯中,两端可以同时收发数据。

 

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

 图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拂去尘世尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值