<s>
In	O
Unix-like	B-Operating_System
computer	B-General_Concept
operating	I-General_Concept
systems	I-General_Concept
,	O
a	O
pipeline	B-General_Concept
is	O
a	O
mechanism	O
for	O
inter-process	B-Operating_System
communication	I-Operating_System
using	O
message	O
passing	O
.	O
</s>
<s>
A	O
pipeline	B-General_Concept
is	O
a	O
set	O
of	O
processes	O
chained	O
together	O
by	O
their	O
standard	B-Operating_System
streams	I-Operating_System
,	O
so	O
that	O
the	O
output	O
text	O
of	O
each	O
process	B-Operating_System
(	O
stdout	O
)	O
is	O
passed	O
directly	O
as	O
input	O
(	O
stdin	O
)	O
to	O
the	O
next	O
one	O
.	O
</s>
<s>
The	O
second	O
process	B-Operating_System
is	O
started	O
as	O
the	O
first	O
process	B-Operating_System
is	O
still	O
executing	O
,	O
and	O
they	O
are	O
executed	O
concurrently	B-Operating_System
.	O
</s>
<s>
The	O
concept	O
of	O
pipelines	B-General_Concept
was	O
championed	O
by	O
Douglas	O
McIlroy	O
at	O
Unix	B-Application
's	O
ancestral	O
home	O
of	O
Bell	O
Labs	O
,	O
during	O
the	O
development	O
of	O
Unix	B-Application
,	O
shaping	O
its	O
toolbox	B-Operating_System
philosophy	I-Operating_System
.	O
</s>
<s>
It	O
is	O
named	O
by	O
analogy	O
to	O
a	O
physical	O
pipeline	B-General_Concept
.	O
</s>
<s>
A	O
key	O
feature	O
of	O
these	O
pipelines	B-General_Concept
is	O
their	O
"	O
hiding	O
of	O
internals	O
"	O
(	O
Ritchie	O
&	O
Thompson	O
,	O
1974	O
)	O
.	O
</s>
<s>
This	O
article	O
is	O
about	O
anonymous	B-Operating_System
pipes	I-Operating_System
,	O
where	O
data	O
written	O
by	O
one	O
process	B-Operating_System
is	O
buffered	O
by	O
the	O
operating	B-General_Concept
system	I-General_Concept
until	O
it	O
is	O
read	O
by	O
the	O
next	O
process	B-Operating_System
,	O
and	O
this	O
uni-directional	O
channel	O
disappears	O
when	O
the	O
processes	O
are	O
completed	O
.	O
</s>
<s>
This	O
differs	O
from	O
named	B-Operating_System
pipes	I-Operating_System
,	O
where	O
messages	O
are	O
passed	O
to	O
or	O
from	O
a	O
pipe	O
that	O
is	O
named	O
by	O
making	O
it	O
a	O
file	O
,	O
and	O
remains	O
after	O
the	O
processes	O
are	O
completed	O
.	O
</s>
<s>
The	O
standard	O
shell	B-Operating_System
syntax	O
for	O
anonymous	B-Operating_System
pipes	I-Operating_System
is	O
to	O
list	O
multiple	O
commands	O
,	O
separated	O
by	O
vertical	O
bars	O
(	O
"	O
pipes	B-Operating_System
"	O
in	O
common	O
Unix	B-Application
verbiage	O
)	O
:	O
</s>
<s>
The	O
command	O
ls	B-Application
-l	O
is	O
executed	O
as	O
a	O
process	B-Operating_System
,	O
the	O
output	O
(	O
stdout	O
)	O
of	O
which	O
is	O
piped	O
to	O
the	O
input	O
(	O
stdin	O
)	O
of	O
the	O
process	B-Operating_System
for	O
grep	B-Application
key	O
;	O
and	O
likewise	O
for	O
the	O
process	B-Operating_System
for	O
less	B-Application
.	O
</s>
<s>
Each	O
process	B-Operating_System
takes	O
input	O
from	O
the	O
previous	O
process	B-Operating_System
and	O
produces	O
output	O
for	O
the	O
next	O
process	B-Operating_System
via	O
standard	B-Operating_System
streams	I-Operating_System
.	O
</s>
<s>
Each	O
|	O
tells	O
the	O
shell	B-Operating_System
to	O
connect	O
the	O
standard	O
output	O
of	O
the	O
command	O
on	O
the	O
left	O
to	O
the	O
standard	O
input	O
of	O
the	O
command	O
on	O
the	O
right	O
by	O
an	O
inter-process	B-Operating_System
communication	I-Operating_System
mechanism	O
called	O
an	O
(	O
anonymous	O
)	O
pipe	O
,	O
implemented	O
in	O
the	O
operating	B-General_Concept
system	I-General_Concept
.	O
</s>
<s>
Pipes	B-Operating_System
are	O
unidirectional	O
;	O
data	O
flows	O
through	O
the	O
pipeline	B-General_Concept
from	O
left	O
to	O
right	O
.	O
</s>
<s>
Below	O
is	O
an	O
example	O
of	O
a	O
pipeline	B-General_Concept
that	O
implements	O
a	O
kind	O
of	O
spell	B-Application
checker	I-Application
for	O
the	O
web	O
resource	O
indicated	O
by	O
a	O
URL	O
.	O
</s>
<s>
curl	B-Language
obtains	O
the	O
HTML	B-Language
contents	O
of	O
a	O
web	O
page	O
(	O
could	O
use	O
wget	O
on	O
some	O
systems	O
)	O
.	O
</s>
<s>
sed	B-Application
replaces	O
all	O
characters	O
(	O
from	O
the	O
web	O
page	O
's	O
content	O
)	O
that	O
are	O
not	O
spaces	O
or	O
letters	O
,	O
with	O
spaces	O
.	O
</s>
<s>
tr	B-Application
changes	O
all	O
of	O
the	O
uppercase	O
letters	O
into	O
lowercase	O
and	O
converts	O
the	O
spaces	O
in	O
the	O
lines	O
of	O
text	O
to	O
newlines	O
(	O
each	O
'	O
word	O
 '	O
is	O
now	O
on	O
a	O
separate	O
line	O
)	O
.	O
</s>
<s>
grep	B-Application
includes	O
only	O
lines	O
that	O
contain	O
at	O
least	O
one	O
lowercase	O
alphabetical	O
character	O
(	O
removing	O
any	O
blank	O
lines	O
)	O
.	O
</s>
<s>
sort	B-Application
sorts	O
the	O
list	O
of	O
'	O
words	O
 '	O
into	O
alphabetical	O
order	O
,	O
and	O
the	O
-u	O
switch	O
removes	O
duplicates	O
.	O
</s>
<s>
comm	B-Application
finds	O
lines	O
in	O
common	O
between	O
two	O
files	O
,	O
-23	O
suppresses	O
lines	O
unique	O
to	O
the	O
second	O
file	O
,	O
and	O
those	O
that	O
are	O
common	O
to	O
both	O
,	O
leaving	O
only	O
those	O
that	O
are	O
found	O
only	O
in	O
the	O
first	O
file	O
named	O
.	O
</s>
<s>
The	O
-	O
in	O
place	O
of	O
a	O
filename	O
causes	O
comm	B-Application
to	O
use	O
its	O
standard	O
input	O
(	O
from	O
the	O
pipe	O
line	O
in	O
this	O
case	O
)	O
.	O
</s>
<s>
sort	B-Application
/usr/share/dict/words	O
sorts	O
the	O
contents	O
of	O
the	O
words	O
file	O
alphabetically	O
,	O
as	O
comm	B-Application
expects	O
,	O
and	O
<	O
(	O
...	O
)	O
outputs	O
the	O
results	O
to	O
a	O
temporary	O
file	O
(	O
via	O
process	B-Device
substitution	I-Device
)	O
,	O
which	O
comm	B-Application
reads	O
.	O
</s>
<s>
less	B-Application
allows	O
the	O
user	O
to	O
page	O
through	O
the	O
results	O
.	O
</s>
<s>
All	O
widely	O
used	O
Unix	B-Application
shells	I-Application
have	O
a	O
special	O
syntax	O
construct	O
for	O
the	O
creation	O
of	O
pipelines	B-General_Concept
.	O
</s>
<s>
In	O
all	O
usage	O
one	O
writes	O
the	O
commands	O
in	O
sequence	O
,	O
separated	O
by	O
the	O
ASCII	B-Protocol
vertical	O
bar	O
character	O
|	O
(	O
which	O
,	O
for	O
this	O
reason	O
,	O
is	O
often	O
called	O
"	O
pipe	O
character	O
"	O
)	O
.	O
</s>
<s>
The	O
shell	B-Operating_System
starts	O
the	O
processes	O
and	O
arranges	O
for	O
the	O
necessary	O
connections	O
between	O
their	O
standard	B-Operating_System
streams	I-Operating_System
(	O
including	O
some	O
amount	O
of	O
buffer	B-General_Concept
storage	O
)	O
.	O
</s>
<s>
By	O
default	O
,	O
the	O
standard	O
error	O
streams	O
(	O
"	O
stderr	O
"	O
)	O
of	O
the	O
processes	O
in	O
a	O
pipeline	B-General_Concept
are	O
not	O
passed	O
on	O
through	O
the	O
pipe	O
;	O
instead	O
,	O
they	O
are	O
merged	O
and	O
directed	O
to	O
the	O
console	B-Device
.	O
</s>
<s>
In	O
the	O
csh	B-Operating_System
shell	B-Operating_System
,	O
for	O
instance	O
,	O
using	O
|&	O
instead	O
of	O
|	O
signifies	O
that	O
the	O
standard	O
error	O
stream	O
should	O
also	O
be	O
merged	O
with	O
the	O
standard	O
output	O
and	O
fed	O
to	O
the	O
next	O
process	B-Operating_System
.	O
</s>
<s>
The	O
Bash	B-Operating_System
shell	I-Operating_System
can	O
also	O
merge	O
standard	O
error	O
with	O
|&	O
since	O
version	O
4.0	O
or	O
using	O
2>	O
&1	O
,	O
as	O
well	O
as	O
redirect	O
it	O
to	O
a	O
different	O
file	O
.	O
</s>
<s>
In	O
the	O
most	O
commonly	O
used	O
simple	O
pipelines	B-General_Concept
the	O
shell	B-Operating_System
connects	O
a	O
series	O
of	O
sub-processes	O
via	O
pipes	B-Operating_System
,	O
and	O
executes	O
external	O
commands	O
within	O
each	O
sub-process	O
.	O
</s>
<s>
Thus	O
the	O
shell	B-Operating_System
itself	O
is	O
doing	O
no	O
direct	O
processing	O
of	O
the	O
data	O
flowing	O
through	O
the	O
pipeline	B-General_Concept
.	O
</s>
<s>
However	O
,	O
it	O
's	O
possible	O
for	O
the	O
shell	B-Operating_System
to	O
perform	O
processing	O
directly	O
,	O
using	O
a	O
so-called	O
mill	O
or	O
pipemill	O
(	O
since	O
a	O
while	O
command	O
is	O
used	O
to	O
"	O
mill	O
"	O
over	O
the	O
results	O
from	O
the	O
initial	O
command	O
)	O
.	O
</s>
<s>
As	O
all	O
components	O
of	O
a	O
pipe	O
are	O
run	O
in	O
parallel	O
,	O
a	O
shell	B-Operating_System
typically	O
forks	B-Language
a	O
subprocess	O
(	O
a	O
subshell	O
)	O
to	O
handle	O
its	O
contents	O
,	O
making	O
it	O
impossible	O
to	O
propagate	O
variable	O
changes	O
to	O
the	O
outside	O
shell	B-Operating_System
environment	O
.	O
</s>
<s>
To	O
remedy	O
this	O
issue	O
,	O
the	O
"	O
pipemill	O
"	O
can	O
instead	O
be	O
fed	O
from	O
a	O
here	O
document	O
containing	O
a	O
command	B-Device
substitution	I-Device
,	O
which	O
waits	O
for	O
the	O
pipeline	B-General_Concept
to	O
finish	O
running	O
before	O
milling	O
through	O
the	O
contents	O
.	O
</s>
<s>
Alternatively	O
,	O
a	O
named	B-Operating_System
pipe	I-Operating_System
or	O
a	O
process	B-Device
substitution	I-Device
can	O
be	O
used	O
for	O
parallel	O
execution	O
.	O
</s>
<s>
GNU	B-Operating_System
bash	I-Operating_System
also	O
has	O
a	O
option	O
to	O
disable	O
forking	O
for	O
the	O
last	O
pipe	O
component	O
.	O
</s>
<s>
Pipelines	B-General_Concept
can	O
be	O
created	O
under	O
program	O
control	O
.	O
</s>
<s>
The	O
Unix	O
pipe( )	O
system	B-Operating_System
call	I-Operating_System
asks	O
the	O
operating	B-General_Concept
system	I-General_Concept
to	O
construct	O
a	O
new	O
anonymous	B-Operating_System
pipe	I-Operating_System
object	O
.	O
</s>
<s>
This	O
results	O
in	O
two	O
new	O
,	O
opened	O
file	B-Application
descriptors	I-Application
in	O
the	O
process	B-Operating_System
:	O
the	O
read-only	O
end	O
of	O
the	O
pipe	O
,	O
and	O
the	O
write-only	O
end	O
.	O
</s>
<s>
The	O
pipe	O
ends	O
appear	O
to	O
be	O
normal	O
,	O
anonymous	O
file	B-Application
descriptors	I-Application
,	O
except	O
that	O
they	O
have	O
no	O
ability	O
to	O
seek	O
.	O
</s>
<s>
To	O
avoid	O
deadlock	B-Operating_System
and	O
exploit	O
parallelism	O
,	O
the	O
Unix	B-Operating_System
process	I-Operating_System
with	O
one	O
or	O
more	O
new	O
pipes	B-Operating_System
will	O
then	O
,	O
generally	O
,	O
call	O
fork( )	O
to	O
create	O
new	O
processes	O
.	O
</s>
<s>
Each	O
process	B-Operating_System
will	O
then	O
close	O
the	O
end(s )	O
of	O
the	O
pipe	O
that	O
it	O
will	O
not	O
be	O
using	O
before	O
producing	O
or	O
consuming	O
any	O
data	O
.	O
</s>
<s>
Alternatively	O
,	O
a	O
process	B-Operating_System
might	O
create	O
new	O
threads	B-Operating_System
and	O
use	O
the	O
pipe	O
to	O
communicate	O
between	O
them	O
.	O
</s>
<s>
Named	B-Operating_System
pipes	I-Operating_System
may	O
also	O
be	O
created	O
using	O
mkfifo( )	O
or	O
mknod( )	O
and	O
then	O
presented	O
as	O
the	O
input	O
or	O
output	O
file	O
to	O
programs	O
as	O
they	O
are	O
invoked	O
.	O
</s>
<s>
They	O
allow	O
multi-path	O
pipes	B-Operating_System
to	O
be	O
created	O
,	O
and	O
are	O
especially	O
effective	O
when	O
combined	O
with	O
standard	O
error	O
redirection	B-Device
,	O
or	O
with	O
tee	B-Operating_System
.	O
</s>
<s>
In	O
most	O
Unix-like	B-Operating_System
systems	I-Operating_System
,	O
all	O
processes	O
of	O
a	O
pipeline	B-General_Concept
are	O
started	O
at	O
the	O
same	O
time	O
,	O
with	O
their	O
streams	O
appropriately	O
connected	O
,	O
and	O
managed	O
by	O
the	O
scheduler	O
together	O
with	O
all	O
other	O
processes	O
running	O
on	O
the	O
machine	O
.	O
</s>
<s>
An	O
important	O
aspect	O
of	O
this	O
,	O
setting	O
Unix	B-Operating_System
pipes	I-Operating_System
apart	O
from	O
other	O
pipe	O
implementations	O
,	O
is	O
the	O
concept	O
of	O
buffering	B-General_Concept
:	O
for	O
example	O
a	O
sending	O
program	O
may	O
produce	O
5000	O
bytes	B-Application
per	O
second	O
,	O
and	O
a	O
receiving	O
program	O
may	O
only	O
be	O
able	O
to	O
accept	O
100	O
bytes	B-Application
per	O
second	O
,	O
but	O
no	O
data	O
is	O
lost	O
.	O
</s>
<s>
Instead	O
,	O
the	O
output	O
of	O
the	O
sending	O
program	O
is	O
held	O
in	O
the	O
buffer	B-General_Concept
.	O
</s>
<s>
When	O
the	O
receiving	O
program	O
is	O
ready	O
to	O
read	O
data	O
,	O
the	O
next	O
program	O
in	O
the	O
pipeline	B-General_Concept
reads	O
from	O
the	O
buffer	B-General_Concept
.	O
</s>
<s>
If	O
the	O
buffer	B-General_Concept
is	O
filled	O
,	O
the	O
sending	O
program	O
is	O
stopped	O
(	O
blocked	O
)	O
until	O
at	O
least	O
some	O
data	O
is	O
removed	O
from	O
the	O
buffer	B-General_Concept
by	O
the	O
receiver	O
.	O
</s>
<s>
In	O
Linux	O
,	O
the	O
size	O
of	O
the	O
buffer	B-General_Concept
is	O
65,536	O
bytes	B-Application
(	O
64KiB	O
)	O
.	O
</s>
<s>
An	O
open	O
source	O
third-party	O
filter	O
called	O
is	O
available	O
to	O
provide	O
larger	O
buffers	B-General_Concept
if	O
required	O
.	O
</s>
<s>
Tools	O
like	O
netcat	B-Protocol
and	O
socat	O
can	O
connect	O
pipes	B-Operating_System
to	O
TCP/IP	O
sockets	O
.	O
</s>
<s>
The	O
pipeline	B-General_Concept
concept	O
was	O
invented	O
by	O
Douglas	O
McIlroy	O
and	O
first	O
described	O
in	O
the	O
man	B-Protocol
pages	I-Protocol
of	O
Version	B-Operating_System
3	I-Operating_System
Unix	I-Operating_System
.	O
</s>
<s>
McIlroy	O
noticed	O
that	O
much	O
of	O
the	O
time	O
command	B-Application
shells	I-Application
passed	O
the	O
output	O
file	O
from	O
one	O
program	O
as	O
input	O
to	O
another	O
.	O
</s>
<s>
His	O
ideas	O
were	O
implemented	O
in	O
1973	O
when	O
(	O
"	O
in	O
one	O
feverish	O
night	O
"	O
,	O
wrote	O
McIlroy	O
)	O
Ken	O
Thompson	O
added	O
the	O
pipe( )	O
system	B-Operating_System
call	I-Operating_System
and	O
pipes	B-Operating_System
to	O
the	O
shell	B-Operating_System
and	O
several	O
utilities	O
in	O
Version	B-Operating_System
3	I-Operating_System
Unix	I-Operating_System
.	O
</s>
<s>
McIlroy	O
also	O
credits	O
Thompson	O
with	O
the	O
|	O
notation	O
,	O
which	O
greatly	O
simplified	O
the	O
description	O
of	O
pipe	O
syntax	O
in	O
Version	B-Operating_System
4	I-Operating_System
.	O
</s>
<s>
Although	O
developed	O
independently	O
,	O
Unix	B-Operating_System
pipes	I-Operating_System
are	O
related	O
to	O
,	O
and	O
were	O
preceded	O
by	O
,	O
the	O
'	O
communication	O
files	O
 '	O
developed	O
by	O
Ken	O
Lochner	O
in	O
the	O
1960s	O
for	O
the	O
Dartmouth	B-Operating_System
Time	I-Operating_System
Sharing	I-Operating_System
System	I-Operating_System
.	O
</s>
<s>
In	O
Tony	O
Hoare	O
's	O
communicating	O
sequential	O
processes	O
(	O
CSP	O
)	O
McIlroy	O
's	O
pipes	B-Operating_System
are	O
further	O
developed	O
.	O
</s>
<s>
The	O
robot	O
in	O
the	O
icon	O
for	O
Apple	O
's	O
Automator	O
,	O
which	O
also	O
uses	O
a	O
pipeline	B-General_Concept
concept	O
to	O
chain	O
repetitive	O
commands	O
together	O
,	O
holds	O
a	O
pipe	O
in	O
homage	O
to	O
the	O
original	O
Unix	B-Application
concept	O
.	O
</s>
<s>
This	O
feature	O
of	O
Unix	B-Application
was	O
borrowed	O
by	O
other	O
operating	B-General_Concept
systems	I-General_Concept
,	O
such	O
as	O
MS-DOS	B-Application
and	O
the	O
CMS	B-Operating_System
Pipelines	I-Operating_System
package	O
on	O
VM/CMS	B-Application
and	O
MVS	B-Application
,	O
and	O
eventually	O
came	O
to	O
be	O
designated	O
the	O
pipes	B-Operating_System
and	I-Operating_System
filters	I-Operating_System
design	I-Operating_System
pattern	I-Operating_System
of	O
software	B-General_Concept
engineering	I-General_Concept
.	O
</s>
