In modern operating systems, it is possible to pass "standard output of one program" to "standard input of another program". This OS function is called a pipe.
The function called pipe is a very convenient function. If you create a program that receives data from standard input and outputs it to standard output, you can link multiple programs.
1. Image of pipe
| - -- -- --- | | - -- -- --- | | - -- -- --- | First input - ->| Program A | - ->| Program B | - ->| Program C | - ->Final output | - -- -- --- | | - -- -- --- | | - -- -- --- |
You can use a pipe to connect the standard output of Program A to the standard input of Program B. In addition, the standard output of program B can be concatenated with the standard input of program C. By concatenating standard output and standard input in this way, data can be processed continuously without creating an intermediate file.
2. Use pipes
perldoc perlintro | perl -ne "print if/open /"
| Is the pipe symbol. In this example, the standard output of perldoc perlintro is concatenated to the standard input of perl -ne "print if/open /".
The perldoc command is a perl manual display command. perl -ne "print if/open /" is a one-liner for unix grep. Selects the line containing the word open and outputs it.
Overall, I'm pulling out the line containing open from the perlinto man page. Pipes allow multiple programs to process data continuously.
3. How the pipe works
Pipe is a function that connects the standard output of one program to the standard input of another program. I think that understanding will deepen if you know what the OS is actually doing internally, so I will write it down.
Don't be afraid to misunderstand, a pipe is a temporary file (* 1). Program A outputs to a temporary file, and Program B reads from that temporary file. The image is as follows.
| - -- -- --- | | - -- -- --- | | Program A | - ->Temporary files (pipes) - ->| Program B | | - -- -- --- | | - -- -- --- |
Since the OS implicitly uses temporary files, it looks like the output of program A is directly the input of program B. This is the mechanism behind the pipe.
Let me tell you another important story. Program A and Program B are talking about when to close temporary files.
Program A is opening a temporary file in write mode. Program B is opening a temporary file in read mode. Program A does not need to read the temporary file, and program B does not need to write to the temporary file, so it is opened this way.
Now, when do program A and program B close the temporary files? Consider multitasking when program A and program B are running at the same time.
The answer for Program A is simple. Close the temporary file when the output of program A is finished. The processing of program A does not depend on program B.
So when does Program B close the temporary file? The answer is that Program A closes the temporary file and reads the data in the temporary file to the end. Just reaching EOF like a normal file does not close it. This is because there is still the possibility that Program A will continue to write. So, in order for Program B to close the temporary file, Program A needs to close the temporary file.
* 1 Pipe mounting differs depending on the OS. On Unix-like operating systems, pipes are created in memory. On Windows, a pipe is created as a temporary file on the disk.