Pipeline (PIPE)

Source: Internet
Author: User
1. Background

Once I met this requirement: application a (which can be encoded and compiled by myself) calls a console application B (which cannot be modified by a third party ), console program B prints data on the standard output device (similar to the command line window). Application A needs to obtain and process the data.

After this requirement is met, the following implementation methods are summarized: "Pipeline" and "redirection ". This section briefly summarizes the concept of "Pipeline" and focuses on "anonymous Pipeline ".

2. Pipeline (PIPE)

Pipeline has two types: anonymous pipeline (anonymous pipes) and named pipeline (Named Pipes ). An MPs queue is a shared memory used for inter-process communication. The process for creating an MPS queue is called an MPS Queue Server. The process connecting to an MPS queue is a MPs client. After a process writes data to the pipeline, the other process can read data from the other end of the pipeline.

The anonymous pipeline (anonymous pipes) is an unnamed pipeline for one-way data transmission between the parent process and the child process. It can only be used on a local computer and cannot be used for inter-network communication.

3. Anonymous pipeline (anonymous pipes)

An anonymous pipeline is created by the createpipe function. When creating an anonymous pipeline, this function returns two handles: the read-only handle of the pipeline and the write-only handle of the pipeline. The createpipe function is prototype:

Bool createpipe (

Phandle hreadpipe, // pointer to read handle

Phandle hwritepipe, // pointer to write handle

Lpsecurity_attributes lppipeattributes, // pointer to security attributes

DWORD nsize // pipe size

);

The handles pointed to by hreadpipe and hwritepipe can be read-only and write-only to access the pipeline respectively. When anonymous pipeline communication is used, the server process must send a handle to the client process. The transfer of handles is mostly done through inheritance, and server processes also allow these handles to be inherited by child processes. In addition, processes can send handles to unrelated processes through inter-process communication such as DDE or shared memory.

When the writefile function is used to write data to the MPs queue, the function returns only when the specified bytes of data are written to the MPs queue or when an error occurs. If the buffer in the MPs queue is full and the data has not been written, the writefile will not be returned until another process reads the data in the MPs queue to release more available space. When the MPs Queue Server calls createpipe to create an MPS queue, it sets the buffer size of the MPs queue with the nsize parameter.

Anonymous pipelines do not support asynchronous read and write operations, which means that readfileex and writefileex cannot be used in anonymous pipelines, and the lpoverlapped parameters in readfile and writefile will also be ignored. The anonymous pipeline closes the Read and Write handles, or you can call the closehandle function in the process to close the handles.

4. Pipeline handle inheritance

The pipeline server can control whether an anonymous pipeline can be inherited through the following channels:

◆ When calling the createpipe function, if the MPs Queue Server sets binherithandle as the data member of the security_attributes data structure to true for which lppipeattributes points, the pipeline read and write handles created by createpipe will be inherited.

◆ The pipe server can use the duplicatehandle function to change the inheritance of the pipe handle. The MPs Queue Server can create an inherited copy for an inherited MPs queue handle or an inherited copy for an inherited MPs queue handle.

◆ The CreateProcess function can also enable the MPs Queue Server to determine whether the child process inherits all or not the inherited handles.

Before a child process is generated, the parent process first calls the Win32 API setstdhandle so that the child process and parent process can share the standard input, standard output, and standard error handle. When the parent process sends data to the child process, use setstdhandle to assign the read handle of the pipeline to the standard input handle. When receiving data from the child process, use setstdhandle to assign the write handle of the pipeline to the standard output (or standard error) handle. Then, the parent process can call the CreateProcess function to generate sub-processes. If the parent process needs to send data to the child process, the parent process can call writefile to write data to the pipeline (passing the pipeline write handle to the function), and the child process calls getstdhandle to get the read handle of the pipeline, import the handle to readfile and read data from the MPs queue.

If the parent process reads data from the child process, the child process calls getstdhandle to get the write handle of the pipeline and calls writefile to write data to the pipeline. Then, the parent process calls readfile to read data from the pipeline (pass the pipeline read handle to the function ).

When all write handles to the pipe are closed, the readfile function returns zero. it is important for the parent process to close its handle to the write end of the pipe before calling readfile. if this is not done, the readfile operation cannot return zero because the parent process has an open handle to the write end of the pipe.

The procedure for redirecting the standard input handle is similar to that for redirecting the standard output handle, handle t that the pipe's read handle is used as the child's standard input handle. in this case, the parent process must ensure that the child process does not inherit the pipe's write handle. if this is not done, the readfile operation was med by the child process cannot return zero because the child process has an open handle to the write end of the pipe.

5. Reference

For more information, see the msdn help documentation: msdn library Visual Studio 6.0 (CHS ). You can find the createpipe function, choose "View"> "locate topic in directory" from the menu, and find detailed descriptions of pipelines. At the same time, msdn also provides source code examples.


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.