命名管道

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.)。

 

命名管道的打开规则

如果当前打开操作是为读而打开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;
}

 

如果当前打开操作是为写而打开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;
}

 

命名管道的读写规则同匿名管道

 

示例:不同进程间利用命名管道实现文件复制

写管道进程:

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

int main(int argc, char **argv)
{
    if(argc != 2){
        fprintf(stderr,"usage:%s srcfile\n",argv[0]);
        exit(EXIT_FAILURE);
    }
    int infd;
    infd = open(argv[1],O_RDONLY);  //打开文件
    if(infd == -1){
        perror("open error");
        exit(EXIT_FAILURE);
    }

    if(mkfifo("tmpfifo",0644) == -1){
        perror("mkfifo error");
        exit(EXIT_FAILURE);
    }
    int fd ;
    fd = open("tmpfifo",O_WRONLY);   //写方式打开
    if(fd == -1){
        perror("open error");
        exit(EXIT_FAILURE);
    }
    char buf[1024*4];
    int n = 0;
    while((n = read(infd,buf,1024*4))){
        write(fd,buf,n);
    }
    close(infd);
    close(fd);
    printf("write success\n");
    return 0;
}

读进程:

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

int main(int argc, char **argv)
{
    if(argc != 2){
        fprintf(stderr,"usage:%s desfile\n",argv[0]);
        exit(EXIT_FAILURE);
    }
    int outfd;
    outfd = open(argv[1],O_WRONLY|O_CREAT|O_TRUNC);   //写方式打开文件
    if(outfd == -1){
        perror("open error");
        exit(EXIT_FAILURE);
    }

    int fd ;
    fd = open("tmpfifo",O_RDONLY);  //读方式打开
    if(fd == -1){
        perror("open error");
        exit(EXIT_FAILURE);
    }
    char buf[1024*4];
    int n = 0;
    while((n = read(fd,buf,1024*4))){       //从管道中读取数据
        write(outfd,buf,n);                 //写入到文件中
    }
    close(fd);
    close(outfd);
    unlink("tmpfifo");
    printf("read success\n");
    return 0;
}

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值