What is the executable file of pipe operator / pipe character (vertical bar character “|”) in linux/unix system

  • A+
Category:Languages

Usually, we run commands on terminal/tty/command window in the linux system. If we run ls -l ,we know that, we actually start execute a file named ls located /bin, which full path is /bin/ls.

There are a lot of talks about pipeline in *nix-like system. But what is the exact executable file located in the system corresponding to "|", even if the pipeline function is built in the system, there should be a piece of code be found. But it seems that nobody talks about that. It's no very easy to pick up the answers from the google's result page.

I have read some elementary knowledge about pipeline creation or someting related to pipeline programming in c language. but those looks very different from the behavour of |. Or I can say the pipe operator ("|") in the shell do the same things like the elementary c programming books described?

Besides the "|", pipe character, what is the corresponding to files of ">","<" ?

Update 1: After get some answers from yours posts below, I think my original skeleton of the pipe mechanism is called "faked pipe" from http://www.linfo.org/pipe.html

 


The characters <, >, >>, | are not processes in the same sense as executables like ls. Rather, they are instructions to the shell which modify the ways in which other processes are created.

First, note that however you type in your Linux commands, you're not really directly interacting with the Linux operating system itself. You're using a program called a shell - most likely /bin/bash, but there are several different shell programs available, most of which operate in much of the same basic ways. The main purpose of a shell is to interpret your input in order to start up other processes. This involves the Linux system call fork (or possibly clone) to create a new process as a child process of the shell process, followed by the system call execve to have that child process load and run the specified program with the specified arguments.

The shell might do various bits of interpretation on the input to determine what the actual program and arguments will be, like substituting command aliases, substituting environment variables, and expanding ~, *, and [] characters. Similarly, the <, >, >>, and | symbols have special meaning to the shell, but rather than modifying the executable name or arguments, they modify exactly how the child process is created.

A Linux process normally begins life with three "file handles" open: standard input, standard output, and standard error. By default, child processes of the shell will use the same input/output/error as the shell, meaning that they can get input from the keyboard via the same mechanism as the shell, and output from the process appears in the same window. When the <, >, and >> redirection instructions are used, the shell will modify the file handles for the process in between the fork and the execve so that before the executable is even started in the child, one or more of its default file handles will actually read from/write to the named files.

When the "pipe" character | is used, the shell will start up two child processes at about the same time, but with the output of the first process connected up to the input of the second process. This involves the system call pipe, to create a pair of file handles where writing to one handle just puts the data into operating system memory until it is read out from the other handle.

So for example,

grep 'MAGIC' file1.txt | sort 

will (not necessarily in this order):

  1. Create a pair of pipe file handles
  2. Create a child process for the grep command.
  3. Replace the child's standard output file handle to use the write end of the pipe.
  4. Begin the grep command via execve.
  5. Create a child process for the sort command.
  6. Replace the child's standard input file handle to use the read end of the pipe.
  7. Begin the sort command via execve.
  8. Wait for the sort child process to finish.

In terms of "even if the pipeline function is built in the system, there should be a piece of code be found", you would find the code for interpreting these special shell characters in the code for the shell program.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: