Linux and Windows Process Communication (IPC)-Summary of Network Content

Source: Internet
Author: User
Tags posix
Address: http://www.cnblogs.com/raymon/archive/2012/02/18/2357608.html

Http://blog.csdn.net/kesalin/article/details/3887368

1. Linux inter-process communication mode

Inter-process communication is to spread or exchange information between different processes. So what media can be accessed by both sides between different processes? The user space of a process is independent of each other and generally cannot be accessed from each other. The only exception is the shared memory zone. However, the system space is a "public place", so the kernel can obviously provide such conditions. In addition, this is the peripherals that both parties can access. In this sense, the two processes can also exchange information through common files on the disk, or through some table items and records in the registry or other databases. In a broad sense, this is also a means of inter-process communication, but it is generally not counted as "inter-process communication ". Because the efficiency of those communication means is too low, and people need to have a certain degree of real-time requirement for inter-process communication.

In Linux, there are eight methods for Process Communication: pipelines, named queues (FIFO), memory ory ing (mappedmemeory), and message queues.
Queue), shared memory (shared memory), semaphore (semaphore), signal (signal), socket (socket ).

(1) pipeline (PIPE): the pipeline allows one process to communicate with another process with which it has a common ancestor;

(2) Named Pipe (FIFO): similar to a pipe, but it can be used for communication between any two processes. The named pipe has a corresponding file name in the file system. The famous Pipeline overcomes the pipe's no-name restriction. Therefore, it allows communication between unrelated processes in addition to the functions of the pipeline. The named pipe is created by running the mkfifo command or by calling the mkfifo command;

There are three types of pipelines: 1) pipe in a common pipeline, which usually has some restrictions. One is half duplex, which can only be transmitted in one way; the other is that it can only be used between parent and child processes. 2) Stream pipeline s_pipe: removes the first restriction and supports bidirectional transmission.
3) named pipe: name_pipe, which removes the second restriction and can communicate with many unrelated processes.

(3) signal: a signal is a complex communication method used to notify the receiving process that something happens, except for inter-process communication, the process can also send signals to the process itself. In addition to supporting the Unix early signal semantic function signal, Linux also supports the signal function sigaction whose semantics complies with the posix.1 standard (in fact, this function is based on BSD, BSD can implement reliable signal mechanism and unified external interface, and implement the signal function again using the sigaction function );

(4) memory meming (mapped memory): Memory ing allows communication between any process, every process using this mechanism maps a shared file to its own process address space;

(5) Message Queue: A connection table for messages, including POSIX and System V message queues. A process with sufficient permissions can add messages to the queue. A process with the read permission can read messages from the queue. The message queue overcomes the disadvantages such as the small amount of information carried by the signal, the pipe can only be the unformatted byte stream, and the limited buffer size;

(6) semaphore: semaphores are mainly used for synchronization between processes and between different threads of the same process;

(7) shared memory: it allows multiple processes to access the same memory space. It is the fastest available IPC form. This is designed for low operating efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes;

(8) socket: it is a more common inter-process communication mechanism and can be used for inter-process communication between different machines. It was initially developed by the BSD branch of the UNIX system, but now it can be transplanted to other UNIX-like systems: Linux and System
All variants of V Support sockets;

2. Windows inter-process communication mode

2.1 file ing

Memory-mappedfiles allows the Process to Treat the file content as a piece of memory in the process address range. Therefore, the process does not need to use file I/O operations, and can read and modify the file content with simple pointer operations.

Win32 API allows multiple processes to access the same file ing object. Each process receives memory pointers in its own address space. By using these pointers, different processes can read or modify the file content to share data in the file.

There are three ways for an application to share a file ing object with multiple processes.

(1) Inheritance: the first process establishes a file ing object, and its child process inherits the handle of the object.

