Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 887
Spyros Avatar asked Oct 23 '12 22:10

Spyros


People also ask

How do you create a named pipe in Python?

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.

What does mkfifo function returns on successful creation of a named pipe?

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.

What is named pipe file in Linux?

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.


2 Answers

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);
like image 157
Olaf Dietsche Avatar answered Oct 03 '22 13:10

Olaf Dietsche


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);
like image 38
Michał Górny Avatar answered Oct 03 '22 14:10

Michał Górny