读书人

运行到open()函数卡住怎么处理

发布时间: 2012-10-21 09:00:07 作者: rapoo

运行到open()函数卡住,怎么办?
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
int fd;
if((fd = open("fifo1",O_WRONLY)) < 0) {
perror("打开失败。");
exit(1);
}
printf("%d",fd);
write(fd,"nihao",6);
close(fd);
return 0;
}
路径是正确的,但是控制台没有任何输出。谷歌又跳不了页面,真急死俺啦。。。。

[解决办法]
学linux开发请认真看书, 自己瞎摸既学不好又浪费时间。

O_NONBLOCK
When opening a FIFO with O_RDONLY or O_WRONLY set:

* If O_NONBLOCK is set, an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no
process currently has the file open for reading.

* If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until a thread opens the file for writing. An open()
for writing-only shall block the calling thread until a thread opens the file for reading.
[解决办法]
对的

探讨
我解决啦。happy
2楼正解。
写进程会阻塞到有一个读进程来读这个FIFO管道。就是没有进程来读文件,则写进程会阻塞在open语句。
所以要两个程序一起运行就能看到结果啦。
附上两段代码,要一起运行,一读,一写。
一:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
……

读书人网 >C语言

热点推荐