c - 为什么非阻塞地打开命名管道会返回无效的文件描述符?
问题描述
我试图巩固我对谁在何时以及为什么会阻止打开、写入和读取命名管道的理解。
下面的代码暗示用 . 打开命名管道是无效的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
返回无效的文件描述符?
我怀疑这与需要同时打开读取和写入端的管道有关,而我解决这个问题的老套方法(通过非阻塞地打开一端)由于这个原因失败了。但是我找不到任何支持该假设或以其他方式解释该观察结果的特定文档。
解决方案
推荐阅读
- java - 编写整个字符串的Java方法
- javascript - 向网页添加计时器
- regex - 使用带有 ARRAYFORMULA 的 REGEXEXTRACT 提取列值
- r - 带有Access DB的R中的ODBC是否存在语言问题?
- java - 如何将 2 excel 中的数据与 java 进行比较
- laravel - 如何重定向当前页面以使用 guzzle (laravel) 发布响应
- java - 如何在创建的目录中下载文件
- html - 替换 Sphinx 标准 HTML 输出中的“文档”后缀
- angular - 将字符串 (API) 添加为单个变量
- amazon - 什么是最好的 IMDB REST API?我可以从哪里得到它?