您的位置:首页 > 运维架构 > Linux

【Linux系统编程】进程间通信之命名管道

2020-03-06 10:50 225 查看

00. 目录

文章目录

01. 命名管道概述

无名管道,由于没有名字,只能用于亲缘关系的进程间通信。为了克服这个缺点,提出了命名管道(FIFO),也叫有名管道、FIFO 文件。

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

命名管道(FIFO)和无名管道(pipe)有一些特点是相同的,不一样的地方在于:

1、FIFO 在文件系统中作为一个特殊的文件而存在,但 FIFO 中的内容却存放在内存中。

2、当使用 FIFO 的进程退出后,FIFO 文件将继续保存在文件系统中以便以后使用。

3、FIFO 有名字,不相关的进程可以通过打开命名管道进行通信。

02. 命名管道创建

相关函数:

#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);
功能:
命名管道的创建。
参数:
pathname : 普通的路径名,也就是创建后 FIFO 的名字。
mode : 文件的权限,与打开普通文件的 open() 函数中的 mode 参数相同。(0666)
返回值:
成功:0   状态码
失败:如果文件已经存在,则会出错且返回 -1。

测试代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
int ret;

ret = mkfifo("fifo", 0666); // 创建命名管道
if(ret != 0){	// 出错
perror("mkfifo");
}

return 0;
}

测试结果:

deng@itcast:~$ gcc 1.c
deng@itcast:~$ ./a.out
deng@itcast:~$ ls fifo
fifo
deng@itcast:~$

03. 命名管道特性

我们可以将命名管道当做普通文件一样进行操作:open()、write()、read()、close()。但是,和无名管道一样,操作命名管道肯定要考虑默认情况下其阻塞特性。

3.1 打开管道阻塞特性

下面验证的是默认情况下的特点,即 open() 的时候没有指定非阻塞标志( O_NONBLOCK )。

open() 以只读方式打开 FIFO 时,要阻塞到某个进程为写而打开此 FIFO
open() 以只写方式打开 FIFO 时,要阻塞到某个进程为读而打开此 FIFO。

简单一句话,只读等着只写,只写等着只读,只有两个都执行到,才会往下执行。

读端代码

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666);
if(ret != 0)
{
perror("mkfifo");
}

printf("before open\n");
fd = open("my_fifo", O_RDONLY);//等着只写
if(fd < 0)
{
perror("open fifo");
}

printf("after open\n");

return 0;
}

写端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666);
if(ret != 0)
{
perror("mkfifo");
}

printf("before open\n");
fd = open("my_fifo", O_WRONLY); //等着只读
if(fd < 0)
{
perror("open fifo");
}

printf("after open\n");

return 0;
}

打开两个终端,只有两个程序都执行之后,两个程序才会有输出。

如果大家不想在 open() 的时候阻塞,我们可以以可读可写方式打开 FIFO 文件,这样 open() 函数就不会阻塞。

// 可读可写方式打开
int fd = open("my_fifo", O_RDWR);

3.2 有名管道读写特性

假如 FIFO 里没有数据,调用 read() 函数从 FIFO 里读数据时 read() 也会阻塞。这个特点和无名管道是一样的。

写端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666);//创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

printf("before open\n");
fd = open("my_fifo", O_WRONLY); //等着只读
if(fd < 0)
{
perror("open fifo");
}
printf("after open\n");

printf("before write\n");
// 5s后才往命名管道写数据,没数据前,读端read()阻塞
sleep(5);
char send[100] = "Hello Mike";
write(fd, send, strlen(send));
printf("write to my_fifo buf=%s\n", send);

return 0;
}

读端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666); //创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

printf("before open\n");
fd = open("my_fifo", O_RDONLY);//等着只写
if(fd < 0)
{
perror("open fifo");
}
printf("after open\n");

printf("before read\n");
char recv[100] = {0};

//读数据,命名管道没数据时会阻塞,有数据时就取出来
read(fd, recv, sizeof(recv));
printf("read from my_fifo buf=[%s]\n", recv);

return 0;
}

通信过程中若写进程先退出了,就算命名管道里没有数据,调用 read() 函数从 FIFO 里读数据时不阻塞;若写进程又重新运行,则调用 read() 函数从 FIFO 里读数据时又恢复阻塞。

写端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666); // 创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

fd = open("my_fifo", O_WRONLY); // 等着只读
if(fd < 0)
{
perror("open fifo");
}

char send[100] = "Hello Mike";
write(fd, send, strlen(send));	//写数据
printf("write to my_fifo buf=%s\n",send);

while(1); // 阻塞,保证读写进程保持着通信过程

return 0;
}

读端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666);// 创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

fd = open("my_fifo", O_RDONLY);// 等着只写
if(fd < 0)
{
perror("open fifo");
}

while(1)
{
char recv[100] = {0};
read(fd, recv, sizeof(recv)); // 读数据
printf("read from my_fifo buf=[%s]\n",recv);
sleep(1);
}

return 0;
}

通信过程中,读进程退出后,写进程向命名管道内写数据时,写进程也会(收到 SIGPIPE 信号)退出。

调用 write() 函数向 FIFO 里写数据,当缓冲区已满时 write() 也会阻塞。

这两个特点和无名管道是一样的,这里不再验证。

04. 命名管道非阻塞

命名管道可以以非阻塞标志(O_NONBLOCK)方式打开:

fd = open("my_fifo", O_WRONLY | O_NONBLOCK);
fd = open("my_fifo", O_RDONLY | O_NONBLOCK);

非阻塞标志(O_NONBLOCK)打开的命名管道有以下特点:
1、先以只读方式打开,如果没有进程已经为写而打开一个 FIFO, 只读 open() 成功,并且 open() 不阻塞。

2、先以只写方式打开,如果没有进程已经为读而打开一个 FIFO,只写 open() 将出错返回 -1。

3、read()、write() 读写命名管道中读数据时不阻塞。

测试代码

写端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666); // 创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

// 只写并指定非阻塞方式打开
fd = open("my_fifo", O_WRONLY|O_NONBLOCK);
if(fd<0)
{
perror("open fifo");
}

char send[100] = "Hello Mike";
write(fd, send, strlen(send));
printf("write to my_fifo buf=%s\n",send);
while(1);

return 0;
}

读端代码:

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

int main(int argc, char *argv[])
{
int fd;
int ret;

ret = mkfifo("my_fifo", 0666); // 创建命名管道
if(ret != 0)
{
perror("mkfifo");
}

// 只读并指定非阻塞方式打开
fd = open("my_fifo", O_RDONLY|O_NONBLOCK);
if(fd < 0)
{
perror("open fifo");
}

while(1)
{
char recv[100] = {0};

read(fd, recv, sizeof(recv));
printf("read from my_fifo buf=[%s]\n",recv);
sleep(1);
}

return 0;
}

05. 附录

  • 点赞 1
  • 收藏
  • 分享
  • 文章举报
沧海一笑-dj 博客专家 发布了641 篇原创文章 · 获赞 2566 · 访问量 91万+ 他的留言板 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: