25

I've used the mkfifo <file> command to create named FIFOs, where one process writes to the file, and another process reads from the file.

Now, I know the mknod command is able to create named pipes. Are these named pipes equivalent to the FIFOs created by mkfifo, or do they have different features?

2 Answers 2

31

Yes, it's equivalent, but obviously only if you tell mknod to actually create a FIFO, and not a block or character device (rarely done these days as devtmpfs/udev does it for you).

mkfifo foobar
# same difference
mknod foobar p

In strace it's identical for both commands:

mknod("foobar", S_IFIFO|0666)           = 0

So in terms of syscalls, mkfifo is actually shorthand for mknod.

The biggest difference, then, is in semantics. With mkfifo you can create a bunch of FIFOs in one go:

mkfifo a b c

With mknod, since you have to specify the type, it only ever accepts one argument:

# wrong:
$ mknod a b c p
mknod: invalid major device number ‘c’
# right:
mknod a p
mknod b p
mknod c p

In general, mknod can be difficult to use correctly. So if you want to work with FIFO, stick to mkfifo.

5
  • 16
    while the OP certainly doesn't care, since the Q is not tagged [linux], notice that on BSD mkfifo(2) really is a separate system call from mknod(2) (but it'll end up doing exactly the same thing as mknod(S_FIFO)).
    – user313992
    Jul 28, 2019 at 14:30
  • @frostschutz - thank you for an excellent answer. So just to clarify things. mkfifo and mknod are actually programs using the mknod system call (didn't knew of that system call before today) to create a FIFO. You use the terms "FIFO" and "named" interchangeably, I guess. Are they the same thing? Bi-directional named pipes are implemented by means of Unix domain sockets, right?
    – Shuzheng
    Jul 29, 2019 at 12:21
  • Yes, "named pipe" and FIFO usually refers to the same thing (in the context of pipes - FIFO is a concept that exists outside of pipes as well). A socket is a different beast altogether, some interesting answers to that here: unix.stackexchange.com/q/75904/30851 Jul 29, 2019 at 12:28
  • @frostschutz - thank you. I guess what confused me most is that, when I think of named pipes, I also think of bi-directional pipes - and a FIFO is definitely not bi-directional (AFAIK).
    – Shuzheng
    Jul 30, 2019 at 9:07
  • @Shuzheng a FIFO does not have any rules on direction really. It's really just read and write. There can be any number of readers and writers, but anything written can only be read once, so it's unclear who would get the data in the end. Jul 30, 2019 at 9:44
19

They are equivalent except at the extreme edges of portability. mknod ... p was originally the only way to create named pipes, but POSIX chose to omit it and invent mkfifo instead, presumably because named pipes are an inherently more portable concept than all that other stuff mknod can do with devices and their major and minor numbers. The mknod system call was also left out of the early verions of POSIX.

So, for portability to ancient UNIX, mknod ... p is better. For modern systems, mkfifo is slightly better, although it's pretty unlikely that you'll find an actual modern unix where mknod ... p doesn't work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .