Inter-process Communication mode

Source: Internet
Author: User
Tags message queue ole

1. Inter-process communication mode(1) How Windows processes communicate with each other

1, file mapping;

2, Shared memory (is a special case of file mapping);

3, mail slot (mailslot) (Point-to-point message queue);

4, anonymous pipeline;

5, named pipe;

6, clipboard;

7, Dynamic Data exchange;

8, object linking and embedding;

9, remote procedure call;

10, dynamic link library;

11, socket;

12, Wm_copydata.

(2) How Linux processes communicate with each other

1. Pipeline

2. Signal Volume

3. Shared memory

4. Message Queuing

5. Socket SOCKET

6. Signal

(3) Common inter-process communication between Windows and Linux

1. Messages (called Signals in Linux)

2. Shared memory

3. Postal Slots

4. Pipeline

5. Socket

2. How Windows processes communicate with each other1. File 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 give the object a name (different from the file name) when it establishes the file mapping object. 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 copy: 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 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.

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

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

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

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.

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

3. How to communicate between Linux processes1. Piping (pipe)

A pipeline is a half-duplex method of communication in which data can only flow in one direction and can only be used between processes that have affinity. A process's affinity usually refers to a parent-child process relationship.

2. Famous pipe (named pipe)

A well-known pipe is also a half-duplex mode of communication, but it allows communication between unrelated processes.

3. Signal Volume (Semophore)

A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.

4. Message Queuing (Messages queue)

Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.

5. Signal (sinal)

A signal is a more sophisticated form of communication that notifies the receiving process that an event has occurred.

6. Shared Memory

Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

7. Socket (socket)

The socket is also an inter-process communication mechanism, which differs from other communication mechanisms in that it can be used for process communication between and among different processes.

Inter-process Communication mode

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.