Error mkfifo () ---> "Error creating named pipe .: File exists"

The mkfifo function takes 2 arguments, a path and a mode. But I do not know what the format of the path that it uses is. I am writing a small program to create a named pipe and as a path in mkfifo . For example, using /home/username/Documents , but it always returns -1 with the message Error creating the named pipe.: File exists .

I checked this directory many times and there is no pipe inside it. So I wonder what the problem is. The mode I use in mkfifo is 0666 or 0777.

+6
source share
3 answers

You gave mkfifo() name of an existing directory, thus an error. You must indicate to it a nonexistent file:

 mkfifo("/home/username/Documents/myfifo", 0600); 
+5
source

The path argument for mkfifo() must specify the full path, directory, and file name.

So it will be:

 char *myfifo="/home/username/Documents/mypipe"; mkfifo(myfifo, 0777); 

As a side note, you should avoid using octal resolution bits and use named constants (from sys/stat.h ) sys/stat.h , so:

 mkfifo(myfifo, S_IRWXU | S_IRWXG | S_IRWXO); 
+2
source

Use remove() to remove the file either at the end of the program or at the beginning of the program to make sure that it does not exist the next time it is created.

0
source