1 process and Process communication
A process is a program that is loaded into memory and ready to execute, each with a private virtual address space, consisting of code, data, and the system resources (such as files, pipelines, and so on) that it can take advantage of. Multi-process/multithreading is a basic feature of the Windows operating system. The Microsoft WIN32 application Programming Interface (application Programming Interface, API) provides a number of mechanisms to support data sharing and exchange among applications that exercise activities called interprocess communication (interprocess Communication, IPC), process communication means data sharing and data exchange between different processes.
Because there are many ways to use the Win32 API for process communication, how to choose the proper communication mode becomes an important problem in application development, and the following article will analyze and compare several methods of process communication in Win32.
2 Process Communication Methods
2.1file Mapping
File mapping (memory-mapped files) allows a process to treat the file content as if it were a block of memory in the process address range. Therefore, the process does not have to use file I/O operations and can read and modify the contents of the file with a simple pointer operation.
The Win32 API allows multiple processes to access the same file mapping object, and each process receives a pointer to memory in its own address space. By using these pointers, different processes can read or modify the contents of the file, enabling the sharing of the data in the file.
There are three ways for an application to share a file-mapping object with multiple processes.
(1) Inheritance: The first process establishes a file-mapping object whose child processes inherit the handle of the object.
(2) Named file Mapping: The first process can assign a name to a file-mapped object (different from the file name) when the object is created. The second process can open this file mapping object by this name. In addition, the first process can pass a name to the second process through some other IPC mechanisms (known as pipelines, mail slots, etc.).
(3) Handle replication: The first process establishes a file mapping object, and then passes the object handle to the second process through other IPC mechanisms (known as pipelines, mail slots, and so on). The second process copies the handle to gain access to the file-mapped object.
File mapping is a very efficient way to share data across multiple processes and has better security. However, file mappings can only be used between processes on the local machine, not on the network, and developers must also control synchronization between processes.
2.2Shared Memory
Shared memory in the Win32 API is actually a special case of file mapping. When a process creates a file-mapped object with 0xFFFFFFFF instead of a file handle (HANDLE), it indicates that the corresponding file-mapping object accesses the memory from the operating system paging file, and the other process opens the file-map object to access the memory block. Because shared memory is implemented with file mappings, it also has better security and can only run between processes on the same computer.
2.3Anonymous Pipeline
A pipe is a communication channel with two endpoints: a process with one end handle can communicate with a process that has a handle on the other end. A pipe can be unidirectional-one end is read-only, the other end is write-only, or a two-way pipe can be both readable and writable.
An anonymous pipe (Anonymous pipe) is a non-named one-way pipeline that transmits data between a parent process and a child process, or two child processes of the same parent process. The pipeline is typically created by the parent process, which then inherits the read endpoint handle or write endpoint handle of the channel, and then implements communication by the child process to communicate. The parent process can also establish two or more child processes that inherit the read and write handles of an anonymous pipe. These child processes can communicate directly using pipelines and do not need to pass through the parent process.
Anonymous pipelines are an effective way of implementing child process standard I/O redirection on a single machine, which cannot be used on-line or between two unrelated processes.
2.4Named Pipes
A named pipe (Named pipe) is a one-way or two-way pipeline that communicates between a server process and one or more client processes. Unlike anonymous pipes, named pipes can be used between unrelated processes and between different computers, and when the server establishes a named pipe, it assigns a name to it, and any process can open the other end of the pipeline by that name, communicating with the given permissions and the server process.
Named pipes provide a relatively simple programming interface that makes it more difficult to transmit data over the network than between two processes on the same computer, but it is not enough to communicate with multiple processes at the same time.
2.5Mail Slots
Mail Slots (mailslots) provide one-way communication between processes, and any process can establish a mail slot as a mail slot server. Other processes, called mail slots customers, can send messages to the mail slot server process by the name of the mail slot. The incoming message is kept in the mail slot until the server process reads it. A process can be either a mail slot server or a mail slot customer, so multiple message slots can be established to enable two-way communication between processes.
Mail slots enable you to send messages to a mail slot on the local computer, to a mail slot on another computer, or to a message slot with the same name on all computers in the specified network area. The message length of a broadcast communication cannot exceed 400 bytes, and the length of the non-broadcast message is limited by the maximum message length specified by the message slot server.
A message slot is similar to a named pipe, but it transmits data through unreliable datagrams (such as UDP packets in the TCP/IP protocol), and when a network error fails to guarantee that the message is received correctly, the named pipe transfer data is based on a reliable connection. However, the mail slot has a simplified programming interface and the ability to broadcast messages to all computers in the specified network area, so the mail slot is another option for the application to send and receive messages.
2.6Clip Board
The Clipboard (clipped Board) is essentially a set of functions and messages in the Win32 API that are used to transfer data, providing a mediator for data sharing between Windows applications, and Windows has established a cut (copy)-paste mechanism that is not Sharing different format data with the application provides a shortcut. When a user performs a cut or copy operation in an application, the application places the selected data in one or more formats on the Clipboard. Any other application can then pick up the data from the Clipboard and choose the format that suits you in the given format.
The Clipboard is a very loosely exchanged medium that can support any data format, identified by an unsigned integer, for standard (predefined) clipboard formats, which are constants defined by the Win32 API, and register Clipboard for non-standard formats The Format function registers as the new clipboard formats. The data exchanged using the Clipboard can only be done in the same format or in a format. However, the Clipboard can only be used in Windows-based programs and cannot be used on the network.
2.7Dynamic Data Exchange
Dynamic Data Exchange (DDE) is an inter-process communication form that uses data exchange between applications that exist within a share. Applications can use DDE for one-time data transfer, or they can dynamically exchange data between applications by sending updated values when new data is present.
DDE, like the Clipboard, supports both standard data formats (such as text, bitmaps, and so on) and the data formats that you define. But their data transfer mechanism is different, one obvious difference is that clipboard operations are almost always used as a one-time response to user-specified actions-such as selecting the Paste command from the menu. Although DDE can also be initiated by the user, it continues to function generally without the user further intervention. DDE has three ways of exchanging data:
(1) Cold chain: Data Exchange is a one-time data transfer, the same as the Clipboard.
(2) Win Chen: When the data Exchange Server notifies the customer, then the customer must request new data.
(3) Heat chain: The server automatically sends the data to the customer when the data is exchanged.
DDE interchanges can occur between applications on a single machine or on a different computer in the network. Developers can also define custom DDE data formats for special purpose IPC between applications, which have more tightly coupled communication requirements. Most Windows-based applications support DDE.
2.8Object Linking and Embedding
The application uses object linking and embedding (OLE) technology to manage composite documents (documents consisting of multiple data formats), and OLE provides services that make it easier for an application to invoke other applications for data editing. For example, OLE supported word processors can nest spreadsheets, and the OLE library can automatically start the spreadsheet editor when the user wants to edit the spreadsheet. When the user exits the spreadsheet editor, the table has been updated in the original Word processor document. Here the Spreadsheet editor becomes an extension of the word processor, and if DDE is used, the user explicitly launches the spreadsheet editor.
As with DDE technology, most Windows-based Applications support OLE technology.
2.9Dynamic Connection Library
The global data in the WIN32 Dynamic Connection library (DLL) can be shared by all processes that call the DLL, which in turn opens up a new way for interprocess communication and, of course, to be aware of synchronization issues when visiting.
Although you can use DLLs for inter-process data sharing, from a data security standpoint, we do not advocate this approach, and using shared memory with access control is a better approach.
2.10Remote Procedure Call
The Remote Procedure call (RPC) provided by the Win32 API enables applications to use remote calling functions, which makes process communication with RPC on the network as simple as a function call. RPC can be used either on a single machine or in a network.
Because the RPC provided by the Win32 API obeys the OSF-DCE (Open software Foundation distributed Computing Environment) standard. So RPC applications written through the Win32 API can communicate with DEC-capable RPC applications on other operating systems. Using RPC developers, you can build high-performance, tightly coupled distributed applications.
2.11 NetBiosfunction
The WIN32 API provides NetBIOS functions for handling low-level network control, primarily writing interfaces to Windows for IBM NetBIOS systems. Unless applications with special low-level network functionality are required, it is best that other applications do not use NetBIOS functions for interprocess communication.
2.12 Sockets
The Windows Sockets specification is a set of network programming interfaces under Windows defined by the socket interface that is popular in the U.c.berkeley University BSD Unix paradigm. In addition to the original library functions of the Berkeley socket, a set of functions for Windows has been extended to enable programmers to fully utilize the Windows Messaging mechanism for programming.
More and more network applications are now implemented through sockets for process communication, mainly because sockets is much better cross-platform than other IPC mechanisms, and Winsock 2.0 supports not only the TCP/IP protocol, but also other protocols such as IPX. The only drawback of sockets is that it supports the underlying communication operations, which makes simple data transfer between single-machine processes less convenient, and the WM_COPYDATA message described below will be more appropriate.
2.13 Wm_copydatamessage
Wm_copydata is a very powerful but little-known message. When an app transmits data to another app, the sender simply uses the call SendMessage function, which is the handle to the destination window, the starting address for passing the data, and the WM_COPYDATA message. The receiver simply processes the WM_COPY data message just as it does with other messages, so that data sharing is achieved between the sending and receiving parties.
Wm_copydata is a very simple method, which is actually implemented by file mapping at the bottom. Its disadvantage is that the flexibility is not high, and it can only be used in a single machine environment for Windows platforms.
3 Concluding remarks
The Win32 API provides so many options for application-interprocess communication, so how do developers make the choice? It is common to consider the following questions before deciding which IPC method to use, such as whether the application works in a network environment or in a stand-alone environment.
http://blog.csdn.net/zhanghefu/article/details/4420623
Analysis and comparison of inter-process communication between WIN32 (13 methods)