为什么打开一个mkfifo子-ED管时,我的程序挂起? [英] Why does my program hang when opening a mkfifo-ed pipe?

查看:136
本文介绍了为什么打开一个mkfifo子-ED管时,我的程序挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用mkfifo子来创建一个命名管道。
然后我用下面的程序将其打开。但是,该程序挂在线的fopen。是不是有什么错在这里?

  INT主(INT ARGC,字符** argv的){
字符*线=你好,世界!;
FILE *计划生育=的fopen(/ tmp目录/ myFIFO,RW);
fprintf中(FP,线);
FCLOSE(FP);
返回0;
}


解决方案

尝试通过W的模式给fopen RW,而不是。你可能不想读写到FIFO中相同的处理。

在FIFO的打开方式一些背景资料:

使用打开为只读,而无需指定 O_NONBLOCK ,直到另一个进程打开FIFO写入导致块。同样,打开(不指定 O_NONBLOCK )以只写阻塞,直到另一个进程打开FIFO读

如果您通过 O_NONBLOCK 打开,开口只读立即返回,但开口只写返回一个错误与错误号 ENXIO ,除非另一个进程具有FIFO开放阅读。

由理查德·史蒂文斯UNIX环境高级编程信息。

I use mkfifo to create a named pipe. Then I use the following program to open it. However, the program hangs at the line "fopen". Is there something wrong here?

int main(int argc, char** argv) {
char* line = "hello, world!";
FILE* fp = fopen("/tmp/myFIFO", "rw");
fprintf(fp, line);
fclose(fp);
return 0;
}

解决方案

Try passing "w" as the mode to fopen instead of "rw". You probably don't want to both read and write to the FIFO in the same process.

Some background on how FIFOs are opened:

Using open for read-only without specifying O_NONBLOCK causes a block until another process opens the FIFO for writing. Similarly, an open (without specifying O_NONBLOCK) for write-only blocks until another process opens the FIFO for reading.

If you pass O_NONBLOCK to open, opening for read-only returns immediately, but opening for write-only returns an error with an errno of ENXIO unless another process has the FIFO open for reading.

Info from "Advanced Programming in the UNIX Environment" by W. Richard Stevens.

这篇关于为什么打开一个mkfifo子-ED管时,我的程序挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