Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between mkfifo() and mknod()

Tags:

c

linux

pipe

What is the difference between mkfifo() and mknod() while creating a named pipe?

I tried searching but couldn't get a satisfactory answer.

like image 959
babybear Avatar asked Mar 25 '17 23:03

babybear


People also ask

What is the use of mkfifo?

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.

What is mkfifo?

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.

Is mkfifo a Syscall?

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 .

What is the difference between mkfifo () and mknod () in POSIX?

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.

Is mknod () 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 ().

How to create a FIFO file in Linux?

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).

What is the difference between a pipe and a FIFO file?

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.


1 Answers

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.

like image 64
Jonathan Leffler Avatar answered Oct 21 '22 23:10

Jonathan Leffler