有名管道的非阻塞设置

  1. #include<sys/types.h>  
  2. #include<sys/stat.h>  
  3. #include<errno.h>  
  4. #include<string.h>  
  5. #include<stdio.h>  
  6. #include<unistd.h>  
  7. #include<fcntl.h>  
  8. int  
  9. main (void)  
  10. {  
  11.   char buf[1024];  
  12.   char fn[] = "myfifo";  
  13.   int ret = mkfifo (fn, S_IRUSR | S_IWUSR);  
  14.   if (ret == -1)  
  15.     {  
  16.       printf ("mkfifo error:%s\n", strerror (errno));  
  17.       return 1;  
  18.     }  
  19.   int rd = open (fn, O_RDONLY | O_NONBLOCK);  
  20.   int fd = open (fn, O_WRONLY | O_NONBLOCK, S_IRWXU);//此处注意,必须先打开读,否则会打不开fifo,根据论坛提到的如果设置非阻塞,必须先open一个fifo读。  
  21.   if (fd == -1)  
  22.     {  
  23.       printf ("open error:%s\n", strerror (errno));  
  24.       return 1;  
  25.     }  
  26.   write (fd, "hello", 5);  
  27.   read (rd, buf, 5);  
  28.   write (1, buf, 5);//向设备1写入,即显示屏  
  29.   close (fd);  
  30.   unlink (fn);  
  31.   return 0;  

转载于:https://www.cnblogs.com/dpf-learn/p/7700977.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值