Summary of inter-process communication methods

Source: Internet
Author: User
Tags message queue semaphore

Inter-process communication is the transmission or exchange of information between different processes, so what is there between the different processes that can be accessed by both parties? The user space of the process is independent of each other and is generally inaccessible to each other, with the only exception being the shared memory area. However, the system space is a "public place", so the kernel can obviously provide such conditions. In addition, it is a peripheral that can be accessed by both parties. In this sense, two processes can of course also exchange information through ordinary files on disk, or through "registry" or some table entries and records in other databases. Broadly speaking, this is also a means of inter-process communication, but it is generally not counted as "interprocess communication". Because the efficiency of those communication means is too low, and people's requirements for inter-process communication is to have a certain real-time nature.


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


The pipeline consists of three kinds: 1) Common pipe pipes, usually have a limit, one half-duplex, only one-way transmission, and second, can only be used between parent-child processes. 2) Flow pipe s_pipe: Remove the first restriction, can be transmitted in both directions. 3) Named pipes: Name_pipe, which removes the second limitation, can communicate between many unrelated processes.


The three ways in which the system IPC is similar are identified using the identifiers in the kernel.

# pipe: A pipe is a half-duplex mode 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.

# famous pipe (named pipe): A well-known pipe is also a half-duplex mode of communication, but it allows communication between unrelated processes.

# semaphore (Semophore): 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.

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

# signal (sinal): A signal is a more sophisticated means of communication that notifies the receiving process that an event has occurred.

# 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 signal two, to achieve synchronization and communication between processes.

# Socket: Socket is also an inter-process communication mechanism, unlike other communication mechanisms, it can be used for different and inter-process communication.


1: Pipeline with file descriptor, file pointer relationship?

A: In fact, the use of pipelines similar to files, can use Read,write,open and other ordinary IO functions. A pipe descriptor is similar to a file descriptor. In fact, the descriptor used by the pipeline, the file pointer, and the file descriptor will eventually be translated into the system's socket descriptor. are limited by the socket descriptor in the system kernel. Essentially, the Linux kernel source pipeline is implemented by an empty file.


2: How to use the pipe?

A: There are several methods: 1) pipe, create a pipe, and return 2 pipe descriptors. Typically used for communication between parent-child processes. 2) Popen, Pclose: This method returns only one pipe descriptor, which is often used for communication the other party is stdin or stdout; 3) Mkpipe: Named pipes that interact with many processes.


3: Comparison between pipeline and system IPC?

A: The advantage is that all UNIX implementations are supported, and the pipeline is completely removed after the last access pipeline's process terminates, and the flaw is that the pipeline allows only one-way transmission or between parent and child processes.


System IPC: The advantages are powerful, able to communicate between unrelated processes; The flaw is that the keyword key_t uses kernel identities, consumes kernel resources, and can only be explicitly deleted, and cannot use some of the socket's mechanisms, such as select,epoll.

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1829214

Summary of inter-process communication methods

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.