【Linux进程间通信】 - 命名管道FIFO

原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/50826962

在前面一篇文章中【Linux进程间通信】 - 匿名管道中,我们介绍了Linux/Unix系统中最古老的一种进程间通信方式 – 匿名管道。此外,我们也讲解了匿名管道的几个局限性,特别是匿名管道只能用于父子进程或兄弟进程间,无法在不相关的进程间交换数据。为了克服匿名管道的这个限制,有人又提出了一种称作命名管道(FIFO)的进程间通信方式。今天我们就来介绍一下这种通信手段。


1、什么是命名管道?

在上一篇文章我们说过,要想实现进程间通信需要提供一种介质让通信双方的进程都能够访问到,这种介质甚至可以是磁盘中的普通文件。但是对于计算机来说对普通文件进行读写效率实在太低了,无法满足进程间信息交换的实时性要求。那有没有可能提供一种高效的特殊文件作为进程间通信的介质呢?你可以简单地把我们今天介绍的命名管道当做是这样一种特殊文件。命名管道又称作FIFO文件,它是一种特殊的文件类型,它提供了一个路径名与之关联,并以FIFO文件的形式存在于文件系统中。这意味着我们可以在文件系统中看到命名管道。有了这样一个“介质”文件,对于两个即便是没有亲缘关系、不相关的进程来说,只要它们都可以访问该文件,就能够彼此通信了。更方便的是,既然命名管道也是一种文件,那么我们对这类文件的操作也就变得和普通文件的操作一致,非常方便。

2、命名管道与匿名管道的区别

命名管道的特点是区别于匿名管道而言的,归纳起来主要有以下两点:

  1. 匿名管道只能用于具有亲缘关系的两个进程间的通信,而命名管道可以用于任何两个进程间的通信,要灵活方便许多。
  2. 命名管道虽然克服了匿名管道只能在具有亲缘关系的两个进程间使用的限制,但是并未克服匿名管道只能半双工通信的缺点。也就是说命名管道通常也是一种半双工通信方式。
  3. 命名管道是一种特殊的文件类型,而且存在于文件系统中。所以,当通信进程使用完命名管道后,如果没有对其进行删除该文件依然存在。而匿名管道只存在于内存中,无法在文件系统中查看。

3、命名管道的创建

创建命名管道类似于创建一个普通文件,其函数原型如下:

#include <sys/types.h>  
#include <sys/stat.h>  
int mkfifo(const char *filename, mode_t mode);

mkfifo函数的第一个参数是一个路径名,也就是创建成功后命名管道的文件名。第二个参数与打开普通文件的open函数中的mode参数相同。如果创建成功则返回0,否则返回-1。

下面一个例子演示一下如何创建命名管道:

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

int main()
{
    char *pathname = "myfifo";
    if (mkfifo(pathname, 0666) < 0)
    {
        perror("mkfifo error\n");
        exit(1);
    }
    else 
    {
        printf("create a FIFO(name : %s)\n", pathname);
    }
}

程序输出如下:

create a FIFO(name : myfifo)

接下来,我们来验证一下创建后的管道文件是否存在文件系统中。具体过程如下:

fred@ubuntu:~/Workspace/linux_c_code$ sudo find / -name myfifo
/home/fred/Workspace/linux_c_code/myfifo
fred@ubuntu:~/Workspace/linux_c_code$ ls -l myfifo
prw-rw-r-- 1 fred fred 0 Mar  8 15:30 myfifo

我们可以看到,在当前目录下的确存在一份FIFO文件。其中‘p’表示这是一个管道文件。

如果我们以同样的名称重复创建一个命名管道,结果会怎样呢?再次执行上面的代码,输出如下:

mkfifo error! : File exists

4、访问命名管道

4.1、打开FIFO文件

命名管道也是一种(特殊的)文件类型,在访问该文件之前需要先打开。与其他普通文件一样,命名管道的打开也使用open函数。

