首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2012-10-21 
运行到open()函数卡住,怎么办?代码如下:#include stdio.h#include stdlib.h#include sys/types.h#in

运行到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>
……

热点排行