The mkfifo function takes 2 arguments, path and mode. But I don't know what is the format of the path that it uses. I am writing a small program to create a named pipe and as path in the mkfifo
. Using /home/username/Documents
for example, but it always returns -1 with the message Error creating the named pipe.: File exists
.
I have checked this dir a lot of times and there is no pipe inside it. So I am wondering what is the problem. The mode I use in mkfifo is either 0666 or 0777.
To create a FIFO(named pipe) and use it in Python, you can use the os. mkfifo(). But mkfifo fails with File exists exception if file already exists. In order to avoid that, you can put it in a try-except block.
If successful, mkfifo() returns 0. If unsuccessful, mkfifo() does not create a FIFO file, returns -1, and sets errno to one of the following values: Error Code. Description.
A FIFO, also known as a named pipe, is a special file similar to a pipe but with a name on the filesystem. Multiple processes can access this special file for reading and writing like any ordinary file. Thus, the name works only as a reference point for processes that need to use a name in the filesystem.
You gave mkfifo()
the name of an existing directory, thus the error. You must give it the name of a non-existing file, e.g.
mkfifo("/home/username/Documents/myfifo", 0600);
The 'path' argument to mkfifo()
has to specify a full path, the directory and the filename that is.
Thus, it would be:
char *myfifo="/home/username/Documents/mypipe";
mkfifo(myfifo, 0777);
As a side note, you should avoid using octal permission bits and use named constants instead (from sys/stat.h
), so:
mkfifo(myfifo, S_IRWXU | S_IRWXG | S_IRWXO);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With