FIFO – Are Named Pipe Created by mknod and FIFO Created by mkfifo Equivalent?

fifo

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?

Best Answer

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.