Support for inter-process communication in Windows operating systems-pipelines and routes

Source: Internet
Author: User
Recently, I used C ++ to write inter-process communication. Program The concept of MPs queue is used:

There are four basic methods for data communication between Windows applications. The simplest is to useClipboard; The other isDDE(Dynamic Data Exchange), it uses a public protocol to achieve communication between two or more applications; in addition, throughMemory ing FileMemory ing can map a virtual address of a process into a file, and other processes can share the virtual address of the process.Pipelines and postal routesImplement inter-process data communication.

Before discussing pipelines and routes, let's review these concepts: Processes and threads. Microsoft officially defines processes and threads as follows:

Process: In the simplest words, a process is an ongoing program. One or more threads run in the process. The thread is the minimum unit for the operating system to allocate CPU computing time. Each process provides the resources necessary to run a program, A process has a 4 GB virtual address space (Windows NT Server Enterprise Edition and Windows 2000 Advanced Server medium and low 3 GB virtual address space for the process to use, 1 GB high for the operating system kernelCode. Windows NT/2000 medium/low 2 GB for the process, 2 GB for the operating system kernel code. Windows 9x: 0-64 K read-only space is used to load Microsoft DOS information, 64 K-4 m is used to load dos compatible code, and 4 m-2 GB is used by processes, 2 GB-3 GB shared space loads various DLL code, 3 GB-4 GB is the shared system kernel code space, among them, the shared 2 GB-4 GB space is 99% of the "Invalid Memory Page error", "General protect error (GPE)" and the culprit of the blue screen .), Executable code, Data, object handle, environment variables, priority, and the ability to maximize and minimize. Each process is executed from a main thread, but an additional thread can be created in the thread it owns. All threads of a process share the virtual address space and system resources of the process. The resources of a thread include the machine register settings of the thread, the kernel stack, the thread environment variables, and the user stack in the virtual address of the process.

Let's take a look at how Microsoft officially explained the channel and postal routes.

MPs queue(PIPE) is the shared memory area used by processes for communication. A process writes information to the pipeline, while other processes can read the information from the pipeline. As its name suggests, a pipeline is a channel for data exchange between processes. The mailslots function is similar to that of MPs queues. It is also a medium for interprocess communications (IPC), but its implementation method is different from that of MPs queues. A Win32-based application can store messages in the mail path, these messages are usually sent to a specified computer or domain name over a network (the domain is a group of workstations or servers that share a group name .) All computers under. You can also use the named pipe instead of the mail path for inter-process communication. The named pipe is most suitable for message transmission between two processes, while the mail channel is more suitable for a process to broadcast messages to multiple processes. Mail routes have an important feature. They use data packets to broadcast messages. Broadcast is a term used in network transmission. It means that the receiver does not send a confirmation message to the sender after receiving the data. Pipeline (the pipeline here refers to the named pipeline. For details about the named pipeline, see below .) The difference is that it is more like making a phone call. You speak to only one client, but you are very clear that your words are heard by the other party. The postal path is also a virtual file stored in the memory, but you must use common Win32 file functions to access it, such as createfile, readfile, and writefile. The data stored in the mail path can be in any form. The only requirement is that it cannot exceed 64 KB. Unlike a disk file, a mail path is a temporary object. When all the handles of a mail path are closed, the mail path and its data are deleted.

There are two types of pipelines:Anonymous and named Pipelines. An anonymous pipeline is not named. It is initially used for communication between the parent process and the child process it starts in the local system. The name pipeline is more advanced. It is identified by a name so that the client and server applications can communicate with each other through it. Moreover, the Win32 naming pipeline can even be used between processes of different systems, making it an ideal choice for many client/server applications.

Just like a pipe connecting two places and delivering water, a software pipe connects two processes and delivers data. Once an MPS queue is created, it can be accessed like a file, and many functions similar to file operations can be used. You can use the createfile function to obtain the handle of an opened pipeline, or another process provides a handle. Use the writefile function to write data to the MPs queue. Later, the data can be read by another process using the readfile function. The MPs queue is a system object. Therefore, you must use the closehandle function to close the MPs queue handle when it is not required.

