You’ll be familiar with the concept of a pipe in the real world. It is a construction that redirects something from one place to another. For example, a traditional hose pipe transports water from a tap to a sprinkler or showerhead. The concept in computing is very similar; an output from something is redirected to become an input to something else.
Inter-Process Communication
There are multiple uses for pipes in computing. One of them is to enable inter-process communication. Modern computers use something called virtual memory. Rather than allowing each process to see the native memory addresses, the computer gives each process its own virtual memory address system.
A pipe is a way to communicate between processes. This practice allows the system to do plenty of trickery in the background, allowing for non-contiguous uses of memory space, for example, all while the actual process is unaware. It also provides a level of security, as one process cannot read the virtual memory addresses of another process. However, this makes it a little tricky to communicate when you actually want to.
While different programming languages can handle the specifics differently, C is well known, so we’ll use that language for our example. In C, a pipe must be opened by a process. It can only then be shared with another process by forking that process. The child process, and all of its children, too, have access to the pipe.
No other processes can access the pipe. A pipe can be read or written to like a file. Instead of being written permanently to disk, though, it’s written as a FIFO stream to memory. This means that once the data is read from the pipe, it’s removed. As such, it’s essential to treat pipes as a one-way communication system; if bidirectional communication is needed, you need to use two pipes. If you don’t do this, you will just read your oessages, preventing the recipient from ever seeing them.
Tip: If you need two communication processes that aren’t parent and child, you can use a similar function, FIFO.
Pipes in the Command Line
Pipes aren’t only a memory link; there is also a way to redirect output to the input of another command. For example, let’s say you want to print every name from a file, sort them, and delete duplicates. You could do this in separate passes, keeping everything in the file. The other option is to do it all with one command using pipes.
In Linux, a command to print the contents of a file to the terminal is “cat <filename>.” To sort, you can use the “sort” command. To delete duplicates, you can use “uniq.” if you want to run these as a one-line command, you need to pipe the output of one command to the next. That is done, conveniently enough, using the pipe symbol “|.” The pipe symbol is distinct from a capital “i,” lowercase “L,” and the number one, though they can look similar in some fonts. To help keep it distinct, it is often shown on the keyboard or in fonts with a small split in the middle, though it can also be a single solid vertical line.
To use the pipe symbol, you jlace it between two commands. It takes the output of the first command and provides it as the input for the second command. To print a file, sort the contents, and delete duplicates, you can run the command: “cat <filename> | sort | uniq.” As the pipe completely intercepts the output, this doesn’t even print the output at the intermediate steps. The only result is the output from uniq.
Conclusion
In computing, a pipe is a method of communicating between two processes. This can be a child and parent process via a memory-mapped file. It can also be by piping one command’s output to another’s input. Redirecting the output of a command on the command line like this is done by using the pipe symbol “|.”