World Library  
Flag as Inappropriate
Email this Article

Pipeline (Unix)

Article Id: WHEBN0001236117
Reproduction Date:

Title: Pipeline (Unix)  
Author: World Heritage Encyclopedia
Language: English
Subject: Inter-process communication, Standard streams, Unix, Command-line interface, Named pipe
Collection: Inter-Process Communication, Unix
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Pipeline (Unix)

A pipeline of three programs run on a text terminal

In Unix-like computer operating systems (and, to some extent, Microsoft Windows), a pipeline is a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) to the next one. Filter programs are often used in this configuration.

The concept of pipelines was invented by Douglas McIlroy at Unix's ancestral home of Bell Labs, prior to the actual invention of the operating system, and implemented in Unix at his insistence, shaping its toolbox philosophy.[1][2] It is named by analogy to a physical pipeline. The standard shell syntax for pipelines is to list multiple programs to invoke in one command, separated by vertical bars:

program1 | program2 | program3

Each program is assumed to take input and give output on its standard streams. Each "|" tells the shell to connect the standard output of the left program to the standard input of the right program by an inter-process communication mechanism called an (anonymous) pipe, implemented in the operating system. Since pipes are unidirectional, data flows through the pipeline from left to right.

For example, to list files in the current directory (ls), retain only the lines of ls output containing the string "key" (grep), and view the result in a scrolling page (less), one can issue the command:

ls -l | grep key | less

Contents

  • Pipelines in command line interfaces 1
    • Error stream 1.1
    • Pipemill 1.2
  • Creating pipelines programmatically 2
  • Implementation 3
    • Network pipes 3.1
  • History 4
    • Other operating systems 4.1
  • See also 5
  • References 6
  • External links 7

Pipelines in command line interfaces

All widely used Unix and Windows shells have a special syntax construct for the creation of pipelines. In all usage one writes the filter commands in sequence, separated by the ASCII vertical bar character "|" (which, for this reason, is often called "pipe character"). The shell starts the processes and arranges for the necessary connections between their standard streams (including some amount of buffer storage).

Error stream

By default, the standard error streams ("stderr") of the processes in a pipeline are not passed on through the pipe; instead, they are merged and directed to the console. However, many shells have additional syntax for changing this behaviour. In the csh shell, for instance, using "|&" instead of "|" signifies that the standard error stream too should be merged with the standard output and fed to the next process. The Bourne Shell can also merge standard error, using 2>&1, as well as redirect it to a different file.

Pipemill

In the most commonly used simple pipelines the shell connects a series of sub-processes via pipes, and executes external commands within each sub-process. Thus the shell itself is doing no direct processing of the data flowing through the pipeline.

However, it's possible for the shell to perform processing directly, using a so-called "mill", or "pipemill", (since a while command is used to "mill" over the results from the initial command). This construct generally looks something like:

command | while read var1 var2 ...; do
   # process each line, using variables as parsed into $var1, $var2, etc
   # (note that this may be a subshell: var1, var2 etc will not be available
   # after the while loop terminates; some shells, such as zsh and newer
   # versions of Korn shell, process the commands to the left of the pipe
   # operator in a subshell)
   done

Such pipemill may not perform as intended if the body of the loop includes commands, such as cat and ssh, that read from stdin:[3] on the loop's first iteration, such a program (let's call it the drain) will read the remaining output from command, and the loop will then terminate (with results depending on the specifics of the drain). There are a couple of possible ways to avoid this behavior. First, some drains support an option to disable reading from stdin (e.g. ssh -n). Alternatively, if the drain does not need to read any input from stdin to do something useful, it can be given < /dev/null as input.

Creating pipelines programmatically

Pipelines can be created under program control. The Unix pipe() system call asks the operating system to construct a new anonymous pipe object. This results in two new, opened file descriptors in the process: the read-only end of the pipe, and the write-only end. The pipe ends appear to be normal, anonymous file descriptors, except that they have no ability to seek.

To avoid deadlock and exploit parallelism, the Unix process with one or more new pipes will then, generally, call fork() to create new processes. Each process will then close the end(s) of the pipe that it will not be using before producing or consuming any data. Alternatively, a process might create a new thread and use the pipe to communicate between them.

Named pipes may also be created using mkfifo() or mknod() and then presented as the input or output file to programs as they are invoked. They allow multi-path pipes to be created, and are especially effective when combined with standard error redirection, or with tee.

Implementation

In most Unix-like systems, all processes of a pipeline are started at the same time, with their streams appropriately connected, and managed by the scheduler together with all other processes running on the machine. An important aspect of this, setting Unix pipes apart from other pipe implementations, is the concept of buffering: for example a sending program may produce 5000 bytes per second, and a receiving program may only be able to accept 100 bytes per second, but no data is lost. Instead, the output of the sending program is held in a queue. When the receiving program is ready to read data, the operating system sends its data from the queue, then removes that data from the queue. If the queue buffer fills up, the sending program is suspended (blocked) until the receiving program has had a chance to read some data and make room in the buffer. In Linux, the size of the buffer is 65536 bytes. An open source third-party filter called bfr is available to provide larger buffers if required.

Network pipes

Tools like netcat and socat can connect pipes to TCP/IP sockets.

History

The pipeline concept was invented by Douglas McIlroy and first described in the man pages of Version 3 Unix.[4] One of the authors of the early command shells, McIlroy noticed that much of the time they were processing the output of one program as the input to another.

His ideas were implemented in 1973 when ("in one feverish night", wrote McIlroy) | notation, which greatly simplified the description of pipe syntax in Version 4.[5][4] The idea was eventually ported to other operating systems, such as DOS, OS/2, Microsoft Windows, and BeOS, often with the same notation.

Although developed independently, Unix pipes are similar to, and were preceded by, the 'communication files' developed by Ken Lochner [6] in the 1960s for the Dartmouth Time Sharing System.[7]

The robot in the icon for Apple's Automator, which also uses a pipeline concept to chain repetitive commands together, holds a pipe in homage to the original Unix concept.

Other operating systems

This feature of Unix was borrowed by other operating systems, such as Taos and MS-DOS, and eventually became the pipes and filters design pattern of software engineering.

See also

References

  1. ^ Mahoney, Michael S. "The Unix Oral History Project: Release.0, The Beginning". McIlroy: It was one of the only places where I very nearly exerted managerial control over Unix, was pushing for those things, yes. 
  2. ^ http://cm.bell-labs.com/cm/cs/who/dmr/mdmpipe.html
  3. ^ http://web.archive.org/web/20120306135439/http://72.14.189.113/howto/shell/while-ssh/
  4. ^ a b  
  5. ^ http://www.linfo.org/pipe.html Pipes: A Brief Introduction by The Linux Information Project (LINFO)
  6. ^ http://www.cs.rit.edu/~swm/history/DTSS.doc
  7. ^ http://cm.bell-labs.com/who/dmr/hist.html

External links

  • History of Unix pipe notation
    • Doug McIlroy’s original 1964 memo, proposing the concept of a pipe for the first time
  • : create an interprocess channel – System Interfaces Reference, The Single UNIX® Specification, Issue 7 from The Open Group
  • Pipes: A Brief Introduction by The Linux Information Project (LINFO)
  • Unix Pipes – powerful and elegant programming paradigm (Softpanorama)
  • at WikibooksAd Hoc Data Analysis From The Unix Command Line – Shows how to use pipelines composed of simple filters to do complex data analysis.
  • Use And Abuse Of Pipes With Audio Data – Gives an introduction to using and abusing pipes with netcat, nettee and fifos to play audio across a network.
  • stackoverflow.com – A Q&A about bash pipeline handling.
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.