Inter-process communication: Pipelines

Source: Internet
Author: User

Inter-process communication: Pipelines
Inter-process communication: Pipelines

In Linux, linking commands together is actually to pass the output of a process to the input of another process through a pipeline. These are shell encapsulated and the standard input and output streams are reconnected, make the data stream output from the keyboard through two programs to the screen. As follows:

cmd1|cmd2
Process Pipeline

The simplest way to transmit data between two programs is to use popen () and pclose. The prototype is as follows:

#include <stdio.h>FILE *popen(const char *command, const char *open_mode);int pclose(FILE *stream_to_close);
1. popen Function

The popen function allows one program to start another program as a new program. It can also transmit data to or receive data through it. Command is the name and parameter of the program to be run. open_mode is the method of calling the program and must be "r" or "w ".
When you request a popen call to run a program, it first starts the shell and passes the command string as a parameter to it. Start the shell Analysis command string before starting the program. This allows the program to easily start complicated shell commands. However, each popen call not only starts the Requested program but also starts a shell, which leads to a waste of system resources. Therefore, the target command of the popen function call is slower than that of the normal method.
Popen returns a file stream pointer. You can perform read/write operations with the new program based on the file stream pointer.
If it is opened in Read mode, you can use the library function fread of stdio to read the output of the called program. Fread reads count data from the stream, each of which is size bytes and writes it to the buffer. Then, the number of actually read elements is returned. If the returned value is different from the count value, the end of the file or an error may occur.
If it is opened in write mode, you can use the fwrite function to write data to the called program.
Fread and fwrite can be used for file read and write, but they are not limited to these two functions. Fgets, fputs, and so on.

#include <stdio.h>size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
2. pclose Function

The pclose function is used to disable pipelines and file pointers created by popen. Pclose is returned only after the process started by popen ends. If the program is still running when pclose is called, pclose will wait until the program ends.

Pipe, the underlying Pipeline Function
#include <unistd.h>int pipe(int file_descriptor[2]);

Using this function to transmit data between two programs, you no longer need to start a shell to explain the Request command. It also provides more control over read/write. The parameter of the pipe function is an array pointer consisting of two integer file descriptors. pipe returns 0 after entering two new file descriptors in the array. Data written to file_descriptor [1] can be read from file_descriptor [2]. The read data adopts the FIFO principle.
Note: because the file descriptor is used instead of the file stream, we need to use the underlying read and write Functions for reading and writing, rather than the file stream library functions fread and fwrite.

The real advantage of pipelines is that when data is transmitted between two processes, when the program uses fork to call and create a new process, the file descriptor that was originally opened will remain open. If you create an pipeline in the original process and call fork to create a new process, we can pass data between two processes through the pipeline.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.