Inter-process Communication mode

Source: Internet
Author: User
Tags message queue ole semaphore

Inter-process Communication mode

This section of reference 2: detailed content open links to see

Programmers must reconcile the set of processes that have dependencies in order to achieve the common goal of the process. Two techniques can be used to achieve coordination. The first technique passes information between two processes that have a communication dependency. This technique is called interprocess communication (interprocess communication). The second technique is synchronization, which is used when the processes are mutually dependent on each other. These two types of dependencies can exist at the same time.

Typically, a process has a separate address space. We can understand this by understanding the sequence of mappings that can be built after the executable program is loaded into memory. This means that if we have two processes (process a and process B), the data declared in process A is not available for process B. Also, process B does not see the events that occur in process a, and vice versa. If processes A and B work together to accomplish a task, there must be a way to communicate information and time between two processes. We can take a look at the basic process components here. Note the process has a text, data, and stack fragment. The process may also have additional memory allocated from the free storage space. The data that a process occupies is typically in a data fragment, a stack fragment, or a process's dynamically allocated memory. Data is protected for other processes. in order for a process to access data from another process, it must end up using an operating system call. Similarly, in order for a process to know about events that occur in a text fragment in another process, it is necessary to establish a communication pattern between processes. This also requires help from the operating system API. When a process sends data to another process, it is called IPC (interprocess communication, interprocess communication).

Here are some of the different types of interprocess communication:

Inter-process communication Describe
environment variable/file descriptor The child process accepts a copy of the parent process environment data and all file descriptors. The parent process can set certain variables in its data fragment or environment, while the child processes receive these values. The parent process can open the file while advancing the position of the read/write pointer, and the child process accesses the file with the same offset.
Command-line arguments Command-line arguments can be passed to child processes during the call to exec or derived functions.
Pipeline Used for communication between related and unrelated processes, and forms a communication channel between two processes, usually accessed using a file read-write program.

Shared memory (Dynamic data exchange)

Using client/server model (c/s), server-to-customer data

or action request to respond.

Dde Two processes outside of the memory block, two processes can access it.

Environment variables, file descriptors:

When a child process is created, it accepts a copy of many resources from the parent process. The child process accepts the parent process's text, stack

And a copy of the data fragment. The child process also accepts the parent process's environment data and copies of all file descriptors. Wahabbi

The process of inheriting resources from the parent process creates an opportunity for interprocess communication. The parent process can be in its data fragment or ring

Setting a certain variable in the environment, the child process accepts these values. Similarly, the parent process can also open a file and advance to

The desired position within the file, and the child process can then access the file at the exact location where the parent process leaves the read/write pointer.

The flaw of this type of communication is that it is unidirectional, one-time communication. This means that, in addition to the file description, if the child process

Inherits any other data and is just all the data copied by the parent process. Once a subprocess has been created, the child process

Any changes to these variables are not reflected in the data of the parent process. Similarly, after a child process is created, the data for the parent process

Any changes are not reflected in the child process. So, this type of interprocess communication is more like baton passing. Once the parent enters

The process passes a copy of some resources, and the child processes use them independently and must use the original delivery resources.

Second, the command line parameters:

Another unidirectional, one-time interprocess communication can be accomplished through command-line arguments (command-line argument)

My previous article already mentions the use of command-line arguments. Command-line arguments call an exec or derived call to the operating system

Passed to the child process. Command-line arguments are typically passed to exec as a null-terminated string in one of the parameters

or derived function calls. These functions can pass values to a child process in a one-way, disposable fashion. Windows has a call to execute

EXE program's API. You can refer to the Shellexecutea function related.

Third, Pipeline communication:

Inheriting resources and command-line arguments are the simplest form of interprocess communication. They also have two main limitations. In addition to files

Descriptors, inheriting resources are one-way, single-time forms of IPC. The pass-through command parameter is also one-way, disposable IPC

Method. These methods are also restricted to the associated process, and if not associated, command-line arguments and inherited resources cannot be used. Also

There is another structure, called pipe, which can be used to communicate between associated processes and between unrelated processes.

A pipeline is a data structure that is accessed like a serialized file. It forms a communication channel between the two processes.

The pipeline structure is accessed by using text and write methods. If process a wants to send data to process B through a pipeline, then

Process a writes data to the pipeline. In order for process B to receive this data, process B must read the pipe, with the command-line arguments

The IPC form is not the same. Pipelines can communicate in two directions. The flow of data between two processes is two-way communication. The pipeline can be in the program's

Used throughout the execution period to send and receive data between processes. Therefore, pipelines act as one of the processes that are accessible to pipelines

Live links, there are two basic types of pipelines:

1. Anonymous pipeline

2. Named pipes

The above figure shows that two processes cannot be written to each other without a pipe.

Once the pipelines are established, they can communicate with each other.

Iv. Shared Memory

Shared memory can also be used for inter-process communication. A process needs a block of memory that can be browsed by other processes. Other processes that want to access this memory block request access to it, or the process that created it grants permission to access the memory block. All processes that can access a specific block of memory have immediate visibility into it. Shared memory is mapped to the address space of each process that uses it. So, it looks like another variable that is declared within the process. When a process writes shared memory, all processes immediately know what is written and can be accessed.

