• 【Linux】Linux进程间通信——有名管道



    进程间通信之有名管道​

    进程间通信有多种方式实现,本文主要讲解有名管道的通信方式。

    一, 有名管道简介

    匿名管道由于没有名字,只能用于具有亲缘关系的进程间通信。

    为了克服这个缺点,就提出了有名管道(FIFO),也称为命名管道FIFO文件

    有名管道(FIFO)提供了一个路径名与之关联,以FIFO的文件形式存在于文件系统中,且打开方式与打开一个普通文件是一样的。即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过FIFO相互通信。因此,通过FIFO不相关的进程也能交换数据。

    FIFO文件被打开,就可以使用与操作匿名管道和其它文件的系统调用一样的I/O系统调用,如使用 read()读数据write()写数据close()关闭FIFO等。

    与匿名管道一样,FIFO也有一个写入端和读取端,且从管道中读取数据的顺序与写入数据的顺序是一样的。FIFO的名称也由此而来:先入先出,也是一个环形队列。

    有名管道和匿名管道大部分是相同的,不同在于:

    • FIFO在文件系统中作为一个特殊文件存在,FIFO的内容存放在内存中;
    • 当使用FIFO的进程退出后,FIFO文件将继续保存在文件系统中以便以后使用;

    二, 有名管道的使用

    1. 创建有名管道

    • 方式1:可以使用命令创建有名管道:
    $ mkfifo 名字
    
    • 方式2:使用函数mkfifo()函数创建有名管道

    使用mkfifo()创建了FIFO后,就可以使用open()打开它,常见的文件I/O函数都可以用于FIFO。

    FIFO严格遵循先进先出,对FIFO的读总是从开始处返回数据,对FIFO的写则是把数据添加到末尾,FIFO不支持lseek()等文件定位的函数。

    函数mkkfifo()声明:

    #include 
    #include 
    int mkfifo(const char *pathname, mode_t mode);
    
    • 参数说明:
      • pathname:管道名称的路径;
      • mode:FIFO的权限,和open是一样的;如0664;
    • 返回值:成功返回0,失败返回-1,并设置对应的errno;

    创建管道示例:

    #include 
    #include 
    #include 
    
    int main()
    {
        // 创建有名管道
        int ret = mkfifo("fifo1",0664);
        if(ret == -1)
        {
            perror("mkfifo");
            return -1;
        }
    
        return 0;
    }
    

    有名管道使用示例,有两个文件,read.c用来读管道中的数据,write.c用来向管道写数据。read.c和write.c的内容如下:

    read.c

    // 从管道中读取数据
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
    
        // 1. 打开管道文件
        int fd = open("test",O_RDONLY);  // 阻塞,如果没有写端打开那么会一直阻塞在这里
        if(fd == -1){
            perror("open");
            exit(0);
        }
    
        // 2. 读数据
        while (1)
        {
            char buf[1024]={0};
            int len = read(fd,buf,sizeof(buf));
            if(len == -1){
                perror("read");
                exit(0);
            }
            else if(len == 0){
                // len = 0表示已经读到管道末尾,写端断开连接了
                break;
            }
            printf("receive buf: %s\n",buf);
        }
    
        // 3. 关闭FIFO
        close(fd);
        
    
        return 0;
    }
    

    write.c:

    // 向管道中写数据
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        // 创建有名管道
        // 1. 先判断文件是否存在
        if(-1 == access("test",F_OK))   // 判断文件是否存在
        {
            printf("管道不存在,创建管道\n");
            // 2. 创建管道文件
            int ret = mkfifo("test",0664);
            if(ret == -1)
            {
                perror("mkfifo");
                return -1;
            }
        }
    
        // 3. 打开管道,以只写方式打开管道
        int fd = open("test",O_WRONLY);  // 阻塞方式,如果没有读端打开,那么会一直阻塞在这里
        if(fd == -1)
        {
            perror("open");
            exit(0);
        }
    
        // 4. 向管道中写入数据
        for(int i=0;i<100;i++){
            char buf[1024]={0};
            sprintf(buf,"hello, %d\n",i);
            printf("write data : %s\n",buf);
            write(fd,buf,strlen(buf));
            sleep(1);
        }
    
        // 5. 关闭FIFO
        close(fd);
    
        return 0;
    }
    

    生成可执行文件 read 和 write,当只执行read或只执行write时,会一直阻塞;当两个文件都执行时,会进行数据传递;

    在这里插入图片描述

    2. 有名管道的注意事项

    • 一个进程以只读打开管道会阻塞,直到另外一个进程以只写打开管道;
    • 一个进程以只写打开管道会阻塞,直到另外一个进程以只读打开管道;

    有名管道的读写特性:

    • 读管道
      • 管道中有数据,read返回实际读到的字节数;
      • 管道中无数据:
        • 管道写端被全部关闭,read返回0,相当于读到文件末尾;
        • 管道写端没有被全部关闭,read阻塞等待;
    • 写管道
      • 管道读端全部关闭,进程异常终止,收到信号SIGPIPE;
      • 管道读端没有被全部关闭:
        • 管道已经满了,write阻塞等待
        • 管道没有满,write将数据写入,并返回实际写入的字节数

    三, 有名管道简单实例

    • 使用有名管道完成简单聊天的功能

    进程A

    • 以只写方式打开管道1;
    • 以只读方式打开管道2;
    • 循环写读数据;

    进程B

    • 以只读方式打开管道1;
    • 以只写方式打开管道2;
    • 循环读写数据;

    代码:
    chatA.c

    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        char *fifo1 = "fifo1";
        char *fifo2 = "fifo2";
    
        // 1. 判断有名管道文件是否存在
        int ret = access(fifo1, F_OK);
        if (ret == -1)
        {
            // 文件不存在,需要创建
            printf("%s文件不存在,创建管道\n", fifo1);
            ret = mkfifo(fifo1, 0664);
            if (ret == -1)
            {
                perror("mkfifo");
                exit(-1); // 退出
            }
        }
    
        ret = access(fifo2, F_OK);
        if (ret == -1)
        {
            // 文件不存在,需要创建
            printf("%s文件不存在,创建管道\n", fifo2);
            ret = mkfifo(fifo2, 0664);
            if (ret == -1)
            {
                perror("mkfifo");
                exit(-1); // 退出
            }
        }
    
        // 2. 以只写方式打开管道1
    
        int fdw = open(fifo1, O_WRONLY);
        if (fdw == -1)
        {
            perror("open");
            exit(-1);
        }
    
        printf("只写方式打开fifo1成功,等待写入数据...\n");
    
        // 以只读方式打开管道2
        int fdr = open(fifo2, O_RDONLY);
        if (fdr == -1)
        {
            perror("open");
            exit(-1);
        }
        printf("只读方式打开fifo2成功,等待读取数据...\n");
    
        // 3. 循环写读数据
        char buf[256];
        while (1)
        {
            memset(buf, 0, sizeof(buf));
            // 获取标准输入的数据,使用fgets()函数
            fgets(buf, sizeof(buf), stdin);
            // 写数据到fifo1
            int len = write(fdw, buf, strlen(buf));
            if (len == -1)
            {
                perror("write");
                break;
            }
    
            // 读管道数据
            memset(buf, 0, sizeof(buf));
            len = read(fdr, buf, sizeof(buf));
            if (len <= 0)
            {
                perror("read");
                break;
            }
            printf("buf : %s\n",buf);
        }
    
        // 关闭文件描述符
        close(fdr);
        close(fdw);
    
        return 0;
    }
    

    chatB.c

    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        char *fifo1 = "fifo1";
        char *fifo2 = "fifo2";
    
        // 1. 判断有名管道文件是否存在
        int ret = access(fifo1, F_OK);
        if (ret == -1)
        {
            // 文件不存在,需要创建
            printf("%s文件不存在,创建管道\n", fifo1);
            ret = mkfifo(fifo1, 0664);
            if (ret == -1)
            {
                perror("mkfifo");
                exit(-1); // 退出
            }
        }
    
        ret = access(fifo2, F_OK);
        if (ret == -1)
        {
            // 文件不存在,需要创建
            printf("%s文件不存在,创建管道\n", fifo2);
            ret = mkfifo(fifo2, 0664);
            if (ret == -1)
            {
                perror("mkfifo");
                exit(-1); // 退出
            }
        }
    
        // 2. 以只读方式打开管道1
        int fdr = open(fifo1, O_RDONLY);
        if (fdr == -1)
        {
            perror("open");
            exit(-1);
        }
        printf("只读方式打开fifo1成功,等待读取数据...\n");
       
       
        // 以只写方式打开管道2
        int fdw = open(fifo2, O_WRONLY);
        if (fdw == -1)
        {
            perror("open");
            exit(-1);
        }
        printf("只写方式打开fifo2成功,等待写入数据...\n");
    
        // 3. 循环读写数据
        char buf[256];
        while (1)
        {
             // 读管道数据
            memset(buf, 0, sizeof(buf));
            int len = read(fdr, buf, sizeof(buf));
            if (len <= 0)
            {
                perror("read");
                break;
            }
            printf("buf : %s\n", buf);
    
            memset(buf, 0, sizeof(buf));
            // 获取标准输入的数据,使用fgets()函数
            fgets(buf, sizeof(buf), stdin);
            // 写数据到fifo1
            len = write(fdw, buf, strlen(buf));
            if (len == -1)
            {
                perror("write");
                break;
            }
    
           
        }
    
        // 关闭文件描述符
        close(fdr);
        close(fdw);
    
        return 0;
    }
    

    执行生成的可执行文件ab
    在这里插入图片描述
    可以看出来上面的程序还是有点问题,两个进程之间只能"你来我往"——a发送一句,b接收一句,然后b再发送,a再接收...,不能a或b连续发送几句,a或b一直接收。

    解决方案:读写操作分别放到不同的进程中,比如a中父进程写FIFO1,子进程读FIFO2,b中父进程写FIFO2,子进程读FIFO1

    实现参考:
    newChatA.c

    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        // 1. 创建子进程
        pid_t pid = fork();
    
        if (pid > 0)
        {
            // 父进程, 写FIFO1
            char *fifo1 = "fifo1";
            // 2. 判断有名管道文件是否存在
            int ret = access(fifo1, F_OK);
            if (ret == -1)
            {
                // 文件不存在,需要创建
                printf("%s文件不存在,创建管道\n", fifo1);
                ret = mkfifo(fifo1, 0664);
                if (ret == -1)
                {
                    perror("mkfifo");
                    exit(-1); // 退出
                }
            }
    
            // 3. 以只写方式打开 FIFO1
    
            int fdw = open(fifo1, O_WRONLY);
            if (fdw == -1)
            {
                perror("open");
                exit(-1);
            }
    
            printf("只写方式打开fifo1成功,等待写入数据...\n");
    
            // 4. 循环写入数据
            char buf[256];
            while (1)
            {
                memset(buf, 0, sizeof(buf));
                // 获取标准输入的数据,使用fgets()函数
                fgets(buf, sizeof(buf), stdin);
                // 写数据到fifo1
                int len = write(fdw, buf, strlen(buf));
                if (len == -1)
                {
                    perror("write");
                    break;
                }
            }
    
            close(fdw);
        }
        else if (pid == 0)
        {
            // 子进程  读FIFO2
            char *fifo2 = "fifo2";
    
            int ret = access(fifo2, F_OK);
            if (ret == -1)
            {
                // 文件不存在,需要创建
                printf("%s文件不存在,创建管道\n", fifo2);
                ret = mkfifo(fifo2, 0664);
                if (ret == -1)
                {
                    perror("mkfifo");
                    exit(-1); // 退出
                }
            }
    
            // 以只读方式打开 FIFO2
            int fdr = open(fifo2, O_RDONLY);
            if (fdr == -1)
            {
                perror("open");
                exit(-1);
            }
            printf("只读方式打开fifo2成功,等待读取数据...\n");
    
            // 循环读数据
            char buf[256];
            while (1)
            {
                // 读管道数据
                memset(buf, 0, sizeof(buf));
                int len = read(fdr, buf, sizeof(buf));
                if (len <= 0)
                {
                    perror("read");
                    break;
                }
                printf("buf : %s\n", buf);
            }
            close(fdr);
        }
    
    
    
        return 0;
    }
    

    newChatB.c

    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        // 1. 创建子进程
        pid_t pid = fork();
    
        if (pid > 0)
        {
            // 父进程, 写FIFO2
            char *fifo2 = "fifo2";
            // 2. 判断有名管道文件是否存在
            int ret = access(fifo2, F_OK);
            if (ret == -1)
            {
                // 文件不存在,需要创建
                printf("%s文件不存在,创建管道\n", fifo2);
                ret = mkfifo(fifo2, 0664);
                if (ret == -1)
                {
                    perror("mkfifo");
                    exit(-1); // 退出
                }
            }
    
            // 3. 以只写方式打开 FIFO2
    
            int fdw = open(fifo2, O_WRONLY);
            if (fdw == -1)
            {
                perror("open");
                exit(-1);
            }
    
            printf("只写方式打开fifo2成功,等待写入数据...\n");
    
            // 4. 循环写入数据
            char buf[256];
            while (1)
            {
                memset(buf, 0, sizeof(buf));
                // 获取标准输入的数据,使用fgets()函数
                fgets(buf, sizeof(buf), stdin);
                // 写数据到fifo1
                int len = write(fdw, buf, strlen(buf));
                if (len == -1)
                {
                    perror("write");
                    break;
                }
            }
    
            close(fdw);
        }
        else if (pid == 0)
        {
            // 子进程  读FIFO1
            char *fifo1 = "fifo1";
    
            int ret = access(fifo1, F_OK);
            if (ret == -1)
            {
                // 文件不存在,需要创建
                printf("%s文件不存在,创建管道\n", fifo1);
                ret = mkfifo(fifo1, 0664);
                if (ret == -1)
                {
                    perror("mkfifo");
                    exit(-1); // 退出
                }
            }
    
            // 以只读方式打开 FIFO1
            int fdr = open(fifo1, O_RDONLY);
            if (fdr == -1)
            {
                perror("open");
                exit(-1);
            }
            printf("只读方式打开fifo1成功,等待读取数据...\n");
    
            // 循环读数据
            char buf[256];
            while (1)
            {
                // 读管道数据
                memset(buf, 0, sizeof(buf));
                int len = read(fdr, buf, sizeof(buf));
                if (len <= 0)
                {
                    perror("read");
                    break;
                }
                printf("buf : %s\n", buf);
            }
            close(fdr);
        }
    
    
        return 0;
    }
    

    程序执行:
    在这里插入图片描述

  • 相关阅读:
    简单工厂模式
    双软认证需要什么条件
    移动端实现HTML5 mp3录音踩坑指南:系统播放音量变小、一些机型录音断断续续 之 MediaRecorder和AudioWorklet的终极对决
    基于SSM的电子竞技管理平台
    酷早报:7月21日Web3加密行业新闻大汇总
    3道真题训练|学会链表的前世今生
    算法基础13:数结构
    【数据结构 | 入门】 入坑篇 (浙江大学数据结构学习笔记)
    Unity学习资源(超全)汇总 基础+项目+进阶+面试
    【LeetCode】53、 最大子数组和
  • 原文地址:https://blog.csdn.net/sinat_41752325/article/details/127036598