Inter-process communication between Linux

Source: Internet
Author: User
Tags message queue posix unix domain socket

Purpose of inter-process communication
    1. Data exchange
    2. State synchronization
    3. Event notification
    4. Resource sharing
    5. Process Control

In general, the communication between processes can be divided into two types, one is Transmission control information, the other is the transmission of large amounts of data. General control information is only one or a few bytes, to achieve high-speed execution of process control operations (such as the use of semaphores to achieve process synchronization); The transfer of large amounts of data is typically used to exchange information between processes (the CS architecture, the interaction of large amounts of data between the client and server) [4].

Development process

Inter-process communication (IPC) in Linux is derived from UNIX, with the initial UNIX pipeline, FIFO, signal, and later through Bell Labs and BSD (UC Berkeley's Berkeley Software Release Center) to develop the current process Communication Foundation.

Bell Labs expanded the way early UNIX processes were communicated, forming the "System V IPC", which was confined to a single computer; BSD broke the limit and formed a socket-based interprocess communication mechanism. Linux has inherited both features, and it is also compatible with the POSIX IPC standard (the POSIX standard is now more generic).

Communication mode
    • Pipeline
    • Signal
    • Message Queuing
    • Signal Volume
    • Shared memory
    • Sockets
Piping (pipe)

The pipeline is one of Unix's contributions to the operating system, which is a ring buffer that allows two processes to communicate in a producer/consumer mode, which is a first-in-one-out queue [2].

A pipeline is a half-duplex method of communication (messages can only be transmitted in one Direction). Data transmission is similar to streaming, does not provide random access, between processes to contract a set of data protocol to resolve the transmitted data [5]. The data is buffered by the operating system until the receiver reads [1], which can be used as a file descriptor to listen through the Select,poll and Epoll functions.

Pipelines need to be co-ordinated during the build process (some APIs block when the pipeline is opened, until both sides perform the open operation) and cannot exist independently of the process.

Pipelines are divided into anonymous pipes and named pipes (Named pipe): Anonymous pipes can be shared only between the parent and child processes, or between child processes and child processes, and no affinity processes can communicate only through named pipes.

FIFO (first-in first-out special file, named pipe) belongs to a named pipe in Linux. Allows multiple processes to be accessed by binding the file system to the specified file instead of the standard input and output [1].

In Unix, the pipeline is automatically mutex-protected, and only one process can access the pipeline at a time [2].

Signal (signal)

A signal is used to notify a process of the occurrence of an event . It can be sent to the process itself, in addition to interprocess communication. Because the signal cannot be used to transmit data, the signal is mainly used for process management.

The kernel in Unix treats all signals equally, and for simultaneous signals, only one signal to the process at a time, without a specific order. The process can send signals to each other, and the kernel may also send a signal internally [2].

We often use Ctrl + C to send an exit signal to a process.

Message Queue

Message Queuing functions like a mailbox , and it can also be used between unrelated processes, communicating without a direct connection between the two parties [1]. Message Queuing is directly facing the message, the sender of the message specifies the type and size of the message being sent, and the recipient of the message specifies how the message is received (FIFO rule or specified message type).

It overcomes the disadvantage that the signal carrying information is small and the pipeline can only carry the unformatted byte stream , and can realize the communication between the processes (C/S mode). Even if the recipient does not open the message queue, the sender can still open the queue to send messages without blocking, without the need for additional synchronization operations, to avoid open and read-write blocking issues in the pipeline.

There is an upper limit on the number of messages stored in Message Queuing, and there is an upper limit on the size of each message.

Shared memory

[5] Multiple processes map the same shared memory segment to their own process resources, and read and write data to the same memory to interact with the data, which is the fastest way to communicate between processes and is often mixed with other communication mechanisms (semaphores) to achieve the purpose of interprocess communication.

Pipe and FIFO as well as Message Queuing are required to copy the data from the sender to the kernel cache, and then copy from the kernel cache to the receiver [6] at the time of the interactive information, and copy two times for one communication. Shared memory does not need to be copied through the kernel, so it is faster than pipe, FIFO, and Message Queuing.

Shared memory is often used in business situations where real-time requirements are high, such as in a financial system, but involves distributed services, often requiring cross-host scenarios for shared memory.

Socket (socket)

Often used for cross-host process communication, which was first provided by BSD and later absorbed by Linux. It has TCP/IP sockets (stream-oriented data) and UDP sockets (message-oriented) that communicate across hosts over network ports, as well as UNIX domain sockets, a type that can only be used locally.

Unix domain sockets are similar to TCP/IP Internet sockets, but all traffic is done in the kernel, using the file system as their address space. The process is used as an inode to use a UNIX domain socket, and multiple processes can communicate with the same socket (the Docker service listens for local requests through a UNIX domain socket) [1].

A socket can be used for two-way communication between processes.

Signal Volume

A communication mechanism used to synchronize between different processes, or between different threads in the same process. The PV primitives are used to achieve this.

Reference links
    • [1] https://en.wikipedia.org/w/index.php?title=Inter-process_communication&oldid=833805357

    • [2] "Operating system and Essence" 6.7 Unix concurrency mechanism

    • [3] Https://www.ibm.com/developerworks/cn/linux/l-ipc/index.html

    • [4] "Computer operating system Tutorial" Tsinghua Edition 3rd edition Zhang Yao 3.7 interprocess communication

    • [5] https://www.ibm.com/developerworks/cn/aix/library/au-spunix_sharedmemory/

    • [6] Http://www.kohala.com/start/unpv22e/unpv22e.chap12.pdf

Inter-process communication between Linux

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.