What is the difference between mkfifo() and mknod() while creating a named pipe?
I tried searching but couldn't get a satisfactory answer.
The mkfifo() function creates a new FIFO special file (FIFO) whose name is defined by path. A FIFO special file is a type of file with the property that data written to the file is read on a first-in-first-out basis.
mkfifo() creates a new FIFO special file, pathname. The file permission bits in mode are changed by the file creation mask of the process, and then used to set the file permission bits of the FIFO file being created. If pathname contains a symbolic link, mkfifo() fails.
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). So in terms of syscalls, mkfifo is actually shorthand for mknod .
The POSIX specification says that mkfifo () should be preferred. Otherwise, there is no difference between a FIFO created by mkfifo () and mknod (). Note that mknod () can be used to create other device types than just FIFOs.
Using mknod () in general, is not portable — it is a part of POSIX (despite a statement to the contrary in an earlier version of this answer). The POSIX specification says that mkfifo () should be preferred. Otherwise, there is no difference between a FIFO created by mkfifo () and mknod ().
Creating a FIFO file: In order to create a FIFO file, a function calls i.e. mkfifo is used. int mkfifo(const char *pathname, mode_t mode); mkfifo() makes a FIFO special file with name pathname. Here mode specifies the FIFO’s permissions. It is modified by the process’s umask in the usual way: the permissions of the created file are (mode & ~umask).
Usually a named pipe appears as a file and generally processes attach to it for inter-process communication. A FIFO file is a special kind of file on the local storage which allows two or more processes to communicate with each other by reading/writing to/from this file. A FIFO special file is entered into the filesystem by calling mkfifo () in C.
Using mkfifo()
is standardized and portable. Using mknod()
in general, is not portable — it is a part of POSIX (despite a statement to the contrary in an earlier version of this answer). The POSIX specification says that mkfifo()
should be preferred. Otherwise, there is no difference between a FIFO created by mkfifo()
and mknod()
.
Note that mknod()
can be used to create other device types than just FIFOs. It can create block special and character special devices. Once upon a very (very, very) long time ago, mknod()
was used to create directories too — in the days before there was a mkdir()
or rmdir()
system call. After creating the directory, you had to use link()
twice to create the .
and ..
entries in the new directory. (And you had to have root privileges to use it, so the mkdir
and rmdir
commands were SUID root.) File systems are a lot more reliable nowadays because that's no longer part of the game.
Reference: Version 7 Unix — circa 1979.
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