值得注意的是,不能以O_RDWR(可读可写)的方式打开命名管道,只能以“读”或者“写”的方式打开命名管道文件。其打开的规则如下:

  • 当前进程以“写”方式(且设置了阻塞标识)打开命名管道时,如果已有别的进程以“读”打开该命名管道时,则当前的打开操作成功返回,否则一直阻塞直到有进程为“读”打开该命名管道。
  • 当前进程以“读”方式(且设置了阻塞标识)打开命名管道时,如果已有别的进程为“写”打开该命名管道时,则当前的打开操作成功返回,否则一直阻塞直到有进程为“写”打开该管道。
4.2、命名管道的读写

打开命名管道后,就可以对其进行读写操作了。命名管道的操作和普通文件的操作类似:使用read和write函数进行读写,使用close函数关闭一个命名管道。

下面我们利用上面创建的myfifo文件来演示一下命名管道的读写操作。

写进程fife_write.c如下:

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

#define BUF_SIZE 256

int main()
{
    char buf[BUF_SIZE];
    int fd;
    int i;

    fd = open("myfifo", O_WRONLY);
    if (fd < 0)
    {
        perror("open error!");
        exit(1);
    }   
    printf("open success\n");

    for (i = 0; i < 10; i++)
    {
        bzero(buf, BUF_SIZE);
        int len = sprintf(buf, "write process: this is %dth message!", i);
        if (write(fd, buf, len) < 0)
        {
            perror("write error!");
            close(fd);
            exit(1);
        }
        printf("write to fifo: %s\n", buf);
        sleep(2);// 休眠2秒便于观察
    }

    close(fd);
}

读进程fife_read如下:

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

#define BUF_SIZE 512

int main()
{
    char buf[BUF_SIZE];
    int fd;
    int len;

    fd = open("myfifo", O_RDONLY);
    if (fd < 0)
    {
        perror("open error!");
        exit(1);
    }

    printf("open success\n");
    bzero(buf, BUF_SIZE);
    while ((len = read(fd, buf, BUF_SIZE)) > 0)
    {
        printf("read from fifo: %s\n", buf);
    }
    close(fd);
}

前面我们说过当一个进程为“写”打开命名管道时,如果没有别的进程为“读”打开就一直阻塞到有其他进程为“读”打开。为了更清楚地了解这一过程,我们先运行写进程。这时并没有“open success”输出,写进程一直阻塞在open函数。接下来我们运行读进程,可以看到写进程输出“open success”,open操作返回。

写进程的完整输出如下:

fred@ubuntu:~/Workspace/linux_c_code$ ./fifo_write 
open success
write to fifo: write process: this is 0th message!
write to fifo: write process: this is 1th message!
write to fifo: write process: this is 2th message!
write to fifo: write process: this is 3th message!
write to fifo: write process: this is 4th message!
write to fifo: write process: this is 5th message!
write to fifo: write process: this is 6th message!
write to fifo: write process: this is 7th message!
write to fifo: write process: this is 8th message!
write to fifo: write process: this is 9th message!

读进程的完整输出如下:

fred@ubuntu:~/Workspace/linux_c_code$ ./fifo_read 
open success
read from fifo: write process: this is 0th message!
read from fifo: write process: this is 1th message!
read from fifo: write process: this is 2th message!
read from fifo: write process: this is 3th message!
read from fifo: write process: this is 4th message!
read from fifo: write process: this is 5th message!
read from fifo: write process: this is 6th message!
read from fifo: write process: this is 7th message!
read from fifo: write process: this is 8th message!
read from fifo: write process: this is 9th message!

5、删除命名管道

如果要删除一个使用完的FIFO文件,则需要unlink命令,示例如下:

fred@ubuntu:~/Workspace/linux_c_code$ ls -l myfifo
prw-rw-r-- 1 fred fred 0 Mar  8 16:20 myfifo
fred@ubuntu:~/Workspace/linux_c_code$ unlink myfifo
fred@ubuntu:~/Workspace/linux_c_code$ ls -l myfifo
ls: cannot access myfifo: No such file or directory

Linux也提供了一个相应的unlink函数,这里就不再演示。


参考资料:《UNIX环境高级编程》

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值