浅尝辄止1-Linux基础-命名管道

命名管道

顾名思义,它是有名字的管道,这意味着,多个进程都可以通过一个约定好的名字找到同一个管道,这样就可以在没亲缘关系的进程间使用管道了。
命名管道的创建要使用mkfifo,使用时要先open,然后读写的方法就和匿名管道一样了。

阻塞

虽然代码的写法与匿名管道类似,但是在时序上,命名管道有更大的自由度,因为用户可以决定openread时是否采用阻塞方式。决定的方法是在open时,如果有O_NONBLOCK这个flag,就会是非阻塞的,否则就是阻塞的。
以下代码将读、写和阻塞、非阻塞的情况两两组合进行测试:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FIFO_RB_WB "./rbwb" //read block, write block
#define FIFO_RB_WN "./rbwn" //read block, write non-block
#define FIFO_RN_WB "./rnwb" //read non-block, write block
#define FIFO_RN_WN "./rnwn" //read non-block, write non-block

int main(int argc, char **argv){
    int fd = 0;
    int n;
    pid_t pid;
    char *fifo_files[] = {FIFO_RB_WB, FIFO_RB_WN, FIFO_RN_WB, FIFO_RN_WN};
    int i;
    for (i = 0; i < 4; ++i){//测试两两组合的4中情况
        printf("---write is %s---\n", (i & 1) ? "non-block" : "block");
        printf("---read is %s---\n", (i & 2) ? "non-block" : "block");
        unlink(fifo_files[i]);
        //测试写进程快的情况
        pid = fork();
        if (pid > 0){//父进程 写
            char buf[] = "hello world\n";
            int ret = mkfifo(fifo_files[i], 0777);
            if (!ret){
                int flag = O_WRONLY;
                flag |= (i & 1) ? O_NONBLOCK : 0;
                printf("%d open write++\n", __LINE__);
                while ((fd = open(fifo_files[i], flag)) < 0){
                    printf("%d opening write\n", __LINE__);
                    sleep(1);
                }
                printf("%d open write--\n", __LINE__);
                if (fd > 0){
                    printf("%d write++\n", __LINE__);
                    while((n = write(fd, buf, sizeof(buf))) <= 0){
                        printf("%d writing %d\n", __LINE__, n);
                        sleep(1);
                    }
                    printf("%d write--\n", __LINE__);
                    close(fd);
                }
            }
            waitpid(pid,NULL,0);
            unlink(fifo_files[i]);
        }
        else if (pid == 0){//子进程 读
            char buf[80];
            int flag = O_RDONLY;
            flag |= (i & 2) ? O_NONBLOCK : 0;
            sleep(1);
            printf("%d open read++\n", __LINE__);
            while ((fd = open(fifo_files[i], flag)) < 0){
                printf("%d opening read\n", __LINE__);
                sleep(1);
            }
            printf("%d open read--\n", __LINE__);
            sleep(3);
            if (fd > 0){
                printf("%d read++\n", __LINE__);
                while ((n = read(fd, buf, 80)) <= 0){
                    printf("%d reading %d\n", __LINE__, n);
                    sleep(1);
                }
                printf("%d read-- %s\n", __LINE__, buf);
                close(fd);
            }
            exit(0);
        }

        //测试读进程快的情况
        pid = fork();
        if (pid > 0){//父进程 写
            char buf[] = "hello world\n";
            int ret = mkfifo(fifo_files[i], 0777);
            if (!ret){
                int flag = O_WRONLY;
                flag |= (i & 1) ? O_NONBLOCK : 0;
                sleep(1);
                printf("%d open write++\n", __LINE__);
                while ((fd = open(fifo_files[i], flag)) < 0){
                    printf("%d opening write\n", __LINE__);
                    sleep(1);
                }
                printf("%d open write--\n", __LINE__);
                sleep(3);
                if (fd > 0){
                    printf("%d write++\n", __LINE__);
                    while((n = write(fd, buf, sizeof(buf))) <= 0){
                        printf("%d writing %d\n", __LINE__, n);
                        sleep(1);
                    }
                    printf("%d write--\n", __LINE__);
                    close(fd);
                }

            }
            waitpid(pid,NULL,0);
            unlink(fifo_files[i]);
        }
        else if (pid == 0){//子进程 读
            char buf[80];
            int flag = O_RDONLY;
            flag |= (i & 2) ? O_NONBLOCK : 0;
            printf("%d open read++\n", __LINE__);
            while ((fd = open(fifo_files[i], flag)) < 0){
                printf("%d opening read\n", __LINE__);
                sleep(1);
            }
            printf("%d open read--\n", __LINE__);
            if (fd > 0){
                printf("%d read++\n", __LINE__);
                while ((n = read(fd, buf, 80)) <= 0){
                    printf("%d reading %d\n", __LINE__, n);
                    sleep(1);
                }
                printf("%d read-- %s\n", __LINE__, buf);
                close(fd);
            }
            exit(0);
        }
    }
    return 0;
}

输出

---write is block---
---read is block---
31 open write++
55 open read++
60 open read--
36 open write--
38 write++
43 write--
63 read++
68 read-- hello world

107 open read++
82 open write++
87 open write--
112 open read--
114 read++
90 write++
95 write--
119 read-- hello world

---write is non-block---
---read is block---
31 open write++
33 opening write
55 open read++
33 opening write
36 open write--
38 write++
43 write--
60 open read--
63 read++
68 read-- hello world

107 open read++
82 open write++
87 open write--
112 open read--
114 read++
90 write++
95 write--
119 read-- hello world

---write is block---
---read is non-block---
31 open write++
55 open read++
60 open read--
36 open write--
38 write++
43 write--
63 read++
68 read-- hello world

107 open read++
112 open read--
114 read++
116 reading 0
116 reading 0
82 open write++
87 open write--
116 reading -1
116 reading -1
116 reading -1
90 write++
95 write--
119 read-- hello world

---write is non-block---
---read is non-block---
31 open write++
33 opening write
55 open read++
60 open read--
36 open write--
38 write++
43 write--
63 read++
68 read-- hello world

107 open read++
112 open read--
114 read++
116 reading 0
116 reading 0
82 open write++
87 open write--
116 reading -1
116 reading -1
90 write++
116 reading -1
95 write--
119 read-- hello world

根据源码行号和以上输出可以总结如下

open的阻塞

open flag 阻塞 解除阻塞/成功条件
O_WRONLY 有进程open flag带有O_RDONLY
O_WRONLY|O_NONBLOCK 有进程open flag带有O_RDONLY
O_RDONLY 有进程open flag带有O_WRONLY
O_RDONLY|O_NONBLOCK
  1. 阻塞的open操作会等待,直到有进程发生对应的open操作(读和写相互对应);
  2. 非阻塞open for write操作,在没有读进程open时会open失败;
  3. 非阻塞oepn for read操作,的成功不依赖有没有进程对应的open操作。

read/write的阻塞

操作 阻塞open 阻塞 解除阻塞/成功条件
write 是or否
read 管道中有数据可以读出
read 管道中有数据可以读出
  1. write操作总是不阻塞的;
  2. 若管道中无数据,read操作要么阻塞,要么返回错误。

如果要超越浅尝辄止,可以考虑以下问题:
当两个进程交互的正欢时,其中一个把管道关闭了会怎样?这时,上面每种组合下的读写都会正常吗?再有进程又打开了关闭的管道又会怎样?两个进程都关闭了管道会怎样?关闭前没读出的数据还在管道里吗?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容