(2) named file ing: when creating a file ing object, the first process can specify a name for the object (which may be different from the file name ). The second process can use this name to open the file ing object. In addition, the first process can also pass the name to the second process through some other IPC Mechanisms (such as famous pipelines and email slots.

(3) handle replication: the first process establishes a file ing object, and then passes the object handle to the second process through other IPC Mechanisms (such as famous pipelines and email slots. The second process copies the handle to obtain the access permission for the ing object of the file.

File ing is a very effective way to share data among multiple processes and provides better security. However, file ing can only be used between processes on the local machine and not on the network. Developers must also control the synchronization between processes.

2.2 shared memory

In Win32 API, shared memory (sharedmemory) is a special case of file ing. When creating a file ing object, the process uses 0xffffffff to replace the handle, which means that the corresponding file ing object accesses the memory from the file on the operating system page, other processes can open the file ing object to access the memory block. Because shared memory is implemented by file ing, it also has good security and can only run between processes on the same computer.

2.3 Anonymous Pipeline

Pipe is a communication channel with two endpoints: a process with one end handle can communicate with a process with the other end handle. The pipeline can be one-way-one end is read-only, and the other end point is write-only. It can also be two-way. The two ends of a pipeline are both readable and writable.

An anonymous pipeline (anonymous pipe) is a one-way channel that transfers data between a parent process and a child process, or between two child processes of the same parent process. Generally, the parent process creates an MPS queue, and then the child process to communicate inherits the read endpoint handle or write endpoint handle of the channel, and then implements communication. The parent process can also create two or more sub-processes that inherit anonymous pipeline read and write handles. These sub-processes can communicate directly using pipelines without passing through the parent process.

An anonymous pipeline is an effective method for implementing sub-process standard I/O redirection on a single machine. It cannot be used online or between two unrelated processes.

2.4 Named Pipe

A named pipe (Named Pipe) is a one-way or two-way channel for communication between server processes and one or more client processes. Unlike an anonymous pipe, a named pipe can be used between unrelated processes and different computers. When the server creates a named pipe, a name is specified for it, any process can use this name to open the other end of the pipeline and communicate with the server process based on the given permissions.

The named pipeline provides a relatively simple programming interface, making it easier to transmit data over the network than to communicate with two processes on the same computer. However, it cannot communicate with multiple processes at the same time.

2.5 mail slot

Mailslots provides one-way communication between processes, and any process can establish a mail slot as a mail slot server. Other processes, called mail slot 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. Therefore, multiple mail slots can be established to implement bidirectional communication between processes.

You can use the mail slot to send messages to the mail slots on the local computer, the mail slots on other computers, or the mail slots with the same name on all computers in the specified network area. The length of a message for a broadcast communication cannot exceed 400 bytes. The length of a non-broadcast message is limited by the maximum message length specified by the mail slot server.

The mail slot is similar to the named pipe, but it transmits data through unreliable datagram (such as UDP packets in TCP/IP protocol, once a network error occurs, messages cannot be correctly received, while the named pipe transmits data based on reliable connections. However, the mail slot provides simplified programming interfaces and the ability to broadcast messages to all computers in the specified network area. Therefore, the mail slot is another option for applications to send and receive messages.

2.6 clipboard

Clipped board is essentially a group of functions and messages used to transmit data in Win32 APIs. It provides an intermediary for data sharing between Windows applications) -The pasting Mechanism provides a shortcut for different applications to share data of different formats. When a user performs a cut or copy operation in an application, the application puts the selected data in one or more formats on the clipboard. Then, any other application can pick up data from the clipboard and select a suitable format from the given format.

The clipboard is a very loose exchange medium that supports any data format. Each format is identified by an unsigned integer in a standard (predefined) clipboard format, this value is a constant defined by Win32 API. You can use register for non-standard formats.
The clipboard format function is registered as the new clipboard format. You only need to use the clipboard to exchange data in the same format or convert the data to a certain format. However, the clipboard can only be used in Windows-based programs and cannot be used on the network.

2.7 Dynamic Data Exchange

Dynamic Data Exchange (DDE) is a form of inter-process communication in which data is exchanged between applications in the shared content. Applications can use DDE for one-time data transmission, or dynamically exchange data between applications by sending update values when new data appears.

Like clipboard, DDE supports both standard data formats (such as text and bitmap) and custom data formats. However, their data transmission mechanisms are different. One obvious difference is that clipboard operations are almost always used as a one-time response to user-specified operations-for example, selecting the paste command from the menu. Although DDE can also be started by the user, it continues to play a role without further user intervention. DDE has three data exchange methods:

(1) Cold Chain: data exchange is a one-time data transmission, the same as the clipboard.

(2) temperature chain: when data is exchanged, the server notifies the customer, and then the customer must request new data.

(3) Hot chain: when data is exchanged, the server automatically sends data to the customer.

DDE exchange can occur between applications of different computers in a single machine or network. Developers can also define custom DDE data formats for specific IPC between applications, which have more closely coupled communication requirements. Most Windows-based applications support DDE.

2.8 object connection and embedding

Applications Use object connection and Embedding (OLE) technology to manage composite documents (documents composed of multiple data formats ), OLE provides services that make it easier for an application to call other applications for data editing. For example, the word processor supported by Ole can nest workbooks. When you want to edit a workbook, the OLE library can automatically start the workbook editor. When you exit the workbook editor, the table is updated in the original word processor document. Here, the workbook editor becomes an extension of the word processor. If you use DDE, you need to start the workbook editor explicitly.

Similar to DDE technology, most Windows-based applications support OLE technology.

2.9 dynamic Connection Library

Global data in the Win32 dynamic connection library (DLL) can be shared by all the processes that call the DLL, which opens up a new way for inter-process communication, of course, pay attention to synchronization during access.

Although data can be shared between processes through DLL, we do not advocate this method from the perspective of data security. It is better to use shared memory with access control.

2.10 Remote Procedure Call

Remote Procedure Call (RPC) provided by Win32 API enables applications to call functions remotely, which makes Process Communication over RPC over the network as simple as function call. RPC can be used between different processes on a single machine or in the network.

Since the RPC provided by Win32 API complies with the OSF-DCE (opensoftware Foundation distributed computing environment) standard. Therefore, RPC applications written using WIN32API can communicate with RPC applications that support Dec on other operating systems. RPC developers can build high-performance, tightly coupled distributed applications.

2.11 NetBIOS Functions

Win32 API provides NetBIOS functions for low-level network control, which is mainly used to compile interfaces with windows for the IBM NetBIOS system. Unless there are applications with special low-level network functional requirements, it is recommended that other applications do not use the NetBIOS function for inter-process communication.

2.12 sockets

The Windows Sockets specification is a set of network programming interfaces in Windows defined in the example of socket interfaces popular in U. C. Berkeley University bsd unix. In addition to the original library functions of berkeleysocket, a group of functions for windows are also extended, allowing programmers to make full use of the Windows message mechanism for programming.

Currently, more and more network applications are using sockets to implement process communication. The main reason is that sockets is more cross-platform than other IPC Mechanisms. In addition, Winsock
2.0 not only supports TCP/IP, but also supports other protocols (such as IPX ). The only drawback of sockets is that it supports underlying communication operations, which makes it inconvenient to transmit simple data between standalone processes. In this case, the wm_copydata message described below will be more suitable.

2.13 wm_copydata message

Wm_copydata is a very powerful but little-known message. When an application transmits data to another application, the sender only needs to call the sendmessage function. The parameters are the handle of the destination window, the start address of the transmitted data, and the wm_copydata message. The receiver only processes wm_copy as it processes other messages.
Data messages, so that both parties can share data.

Wm_copydata is a very simple method, which is actually implemented through file ing at the underlying layer. Its disadvantage is that it is not flexible and can only be used in a single-host Windows platform.

3. Related Questions

[Faq1: What is the relationship between pipelines, file descriptors, and file pointers ?]

A: In fact, pipelines are used in a similar way as files. They can all use common Io functions such as read, write, and open. Pipeline descriptors are similar to file descriptors.
In fact, the descriptor used by the pipeline,
The file pointer and file descriptor will eventually be converted to the socket descriptor in the system. These are all restricted by the socket descriptor in the system kernel. Essentially, the pipelines in the Linux kernel source code are implemented through empty files.

[Faq2: How do I use pipelines ?]

A: The main methods are as follows: 1) pipe: Create an MPS queue and return two MPs queue descriptors. It is usually used for communication between parent and child processes.
2) popen, pclose: This method returns only one pipeline descriptor, which is often used for stdin or stdout; 3) mkpipe: named pipeline,
Interaction between many processes.

[Faq3: What are the advantages and disadvantages of pipelines and system IPC ?]

A: MPS queue: All UNIX implementations are supported. After the process of the last MPs queue is terminated, the MPs queue is completely deleted; the defect is that the pipeline only allows one-way transmission or is used between parent and child processes.

System IPC: The advantage is that it is powerful and can communicate with unrelated processes. The defect is that the keyword key_t uses the kernel ID, occupies kernel resources, and can only be explicitly deleted, some socket mechanisms, such as select and epoll, cannot be used.

[Faq4: What is the relationship between ipvs Process Communication and Linux Process Communication ?]

A: In fact, most of ipvs Process Communication is transplanted to Unix, ipvs clipboard, and file ing. You can find the shadow from the shared storage of UNIX Process Communication.

[Faq5: What is the relationship between inter-process communication and inter-thread communication ?]

A: Because the entity running on Windows is a thread, inter-process communication in a narrow sense refers to communication between threads belonging to different processes. the thread synchronization problem between a single process can be combined into a special process communication problem. it uses kernel-supported system calls to maintain synchronization between threads. some commonly used thread synchronization methods include: event,
Mutex, semaphores semaphore, and critical zone resources.

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.