The relationship of shared memory between processes is similar to that of global variables between functions. All functions in a program can use the value of a global variable. Similarly, shared memory blocks can be accessed by all processes that are executing. Memory blocks may share a logical address, and processes can share some physical addresses.

The creation of a shared memory block must be done by a system API call. In a WIN32 environment, the use of createfilemapping (), MapViewOfFile (), and the Mapviewoffileex () API can be done well.

The shared memory allocation is within the 2~3GB address range of the WIN32 system. Once MapViewOfFile () and Mapviewoffileex () are called, all processes that share a file-mapped object can immediately access this block of memory and, if needed, can read and write to this block of memory.

V. Dynamic Data exchange

Dynamic Data exchange is one of the most powerful and sophisticated forms of interprocess communication available today. Dynamic data Exchange uses messaging, shared memory, transaction protocols, client/server categories, synchronization rules, and session protocols to allow data and control information to flow across processes. The basic model of the Dynamic Data exchange session, DDE, is the customer, the server. The server responds to data or actions from the customer. Customers and servers can communicate in a variety of relationships.

A server can communicate with any number of customers. A customer can also communicate with any number of servers. A single DDE agent can be either a customer or a server. In other words, a process can request a service from a DDE proxy that is performing a service for another process.

The next section references 1:

Inter-process communication mainly includes pipelines, system IPC (including message queue, semaphore, shared storage), SOCKET.

Windows System interprocess Communication

Windows provides a variety of mechanisms to enable fast and easy sharing of data and information between applications. These mechanisms include RPC, COM, OLE, DDE, messages, clipboard, mail slots, pipelines, sockets, and so on . However, if you are communicating between multiple processes on the same machine, the mechanism above is related to shared memory. This is called a memory-mapped file on Windows.

This data-sharing mechanism is implemented by having two or more processes map the same view of the same file-mapping object, which means that the same physical storage pages are shared between processes . Therefore, when a process writes data in a view of a file-mapped object, other processes see changes immediately in their views. However, for multiple processes to share the same file-mapping object, the names of the file-mapping objects used by all processes must be identical [1].

Inter-process communication between Linux systems

The process communication methods under Linux are basically inherited from the process communication methods on the UNIX platform. The two main players at T-Lab and BSD (UC Berkeley's Berkeley Software Release Center), which have made significant contributions to the development of UNIX, have a different focus on interprocess communication. The former makes a systematic improvement and expansion of the inter-process communication means of UNIX, which forms the "system V IPC", the communication process is confined to a single computer, the latter skips the limit and forms the inter-process communication mechanism based on socket. Linux has inherited both.

Inter-process communication mechanisms under Linux generally include: pipelines, signals (becoming messages on Windows), signal queues (actually message lists), shared memory, semaphores, sockets.

Common

from the above analysis, it can be seen that two operating systems are common and use more inter-process communication mechanisms are: pipelines, messages, shared memory and sockets. Here's an introduction:

Pipe and well-known pipe (named pipe): Pipelines can be used for communication between affinity processes, and well-known pipelines overcome the limitations of pipe without name, so that, in addition to having the functionality of a pipeline, it allows for inter-process communication without affinity.

Signal or message (Signal): A signal is a more complex form of communication that notifies the receiving process of an event that, in addition to interprocess communication, can send a signal to the process itself.

Shared memory: Allows multiple processes to access the same piece of memory space and is the fastest available IPC form. is designed for inefficient operation 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.

Socket: A more general inter-process communication mechanism that can be used for inter-process communication between different machines.

Summarize

Message queue is a linked table of messages, message queue overcomes the lack of information, the pipeline can only host unformatted byte stream and buffer size limited, and the semaphore is mainly used as synchronization between process and different threads of the same process.

Personally, pipelines, the Clipboard, and shared memory are all based on shared memory. Messages, message queues, and sockets are all based on the message-sending mechanism, except that the message is sent on the same machine (the socket can be seen as sending a message on the network). As for RPC, COM, OLE, DDE is primarily a unified interface generated on the client and server side. In other words, these means of communication are actually equivalent to the protocol on the network. protocols that communicate at the same level regardless of the next layer, as if these two layers directly establish a connection. The basic idea is to create code that resembles stub code locally, and to connect it remotely by building code similar to the post code.

In this way, the Clipboard and the anonymous pipe can only achieve communication between two processes on the same machine, while semaphores, messages, message queues, and shared memory can only communicate between multiple processes on the same machine, but named pipes, mail slots, and sockets can not only realize the communication of two processes on the same machine, Also can achieve inter-process communication across the network, in addition, the oil trough and sockets can achieve one-to-many communication, and the named pipe can only be point-to-point single communication, but the disadvantage of the oil trough is a small amount of data, usually under 424 bytes, if the data volume is large, you can use named pipes and sockets to complete. all in all: sockets are undoubtedly the best means of communication in cross-network communication, which may be the original intention of Berkeley to develop sockets for network communication (traditional communication methods are not good).

Reference:

1:http://blog.sina.com.cn/s/blog_6002b97001013wec.html

2:http://blog.csdn.net/liuzhanchen1987/article/details/7452910

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.