【发布时间】:2015-06-17 16:47:43
【问题描述】:
我有一个示例程序:
int main()
{
const char* fn = "/tmp/tmpfifo";
int i = mkfifo(fn, 0666);
int fd = open(fn, O_RDONLY | O_NONBLOCK);
int flags = fcntl(fd, F_GETFL);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
char buf[1024];
int rd= read(fd, buf, 100);
cout << rd << endl;
remove(fn);
return 0;
}
似乎在从文件描述符中删除非阻塞标志后,read 调用应该阻塞,直到将某些内容写入 FIFO,但我的程序总是在没有阻塞的情况下运行并且 rd=0 结果。你能解释一下这种行为吗?谢谢!
【问题讨论】:
-
看起来像 C++ 而不是 C。见 stackoverflow.com/questions/2784500/…
-
c++、c 的区别在这里重要吗?
-
当您从 fifo 读取结果为
rd = 0时,errno的值是否有任何变化? -
cout << rd << endl;是纯 C++,但一切都在上一个链接 stackoverflow.com/questions/2784500/… 中进行了解释 -
是的,可以很容易地用
printf替换,并且与讨论无关。
标签: c linux io nonblocking fcntl