0

I have two problems with my code.

First problem is that once I create a fifo, I don't know how to add a condition in the code so in future running, if the fifo exists - then just don't create it. Tried to google, "access" didn't work (it just stucked on there and didn't continue).

My second problem is, that the code is stuck in the "open("MyFifo..." line, even if it's the first time after I created the fifo (meaning I just created the fifo, mkfifo succeed, and I reach open() line - it's stuck there).

//create new fifo
if(mkfifo("myFifo",0666)<0)
{
    perror("fifo creation failed.");
    exit(1);
}

//get fifo fd
if((fd=open("myFifo",O_RDONLY))==-1)
{
    perror("failed opening fifo.");
    exit(1);
}

Any ideas what am I doing wrong?

4

1 回答 1

1

您要查找的函数调用是stat. 如果文件存在,它将struct stat用文件属性(如修改时间)填充一个。如果文件不存在,stat()将返回 -1 并errno设置为EACCESS.


mkfifo手册页说,

打开一个 FIFO 进行读取通常会阻塞,直到某个其他进程打开同一个 FIFO 进行写入,反之亦然。

open()停止挂起,请启动另一个首先写入 FIFO 的进程。如果其他进程先打开 FIFO 进行写入,则读取程序根本不会挂起open()

于 2013-05-12T20:46:48.923 回答