Inter-process communication: Pipelines
The use of the commands in Linux is actually the input that passes the output of one process through the pipeline to another process, which is packaged by the shell, reconnecting the standard input and output streams, and finally outputting the data stream from the keyboard input to the screen by two programs. As follows:
cmd1|cmd2
Process Pipeline
The simplest way to pass data between two programs is to use Popen () and Pclose (). The prototype is as follows:
#include <stdio.h>FILE *popen(constcharconstchar *open_mode);int pclose(FILE *stream_to_close);
1.popen function
The Popen function allows a program to launch another program as a new program. and can pass data to him or receive data through it. command is the program name and parameter to run, Open_mode is the way to invoke the program and must be "R" or "W".
When you request the Popen call to run a program, it starts the shell first, and then passes the command string to it as a parameter. Start the shell parsing command string before starting the program, which allows the program to easily start a complex shell command. However, each Popen call will not only start the requested program but also start a shell, which leads to a waste of system resources, so the Popen function calls the target command slower than the normal way.
Popen will return a file stream pointer. Depending on the file stream pointer, you can read and write to the new program accordingly.
If opened in read mode, the output of the called program can be read through the Stdio library function fread. The fread reads count from the stream stream, each of which is sized as a size byte and written to buffer. It then returns the number of elements actually read and, if the return value is not the same as count, the end of the file or an error may occur.
If it is open in writing, you can use the Fwrite function to write data to the called program.
The read and write of the file here can use Fread and fwrite, but not limited to these two functions. Fgets and fputs and so on are also perfectly acceptable.
#include <stdio.h>void *buffer, size_t size, size_t count, FILE *stream) ;size_t fwrite(constvoid* buffer, size_t size, size_t count, FILE* stream);
2.pclose function
The Pclose function is used to close the pipe and file pointers established by Popen. The Pclose returns only after the Popen-initiated process has ended, and Pclose waits for the program to end if the program is still running when the pclose is called.
The bottom of the pipeline function pipe
#include <unistd.h>int pipe(int file_descriptor[2]);
Passing data between two programs through this function eliminates the need to start a shell to interpret the requested command, and it provides more control over read and write. The parameter of the pipe function is an array pointer consisting of a file descriptor of two integer types, and the pipe returns 0 after filling in the array with two new file descriptors. Data written to file_descriptor[1] can be read from file_descriptor[2], and the principle of FIFO is used to read data.
Note: Since this is a file descriptor instead of a file stream, we use the underlying read and write functions to read and write instead of the file stream library functions fread and fwrite.
The real advantage of pipelines is that when data is passed between two processes, the original open file descriptor remains open when the program creates a new process with fork calls. If you create a pipeline in the original process and then call Fork to create a new process, we can pass the data through the pipeline between two processes.
Inter-process communication: Pipelines