码迷,mamicode.com
首页 > 其他好文 > 详细

有名管道的非阻塞设置

时间:2017-10-20 20:12:19      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:str   bsp   erro   return   pen   必须   usr   star   turn   

  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;  

有名管道的非阻塞设置

标签:str   bsp   erro   return   pen   必须   usr   star   turn   

原文地址:http://www.cnblogs.com/dpf-learn/p/7700977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!