MPs Queue Technology in Windows

Source: Internet
Author: User
When I used to learn Linux, I encountered a pipe problem and thought that the pipe is just Program As the input of a program. Like this:
# Cat file | grep "ABC"
Here, the output of cat file is directly used as the input of the grep "ABC" command. Using pipelines, you can change the default input and output of the program.


Today, I accidentally turned to msdn and saw the pipe concept in windows. I never knew it before, Khan ~~

How is pipe defined:
APipeIs a section of shared memory that processes use for communication. The process that creates a pipe isPipe Server. A process that connects to a pipe isPipe Client. One process writes information to the pipe, then the other process reads the information from the pipe.

In other words, the pipeline is a shared memory so that processes can communicate with each other. The process that creates the pipe kernel object is a pipe server. When another process is connected to the pipe server created by this process, it is called pipe client. when a process writes information to the piple, the other process can read this information from the pipe.

There are two types of pipes: anonymous pipes and named pipes. Anonymous pipes require less overhead than named pipes, but offer limited services.

The termPipe, As used here, implies that a pipe is used as an information conduit. conceptually, a pipe has two ends. A one-way Pipe allows the process at one end to write to the pipe, and allows the process at the other end to read from the pipe. A two-way (or duplex) pipe allows a process to read and write from its end of the pipe:

Pipe can be divided into two types: anonymous pipes (last-name pipe) and named pipes (Named Pipe). Anonymous pipes requires less overhead than named pipes, however, the disadvantage is that few functions are provided.
Pipe refers to the concept that, as an information pipeline, pipe contains two ends, one end allows the process to write data, and the other end allows the process to read data. Both ENDS allow the process to read or write data.

1. Anonymous Pipes

AnAnonymous pipeIs an unnamed, one-way pipe that typically transfers data between a parent process and a child process. Anonymous pipes are always local; they cannot be used for communication over a network.
Data can be transmitted between parent and child processes. It is often used on local machines and is not suitable for communication between different machines in the network.

2. Named Pipes

ANamed PipeIs a named, one-way or duplex pipe for communication between the pipe server and one or more pipe clients. all instances of a named pipe share the same pipe name, but each instance has its own buffers and handles, and provides a separate conduit for client-server communication. the use of instances enables multiple pipe clients to use the same named pipe simultaneously.
The two ends in the name pipe can serve as a tool for communication between one pipe server and another (or multiple) pipe clients. All pipe instance objects share the pipe name. However, each pipe instance has its own buffer and handle. in addition, pipe server and each pipe client use different pipelines for data transmission and are not shared. It enables multiple pipe clients to use this pipe name to transmit data simultaneously.

Any process can access named pipes, subject to security checks, making Named Pipes an easy form of communication between related or unrelated processes. named Pipes can be used to provide communication between processes on the same computer or between processes on different computers extends ss a network.
Any process can control named pipes. According to the security check, any associated and unrelated processes can communicate through named pipes and can communicate with processes in a network or between different networks.

Any process can act as both a server and a client, making peer-to-peer communication possible. As used here, the termPipe ServerRefers to a process that creates a named pipe, and the termPipe ClientRefers to a process that connects to an instance of a named pipe.
Processes can be both server and client. In the end-to-end communication environment, pipe server refers to the process that creates pipe, the pipe client is the process connecting to the pipe instance.

Example:

First, let's first look at an unonymous pipe example. In this example, the content of a DoS process is output to the cedit control of an MFC application through pipeline technology.

Example Source: http://dev.csdn.net/develop/article/18/18338.shtm

First, create a pipe, set its two ends, and use the createpipe function. Before createpipe, set its security options. To communicate with sub-processes, the sub-processes also need to use this pipe kernel object. Therefore, the kernel object handle must be inherited. (For kernel objects, see Chapter 3 of core programming)

Handle hreadpipe, hwritepipe;
Security_attributes SA;

SA. nlength =   Sizeof (Security_attributes );
SA. lpsecuritydescriptor = NULL; // Use the default security descriptor of the system
SA. binherithandle = True; // It must be true, otherwise the handle cannot be inherited.

Creeatepipe ( & Hreadpipe, & Hwritepipe, & Sa, 0 );// Create the pipe Kernel Object and set hreadpipe and hwritepipe.

Next, create a DOS process to communicate with it. The function for creating a process is CreateProcess. This function hasL Pstartupinfo LpstartupinfoFor details about the parameters, refer to Chapter 4 of core programming.

Startupinfo Si;
Process_information PI;
Si. CB =   Sizeof (Startupinfo );
Getstartupinfo ( & Si );
Si. hstderror = Hwritepipe; // Set the standard error output to hwritepipe.
Si. hstdoutput = Hwritepipe; // Set the standard output to hwritepipe.
Si. wshowwindow = Sw_hide;
Si. dwflags = Startf_useshowwindow | Startf_usestdhandles;
If ( ! CreateProcess (null, " C: \ Windows \ system32 \ cmd.exe/c dir /? "
, Null, null, true, null, & Si, & Pi )) {
MessageBox ("Error on CreateProcess ()");
Return;
}
Closehandle (hwritepipe );

The preceding settings output the content of the DOS window to hwritepipe. The following figure shows how to set hreadpipe so that the two processes can communicate with each other.

Char Buffer [ 4096 ] =   {0} ;
DWORD bytesread;
While ( True ) {
If (Readfile (hreadpipe, buffer, 4095 , & Bytesread, null) = Null) // Read data from hreadpipe.
Break ;
M_edit1 + = Buffer;
Updatedata ( False );
Sleep ( 200 );
}  

There is also the content of named pipes, and the next decomposition ...........

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.