本文隶属于专题系列: Linux IPC实践

FIFO具名/命名管道

   (匿名)管道应用的一个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。

   如果我们想在不相关的进程之间交换数据,可以使用FIFO文件来做这项工作,它经常被称为命名管道;命名管道是一种特殊类型的文件.

创建一个命名管道

1)命名管道可以从命令行上创建:

  $ mkfifo <filename>

2)命名管道在程序里创建:

#include <sys/types.h>
       #include <sys/stat.h>
       int mkfifo(const char *pathname, mode_t mode);
//示例
int main()
{
    if (mkfifo("p2", 0644) == -1)
        err_exit("mkfifo error");
}

FIFO与PIPE的区别:

1) 匿名管道由pipe函数创建并打开。

   命名管道由mkfifo函数创建,打开用open

2) FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的语义  (The only difference between pipes and FIFOs is the manner in which they are created and opened.  

Once these tasks have been accomplished, I/O on pipes and FIFOs has exactly  the same semantics.)

命名管道的打开规则

1)读打开FIFO

   O_NONBLOCK disable(阻塞):阻塞直到有相应进程为写而打开该FIFO

   O_NONBLOCK enable(非阻塞):立刻返回成功

//示例1: 阻塞, 只读打开
int main()
{
    int fd = open("fifo", O_RDONLY);
    if (fd == -1)
        err_exit("FIFO open error");
    cout << "fifo O_RDONLY open success" << endl;
}
//示例2: 只读, 非阻塞打开
int main()
{
    int fd = open("fifo", O_RDONLY|O_NONBLOCK);
    if (fd == -1)
        err_exit("FIFO open error");
    cout << "fifo O_RDONLY open success" << endl;
}

2)写打开FIFO

   O_NONBLOCK disable(阻塞):阻塞直到有相应进程为读而打开该FIFO

O_NONBLOCK enable(非阻塞):立刻返回失败,错误码为ENXIO

//示例1: 阻塞, 只写打开
int main()
{
    int fd = open("fifo", O_WRONLY);
    if (fd == -1)
        err_exit("FIFO open error");
    cout << "FIFO O_WRONLY open success" << endl;
}
//示例2: 非阻塞, 只写打开
int main()
{
    int fd = open("fifo", O_WRONLY|O_NONBLOCK);
    if (fd == -1)
        err_exit("FIFO open error");
    cout << "FIFO O_WRONLY open success" << endl;
}

命名管道的读写规则

同匿名管道

/**FIFO示例, 两个进程通过FIFO对拷数据:利用管道,两个进程间进行文件复制。
1.进程writefifo:
(1)读文件(文件名从命令行参数中获取)
(2)写入管道myFifo(管道由该进程创建)
(3)关闭文件及管道
2.进程readfifo:	
(1)读管道myFifo
(2)写入文件[该文件有进程创建并打开]
(3)关闭文件
(4)删除管道
*/
//1:writefifo
int main(int argc, char *argv[])
{
    if (argc < 2)
        err_quit("Usage: ./writefifo <read-file-name>");
    // 创建管道
    if (mkfifo("myFifo", 0644) == -1)
        err_exit("mkfifo error");
    int outfd = open("myFifo", O_WRONLY);   //打开FIFO
    int infd = open(argv[1], O_RDONLY);     //打开文件
    if (outfd == -1 || infd == -1)
        err_exit("open file/fifo error");
    char buf[BUFSIZ];
    int readBytes;
    while ((readBytes = read(infd, buf, sizeof(buf))) > 0)
    {
        write(outfd, buf, readBytes);
    }
    close(infd);
    close(outfd);
}
//2:readfifo
int main(int argc, char *argv[])
{
    if (argc < 2)
        err_quit("Usage: ./writefifo <write-file-name>");
    int outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);  //创建并打卡文件
    int infd = open("myFifo", O_RDONLY);    //打开FIFO
    if (infd == -1 || outfd == -1)
        err_exit("open file/fifo error");
    char buf[BUFSIZ];
    int readBytes;
    while ((readBytes = read(infd, buf, sizeof(buf))) > 0)
    {
        write(outfd, buf, readBytes);
    }
    close(outfd);
    unlink("myFifo");   //删除FIFO
}
你可能感兴趣的内容
0条评论

dexcoder

这家伙太懒了 <( ̄ ﹌  ̄)>
Owner