zoukankan      html  css  js  c++  java
  • 有名管道的非阻塞设置

    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 ", 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 ", 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;  
  • 相关阅读:
    Puzzle, ACM/ICPC World Finals 1993, UVa227
    Puzzle, ACM/ICPC World Finals 1993, UVa227
    All in All, UVa 10340
    All in All, UVa 10340
    Box, ACM/ICPC NEERC 2004, UVa1587
    Box, ACM/ICPC NEERC 2004, UVa1587
    动态文本输出
    形态分析法
    粒子系统
    思维
  • 原文地址:https://www.cnblogs.com/dpf-learn/p/7700977.html
Copyright © 2011-2022 走看看