有名管道

1、         open函数flag以O_RDONLY打开,只能打开有名管道的读端,以O_RDONLY打开,只能打开有名管道的写端。

2、         open函数flag以O_RDWR方式打开,将打开管道的读写端,只有同时打开管道的读端和写端,有名管道才被真正的打开。纸打开读端或者写端,open函数会出现阻塞。

创建:

int main()
{
    if( mkfifo("./fifo",0666) < 0 )
    {
        perror("mkfifo error");
        exit(1);
    }

    exit(0);
}

写入:

int main()
{
    int fd;
    int bytes_write;
    char buf[100] = "hello,fifo!";

    printf("open before!\n");
    fd = open("./fifo",O_WRONLY);
    printf("open after!\n");
    if( -1 == fd )
    {
        perror("open error");
        exit(1);
    }

    bytes_write = write(fd,buf,strlen(buf));
    if( -1 == bytes_write )
    {
        perror("write error");
        exit(1);
    }

    printf("bytes_write is %d\n",bytes_write);

    exit(0);
}


读取:

int main()
{
    int fd;
    char buf[100];
    int bytes_read;

    printf("open before!\n");
    fd = open("./fifo",O_RDWR);
    printf("open after!\n");
    if( -1 == fd )
    {
        perror("open error");
        exit(1);
    }
    printf("read before!\n");
    bytes_read = read(fd,buf,100);
    printf("read after!\n");
    if( -1 == bytes_read )
    {
        perror("read error");
        exit(1);
    }

    printf("bytes_read is %d\n",bytes_read);
    
    exit(0);

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值