Named pipes
In computing, a named pipe (also known as a FIFO for its behavior) is an extension to the traditional
pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication.
The concept is also found in Microsoft Windows, although the semantics differ substantially. A traditional
pipe is "unnamed" because it exists anonymously and persists only for as long as the process is running.
A named pipe is system-persistent and exists beyond the life of the process and must be deleted once
it is no longer being used. Processes generally attach to the named pipes (usually appearing as a file)
to perform inter-process communication (IPC).
A named pipe is a named, one-way or duplex pipe for communication between the pipe server and one
or more pipe clients. All instances of a named pipe share the same pipe name, but each instance has
its own buffers and handles, and provides a separate conduit for client/server communication. The
use of instances enables multiple pipe clients to use the same named pipe simultaneously.
Any process can access named pipes, subject to security checks, making named pipes an easy form
of communication between related or unrelated processes.
Any process can act as both a server and a client, making peer-to-peer communication possible. As
used here, the term pipe server refers to a process that creates a named pipe, and the term pipe
client refers to a process that connects to an instance of a named pipe.
In Unix, instead of a conventional, unnamed, shell pipeline, a named pipeline makes use of the filesystem.
It is explicitly created using mkfifo() or mknod(), and two separate processes can access the pipe by
name as a regular file — one process can open it as a reader, and the other as a writer.
For example, one can create a pipe and set up gzip to compress things piped to it:
mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz &
In a separate process shell, independently, one could send the data to be compressed:
cat file > my_pipe
The named pipe can be deleted just like any file:
rm my_pipe
A named pipe can be used to transfer information from one application to another without the use of
an intermediate temporary file. For example you can pipe the output of gzip into a named pipe like so:
mkfifo --mode=0666 /tmp/namedPipe
gzip --stdout -d file.gz > /tmp/namedPipe
Then load the uncompressed data into a MySQL table[1] like so:
LOAD DATA INFILE '/tmp/namedPipe' INTO TABLE tableName;
Without this named pipe one would need to write out the entire uncompressed version of file.gz before
loading it into MySQL. Writing the temporary file is both time consuming and results in more I/O and
less free space on the hard drive.
PostgreSQL's command line terminal, psql, also supports loading data from named pipes.[2]
Named pipes (fifo) have four three advantages I can think of:
- you don't have to start the reading/writing processes at the same time
- you can have multiple readers/writers which do not need common ancestry
- as a file you can control ownership and permissions
they are bi-directional, unnamed pipes may be unidirectional
*
*) Think of a standard shell |
pipeline which is
unidirectional, several shells (ksh
, zsh
, and bash
)
also offer
coprocesses which allow bi-directional communication. POSIX treats
pipes as half-duplex (i.e. each side can only read or write), the pipe()
system call returns two file handles and you may be required to treat one as
read-only and the other as write-only. Some (BSD) systems support read and write
simultaneously (not forbidden by POSIX), on others you would need two pipes, one
for each direction. Check your pipe()
, popen()
and
possibly popen2()
man pages. The undirectionality may not be
dependent on whether the pipe is named or not, though on Linux 2.6 it is
dependent.
(Updated, thanks to feedback from Stephane Chazelas)
So one immediately obvious task you cannot achieve with an unnamed pipe is a
conventional client/server application.
The last (stricken) point above about unidirectional pipes is relevant on Linux,
POSIX (see
popen()
) says that a pipe need only be readable or
writeable, on
Linux they are unidirectional. See Understanding The Linux Kernel (3rd Ed.
O'Reilly) for Linux-specific details (p787). Other OS's offer bidirectional (unnamed)
pipes.
As an example, Nagios uses a fifo for its
command file. Various external processes (CGI scripts, external checks,
NRPE etc) write commands/updates to this fifo and these are processed by the persistent
Nagios process.
Named pipes have features not unlike TCP connections, but there are important
differences. Because a fifo has a persistent filesystem name you can write to it even
when there is no reader, admittedly the writes will block (without async or non-blocking
I/O), though you won't loose data if the receiver isn't started (or is being restarted).
For reference, see also
Unix
domain sockets, and the answer to
this
Stackoverflow question which summarises the main
IPC methods, and
this one which talks about popen()
share|improve
this answer
Unnamed or anonymous pipes provide a means of one-to-one, one-way
interprocess communication between different processes that are
related by either a parent-child relationship, or by being children
of a common parent that provides the pipe, such as a shell process.
Because the processes are related, the association of file
descriptors to the pipe can be implicit and does not require an
object with a name that is external to the processes. An unnamed
pipe exists only as long as the processess that use it maintain
open file descriptors to the pipe. When the processes exit and the
OS closes all of the file descriptors associated with the
processes, the unnamed pipe is closed. Named pipes are in fact
FIFO's. These are persistent objects represented by nodes in the
file system. A named pipe provides many-to-many, two-way
communication between one or more processes that are not
necessarily related and do not need to exist at the same time. The
file name of the pipe serves as an address or contract between the
processes for communication. If only one process writes to a named
pipe and one other process reads from the named pipe, then the
named pipe behaves in the same way as an unnamed pipe between the
two related processes.
So the short answer is that you need a named pipe for
communication between unrelated processes that might not exist at
the same time.
|
Softpanorama Recommended
c - Bi-directional inter-process communication using two pipes - Stack Overflow
What are the advantages of using named pipe over unnamed pipe - Unix & Linux Stack Exchange
Society
Groupthink :
Two Party System
as Polyarchy :
Corruption of Regulators :
Bureaucracies :
Understanding Micromanagers
and Control Freaks : Toxic Managers :
Harvard Mafia :
Diplomatic Communication
: Surviving a Bad Performance
Review : Insufficient Retirement Funds as
Immanent Problem of Neoliberal Regime : PseudoScience :
Who Rules America :
Neoliberalism
: The Iron
Law of Oligarchy :
Libertarian Philosophy
Quotes
War and Peace
: Skeptical
Finance : John
Kenneth Galbraith :Talleyrand :
Oscar Wilde :
Otto Von Bismarck :
Keynes :
George Carlin :
Skeptics :
Propaganda : SE
quotes : Language Design and Programming Quotes :
Random IT-related quotes :
Somerset Maugham :
Marcus Aurelius :
Kurt Vonnegut :
Eric Hoffer :
Winston Churchill :
Napoleon Bonaparte :
Ambrose Bierce :
Bernard Shaw :
Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient
markets hypothesis :
Political Skeptic Bulletin, 2013 :
Unemployment Bulletin, 2010 :
Vol 23, No.10
(October, 2011) An observation about corporate security departments :
Slightly Skeptical Euromaydan Chronicles, June 2014 :
Greenspan legacy bulletin, 2008 :
Vol 25, No.10 (October, 2013) Cryptolocker Trojan
(Win32/Crilock.A) :
Vol 25, No.08 (August, 2013) Cloud providers
as intelligence collection hubs :
Financial Humor Bulletin, 2010 :
Inequality Bulletin, 2009 :
Financial Humor Bulletin, 2008 :
Copyleft Problems
Bulletin, 2004 :
Financial Humor Bulletin, 2011 :
Energy Bulletin, 2010 :
Malware Protection Bulletin, 2010 : Vol 26,
No.1 (January, 2013) Object-Oriented Cult :
Political Skeptic Bulletin, 2011 :
Vol 23, No.11 (November, 2011) Softpanorama classification
of sysadmin horror stories : Vol 25, No.05
(May, 2013) Corporate bullshit as a communication method :
Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000):
the triumph of the US computer engineering :
Donald Knuth : TAoCP
and its Influence of Computer Science : Richard Stallman
: Linus Torvalds :
Larry Wall :
John K. Ousterhout :
CTSS : Multix OS Unix
History : Unix shell history :
VI editor :
History of pipes concept :
Solaris : MS DOS
: Programming Languages History :
PL/1 : Simula 67 :
C :
History of GCC development :
Scripting Languages :
Perl history :
OS History : Mail :
DNS : SSH
: CPU Instruction Sets :
SPARC systems 1987-2006 :
Norton Commander :
Norton Utilities :
Norton Ghost :
Frontpage history :
Malware Defense History :
GNU Screen :
OSS early history
Classic books:
The Peter
Principle : Parkinson
Law : 1984 :
The Mythical Man-Month :
How to Solve It by George Polya :
The Art of Computer Programming :
The Elements of Programming Style :
The Unix Hater’s Handbook :
The Jargon file :
The True Believer :
Programming Pearls :
The Good Soldier Svejk :
The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society :
Ten Commandments
of the IT Slackers Society : Computer Humor Collection
: BSD Logo Story :
The Cuckoo's Egg :
IT Slang : C++ Humor
: ARE YOU A BBS ADDICT? :
The Perl Purity Test :
Object oriented programmers of all nations
: Financial Humor :
Financial Humor Bulletin,
2008 : Financial
Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related
Humor : Programming Language Humor :
Goldman Sachs related humor :
Greenspan humor : C Humor :
Scripting Humor :
Real Programmers Humor :
Web Humor : GPL-related Humor
: OFM Humor :
Politically Incorrect Humor :
IDS Humor :
"Linux Sucks" Humor : Russian
Musical Humor : Best Russian Programmer
Humor : Microsoft plans to buy Catholic Church
: Richard Stallman Related Humor :
Admin Humor : Perl-related
Humor : Linus Torvalds Related
humor : PseudoScience Related Humor :
Networking Humor :
Shell Humor :
Financial Humor Bulletin,
2011 : Financial
Humor Bulletin, 2012 :
Financial Humor Bulletin,
2013 : Java Humor : Software
Engineering Humor : Sun Solaris Related Humor :
Education Humor : IBM
Humor : Assembler-related Humor :
VIM Humor : Computer
Viruses Humor : Bright tomorrow is rescheduled
to a day after tomorrow : Classic Computer
Humor
The Last but not Least Technology is dominated by
two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt.
Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org
was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP)
without any remuneration. This document is an industrial compilation designed and created exclusively
for educational use and is distributed under the Softpanorama Content License.
Original materials copyright belong
to respective owners. Quotes are made for educational purposes only
in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains
copyrighted material the use of which has not always been specifically
authorized by the copyright owner. We are making such material available
to advance understanding of computer science, IT technology, economic, scientific, and social
issues. We believe this constitutes a 'fair use' of any such
copyrighted material as provided by section 107 of the US Copyright Law according to which
such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free)
site written by people for whom English is not a native language. Grammar and spelling errors should
be expected. The site contain some broken links as it develops like a living tree...
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or
referenced source) and are
not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness
of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be
tracked by Google please disable Javascript for this site. This site is perfectly usable without
Javascript.
Last modified: March 12, 2019