7

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.

wallyk
  • 56,922
  • 16
  • 83
  • 148
Spyros
  • 682
  • 7
  • 18
  • 37

2 Answers2

8

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);
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
2

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);
Michał Górny
  • 18,713
  • 5
  • 53
  • 76
  • What's wrong with octal permissions? They are extremely standard and universal. – wallyk Oct 23 '12 at 22:11
  • 2
    And octal permissions are a whole heap more succinct, too! However, in theory, you're supposed to use the S_Iwxyz names. In practice, you'll be fine almost everywhere using octal instead. – Jonathan Leffler Oct 23 '12 at 22:15
  • 3
    @wallyk: The problem with octal permissions is that they are *not* standard. POSIX doesn't require compliant implementations to use any specific values. And using symbolic constants has another benefit too -- you can use `#ifdef` to check whether a particular mode is supported at all. – Michał Górny Oct 24 '12 at 10:26