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


运行到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;
}
路径是正确的,但是控制台没有任何输出。谷歌又跳不了页面,真急死俺啦。。。。

C++/VC 程序开发 异常处理

乱入菌可好? 13 years, 6 months ago


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

一只麻袋猫 answered 13 years, 6 months ago


学linux开发请认真看书, 自己瞎摸既学不好又浪费时间。

ONONBLOCK
When opening a FIFO with O
RDONLY or OWRONLY 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.

阿叔洗内裤 answered 13 years, 6 months ago

Your Answer