无名管道只适用于有亲缘关系的进程间的通信,那么无亲缘关系的进程间如何通信呢?有名管道正是为了解决这个问题。有名管道(FIFO)不同于无名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,这样,即使与 FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过 FIFO 相互通信,因此,通过 FIFO 不相关的进程也能交换数据。
有名管道和无名管道的不同点:
有名管道和无名管道的相同点:
#include
#include
/* 创建有名管道 */
int mkfifo(const char *pathname, mode_t mode);
/* 参数:
pathname: 路径名,即创建后有名管道的路径名
mode: 文件的权限,与打开普通文件的 open() 函数中的 mode 参数相同
*/
/* 返回值:
0:成功。
-1:失败,文件已存在,或出错。
有名管道在操作上和普通文件一样进行,如:open()、write()、read()、close() 等。但是,和无名管道一样,操作命名管道肯定要考虑默认情况下其阻塞特性。
1.以只读和只写方式 open 管道,并且没有指定非阻塞标志 O_NONBLOCK 。则:
2.以只读和只写方式 open 管道,并指定非阻塞标志 O_NONBLOCK 。则 open() 函数不会阻塞。
非阻塞标志(O_NONBLOCK)打开的命名管道有以下特点:
3.以可读可写方式 open 管道,则 open() 函数不会阻塞。
特点:
/* study.cpp 写端代码 */
#include
#include
#include
#include
#include
#include
int main()
{
int ret;
ret = mkfifo("./FIFO", 0666);
if(ret != 0)
{
perror("mkfifo");
}
int fd_w;
printf("open之前\n");
/* 1. 以只写方式打开有名管道 */
//fd_w = open("./FIFO",O_WRONLY);
/* 2. 以只写方式打开有名管道,非阻塞 */
fd_w = open("./FIFO",O_WRONLY|O_NONBLOCK);
/* 3. 以可读可写方式打开有名管道 */
//fd_w = open("./FIFO",O_RDWR);
printf("open之后\n");
if(fd_w < 0)
{
perror("open");
_exit(-1);
}
char str[64];
int i = 0;
while (i<5)
{
i++;
memset(str,0,sizeof(str));
sprintf(str,"%d: hello world!",i);
printf("写:%s\n",str);
write(fd_w,str,strlen(str)+1);
sleep(2);
}
close(fd_w);
return 0;
}
/* main.c 读端代码 */
#include
#include
#include
#include
#include
#include
int main()
{
int ret;
ret = mkfifo("./FIFO", 0666);
if(ret != 0)
{
perror("mkfifo");
}
int fd_r;
printf("open之前\n");
/* 1. 以只写方式打开有名管道 */
//fd_r = open("./FIFO",O_RDONLY);
/* 2. 以只写方式打开有名管道,非阻塞 */
fd_r = open("./FIFO",O_RDONLY|O_NONBLOCK);
/* 3. 以可读可写方式打开有名管道 */
//fd_r = open("./FIFO",O_RDWR);
printf("open之后\n");
if(fd_r < 0)
{
perror("open");
_exit(-1);
}
char str[64];
int i = 0;
while (i<10)
{
i++;
memset(str,0,sizeof(str));
read(fd_r,str,sizeof(str));
printf("读:%s\n",str);
sleep(1);
}
close(fd_r);
return 0;
}
执行一下几种情况:
1. 以只读和只写方式 open 管道,并且没有指定非阻塞标志 O_NONBLOCK:
先在一个命令窗口执行 ./study ,可见阻塞在 open 函数:
⚙ lingyun@manjaro ~/Document/CppCode/study gcc study.cpp -o study
⚙ lingyun@manjaro ~/Document/CppCode/study gcc main.c -o main
⚙ lingyun@manjaro ~/Document/CppCode/study ./study
mkfifo: File exists
open之前 // open() 阻塞
再在另一个命令窗口执行 ./main ,可见能够继续执行下去:
⚙ lingyun@manjaro ~/Document/CppCode/study ./main
mkfifo: File exists
open之前
open之后
读:1: hello world!
读:2: hello world!
读: //此处写进程已经退出,读进程的 read() 函数就不阻塞了
读:
读:
读:1: hello world! //此处写进程再次执行,读进程恢复阻塞特性(sleep(1)但得等 2s,说明 read() 阻塞)
读:2: hello world!
读:3: hello world!
读:4: hello world!
读:5: hello world!
⚙ lingyun@manjaro ~/Document/CppCode/study
第一个命令窗口得以继续执行:
⚙ lingyun@manjaro ~/Document/CppCode/study ./study
mkfifo: File exists
open之前
open之后 //读进程已经 open 了此管道,open() 不再阻塞
写:1: hello world!
写:2: hello world!
^C //此处退出写进程
✘ ⚙ lingyun@manjaro ~/Document/CppCode/study ./study //再次执行写进程
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world!
写:4: hello world!
写:5: hello world!
⚙ lingyun@manjaro ~/Document/CppCode/study
2.以只读和只写方式 open 管道,并指定非阻塞标志 O_NONBLOCK:
先执行只读程序:
⚙ lingyun@manjaro ~/Document/CppCode/study ./main
mkfifo: File exists
open之前
open之后 // 只读 open() 成功,并且 open() 不阻塞。
读: // read() 读有名管道中读数据时不阻塞。
读:
读:
读:
读:
读:
读:
读:
读:
读:
⚙ lingyun@manjaro ~/Document/CppCode/study
先执行只写程序:
⚙ lingyun@manjaro ~/Document/CppCode/study ./study
mkfifo: File exists
open之前
open之后
open: No such device or address // 只写 open() 将出错返回 -1。
✘ ⚙ lingyun@manjaro ~/Document/CppCode/study
3.以可读可写方式 open 管道:
a. 测试在通信过程中退出读进程:
⚙ lingyun@manjaro ~/Document/CppCode/study ./main //读进程
mkfifo: File exists
open之前
open之后
读:1: hello world!
读:2: hello world!
^Z // 退出读进程
[10] + 9536 suspended ./main
✘ ⚙ lingyun@manjaro ~/Document/CppCode/study
⚙ lingyun@manjaro ~/Document/CppCode/study ./study
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world! //虽然读进程退出,但写进程在向管道中写入数据时,并没有退出
写:4: hello world!
写:5: hello world!
⚙ lingyun@manjaro ~/Document/CppCode/study
b. 测试在通信过程中先退出写进程:
⚙ lingyun@manjaro ~/Document/CppCode/study ./study
mkfifo: File exists
open之前
open之后
写:1: hello world!
写:2: hello world!
写:3: hello world!
写:4: hello world!
写:5: hello world! //这里执行完后,写进程即退出
⚙ lingyun@manjaro ~/Document/CppCode/study
⚙ lingyun@manjaro ~/Document/CppCode/study ./main
open之前
open之后
读:1: hello world!
读:2: hello world!
读:3: hello world!
读:4: hello world!
读:5: hello world!
//程序阻塞在这里,说明 read() 阻塞了