Question

I usually think of UNIX pipes as a quick and dirty way to interact with the console, doing things such as:

ls | grep '\.pdf$' #list all pdfs

I understand that it's possible to use create named pipes using mkfifo and mknod.

Are named pipes still used significantly today, or are they a relic of the past?

Was it helpful?

Solution

They are still used, although you might not notice. As a first-class file-system object provided by the operating system, a named pipe can be used by any program, regardless of what language it is written in, that can read and write to the file system for interprocess communication.

Specific to bash (and other shells), process substitution can be implemented using named pipes, and on some platforms that may be how it actually is implemented. The following

command < <( some_other_command )

is roughly identical to

mkfifo named_pipe
some_other_command > named_pipe &
command < named_pipe

and so is useful for things like POSIX-compliant shell code, which does not recognize process substitution.


And it works in the other direction: command > >( some_other_command ) is

mkfifo named_pipe
some_other_command < named_pipe &
command > named_pipe

OTHER TIPS

pianobar, the command line Pandora Radio player, uses a named pipe to provide a mechanism for arbitrary control input. If you want to control Pianobar with another app, such as PianoKeys, you set it up to write command strings to the FIFO file, which piano bar watches for incoming commands.

I wrote a MAMP stack app in college that we used to manage users in an LDAP database on a file server. The PHP scripts would write commands to a FIFO that a shell script running in launchd would read from and interact with the LDAP database. I had no idea what I was doing, so I don’t know if there was a better or more correct way to do it, but it worked great at the time.

Named pipes are useful where a program would expect a path to a file as an argument as opposed to being willing to read/write with stdin and stdout, though very modern versions of bash can get around this with < ( foo ) it is still sometimes useful to have a file like object which is usable as a pipe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top