命名管道
顾名思义,它是有名字的管道,这意味着,多个进程都可以通过一个约定好的名字找到同一个管道,这样就可以在没亲缘关系的进程间使用管道了。
命名管道的创建要使用mkfifo
,使用时要先open
,然后读写的方法就和匿名管道一样了。
阻塞
虽然代码的写法与匿名管道类似,但是在时序上,命名管道有更大的自由度,因为用户可以决定open
和read
时是否采用阻塞方式。决定的方法是在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 | 否 | 无 |
- 阻塞的open操作会等待,直到有进程发生对应的open操作(读和写相互对应);
- 非阻塞open for write操作,在没有读进程open时会open失败;
- 非阻塞oepn for read操作,的成功不依赖有没有进程对应的open操作。
read/write的阻塞
操作 | 阻塞open | 阻塞 | 解除阻塞/成功条件 |
---|---|---|---|
write | 是or否 | 否 | 无 |
read | 是 | 是 | 管道中有数据可以读出 |
read | 否 | 否 | 管道中有数据可以读出 |
- write操作总是不阻塞的;
- 若管道中无数据,read操作要么阻塞,要么返回错误。
如果要超越浅尝辄止,可以考虑以下问题:
当两个进程交互的正欢时,其中一个把管道关闭了会怎样?这时,上面每种组合下的读写都会正常吗?再有进程又打开了关闭的管道又会怎样?两个进程都关闭了管道会怎样?关闭前没读出的数据还在管道里吗?