首页 > 解决方案 > 为什么非阻塞地打开命名管道会返回无效的文件描述符?

问题描述

我试图巩固我对谁在何时以及为什么会阻止打开、写入和读取命名管道的理解。

下面的代码暗示用 . 打开命名管道是无效的O_WRONLY | O_NONBLOCK,但我不确定我的代码中是否有一些我不理解的错误,或者这是否通常是正确的。

// main.c

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

int main( int argc, char* argv[] )
{
  int wfd = open( "/tmp/foo", O_WRONLY | O_NONBLOCK );
  printf( "wfd[%d]\n", wfd );
  if ( wfd >= 0 )
  {
    int res = write( wfd, "0", 1 );
    printf( "write: [%d], errno[%d(%s)]\n", res, errno, strerror( errno ) );
    sleep(3);
    printf( "writer ending!\n" );
  }
  return 0;
}
> ls -l /tmp/foo 
prwxrwxrwx. 1 user user 0 Sep  4 10:35 /tmp/foo
> 
> gcc -g main.c && ./a.out 
wfd[-1]

问题:为什么打开命名管道会O_WRONLY | O_NONBLOCK返回无效的文件描述符?

我怀疑这与需要同时打开读取和写入端的管道有关,而我解决这个问题的老套方法(通过非阻塞地打开一端)由于这个原因失败了。但是我找不到任何支持该假设或以其他方式解释该观察结果的特定文档。

标签: clinuxpipenamed-pipes

解决方案


mkfifo(3) - Linux 手册页:

有关 FIFO 特殊文件的非阻塞处理,请参见fifo(7)

fifo(7) - Linux 手册页:

进程可以在非阻塞模式下打开 FIFO。在这种情况下,即使没有人在写端打开只读模式也会成功,除非另一端已经打开,否则只写模式会失败(没有这样的设备或地址)。ENXIO


推荐阅读