Inter-process communication 2. communication methods and their respective features

Source: Internet
Author: User
Commonly used inter-process communication includes pipelines, message queues, and shared memory.

MPs queue
The first pipeline was used to share data between kinship-related processes. The parent process first creates an MPS queue, and then fork () generates a sub-process. The sub-process automatically shares the access permission to the MPs queue. This pipeline has no name (only one identifier in the process is identified), so it is also called an anonymous pipeline.
For example, when we run ls * | grep Foo in shell, shell will create an anonymous pipeline and redirect the stdout of LS to the input end of the pipeline, redirects the stdin of grep to the output end of the pipeline. The output of LS is automatically called the grep input. This is transparent to LS and grep, and they do not know the existence of pipelines.
Later, first in first out (FIFO) emerged, also known as a famous Pipeline. There is a name and an unknown name for the OS, and the OS can be directly managed as a famous name, otherwise it is unknown.After the emergence of FIFO, the pipeline is not only used for communication between kinship processes, but can be used for communication between any processes.
The lifecycle of an MPS queue follows the process. After all the processes that use the MPs queue exit or all the processes display the call close (), the MPs queue is discarded (in fact, the process exit is equivalent to calling close (), because all opened resource identifiers are disabled by OS when the process exits ). If there is still unread data in the pipeline at this time, the data is also discarded. In addition, when writing data to the pipeline, there must be a process for reading data, otherwise there will be no meaning.

Message Queue
A message queue is similar to a pipeline. A process with sufficient write permission can place messages in the message queue. A process with sufficient read permission can read messages from the message queue. Different from the pipeline, a process does not need to wait for the message to arrive in the queue before writing a message to the queue. This is because the message queue declaration cycle follows the kernel. As long as the kernel does not delete the message queue, even if the process exits, the Message Queue still exists. (However, in the implementation of the existing OS, the Message Queue records how many processes have opened itself by reference counting. When all processes exit, the reference count is 0, in this case, the OS automatically deletes the message queue ).

Shared Memory
There are two types of shared memory: anonymous shared memory and nameshared memory. They differ from the pipelines and FIFO mentioned above. The anonymous shared memory can only be used for communication between kinship-related processes. The famous shared memory does not have this limit.
The declared cycle of shared memory also follows the kernel. The biggest difference between the two methods is that data exchange between processes that use shared memory communication does not go through system calls for the process-kernel data copy process, all data exchanges are completed in the memory. Compared with the first two methods, the shared memory method is the fastest.

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.