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?
The function call you’re looking for is stat
. If the file exists, it will fill a struct stat
with file attributes such as modification time. If the file does not exist, stat()
will return -1 and errno
will be set to EACCESS
.
The mkfifo
man page says,
Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.
To get open()
to stop hanging, start another process that writes to the FIFO first. If the other process opens the FIFO for writing first, the reading program won’t hang at all on open()
.