Inter-process communication and data sharing under Windows

Source: Internet
Author: User
Tags ole

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.

Process Communication Methods

1 File Mappings
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 Shared 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.


3 Anonymous Pipelines
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. The pipe can be unidirectional-one end is read-only, the other end is write-only, or bidirectional-the ends of the pipe are 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.


4 Named 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.


5 Mail 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.


6 clipboard
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 cut (copy) -The paste mechanism provides a shortcut for sharing different format data between different applications. 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.


7 Dynamic 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.


8 Object 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.


9 Dynamic 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.


10 Remote Procedure Calls
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.


One-to-one NetBIOS function
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.


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.


Wm_copydata News
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.

Cloud-Wind Blog: http://blog.codingnow.com/2005/10/interprocess_communications.html

Related Article

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.