Anonymous pipelines can only transmit data in one way, while named pipelines can transmit data in two ways. Pipelines can transmit any amount of data in the form of bits. A named pipeline can also combine datasets into a data block called a message. A named pipe can even connect to multiple processes over a network. However, Windows 9x does not support creating named pipelines. It can only be created on Windows NT, Windows 2000, and Windows XP operating systems.

When discussing pipelines, two processes are involved: Customer processes and service processes. The service process is responsible for creating pipelines. The customer process is connected to the MPs queue. A service process can create multiple instances of one MPs queue to support multiple customer processes.

Create an anonymous pipeline using the following functions:

BoolCreatepipe(
Phandle hreadpipe, // read operation handle
Phandle hwritepipe, // write operation handle
Lpsecurity_attributes lppipeattributes, // a structure that describes security information
DWORD nsize // MPs queue size
);

The named pipe is created using the following functions:

Handle Createnamedpipe (
Lptstr lpname, // MPs queue name
DWORD dwopenmode, // pipeline open mode
DWORD dwpipemode, // pipe mode
DWORD nmaxinstances, // maximum number of instances of the MPs queue
DWORD noutbuffersize, // output buffer size
DWORD ninbuffersize, // input buffer size
DWORD ndefatimetimeout, // specify the default timeout value
Lpsecurity_attributes lpsecurityattributes // a structure that describes security information
);

Pipelines are deleted by the following functions:

BoolClosehandle(
Handle hobject // MPs queue handle
);

Other pipeline functions are described as follows:

Callnamedpipe: connects to a named pipe and closes it after reading or writing data.

Connectnamedpipe: the service process prepares a pipeline to connect to the customer process and waits until a customer process is connected.

Disconnectnamedpipe: used by the server to disconnect from the client.

Getnamedpipehandlestate: gets the status information of a named pipeline.

Getnamedpipeinfo: Get the information of a named pipeline.

Peeknamedpipe: Copy data from an anonymous or named pipeline to a buffer zone.

Setnamedpipehandlestate: Set the pipeline type and other status information, such as bit stream or message Stream pipeline.

Transactnamedpipe: reads or writes messages from or to a message queue.

Waitnamedpipe: A customer process is used to connect to a named pipe.

A mail path is created by a mail path service process. After a mail path is created, the mail path handle is returned. When a process needs to read messages from this route, it must provide the handle. Only the process that creates the mail path or obtains the mail path handle using a certain mechanism (such as inheritance) can read messages from the mail path. Unlike an MPS queue, all the routes belong to the local processes that created them. You cannot create a remote route. The customer process of mail has the permission to write messages to mail. Any process can write messages to it as long as it obtains the mail path name. New messages will be placed behind the mail route message queue.

Mail routes can broadcast messages in a domain. If each process in the domain creates a mail path with the same name, they will receive a message sent to the mail path.

If messages transmitted in the mail path are smaller than 425 bytes, they are transmitted in the form of data packets. Messages larger than 425 bytes are transmitted in other ways. In this case, you can only transmit messages from one customer process to one service process, instead, it cannot be transferred from a customer process to multiple service processes. In addition, Windows NT does not support message transmission of more than or equal to 425 bytes.

The mail path is created by the following functions:

HandleCreatemailslot(
Lptstr lpname, // mail path name
DWORD nmaxmessagesize, // maximum Message Size
DWORD lreadtimeout, // read operation timeout
Lpsecurity_attributes lpsecurityattributes // a structure that describes security information
);

The mail path is deleted by the following function:

BoolClosehandle(
Handle hobject // mail handle
);

Other postal functions are described as follows:

Getmailslotinfo: obtains information about a specified route.

Setmailslotinfo: Set information about the specified mail path.

Windows operating systems support both pipe and mail routes. In addition, memory ing files are also an important way for data exchange between processes. However, this article will not detail them further.

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.