Summary of several ways of inter-process communication between Linux--linux kernel anatomy (vii)

Source: Internet
Author: User
Tags message queue posix semaphore

Inter-process Communication overview Process communication purposes
    • Transferring Data
      A process needs to send its data to another process. The amount of data sent is between one byte and a few m bytes

    • Sharing Data
      Multiple processes want to manipulate shared data, a process to share data

    • Notice things
      A process needs to send a message to another process or group of processes. Notifies it (they) that an event has occurred (such as notifying the parent process when the process terminates).

    • resource Sharing
      Share the same resources between multiple processes. In order to do this, the kernel is required to provide a lock and synchronization mechanism.

    • Process Control
      Some processes want complete control over the execution of a process (such as the debug process), at which point the control process wants to be able to intercept the entire sink and exception of a process and be able to know its state changes in a timely manner.

Development of inter-process communication (IPC) for Linux

The process communication methods under Linux are basically inherited from the process communication methods on the UNIX platform. The two major players at T-Lab and BSD (UC Berkeley's Berkeley Software Publishing Center), which have made significant contributions to the development of UNIX, have a different focus on interprocess communication.

The former has systematically improved and expanded the process of communication between the early stages of UNIX, and formed the "system V IPC". The communication process is confined to a single computer;

The latter skips the limit and forms an inter-process communication mechanism based on the socket Interface (socket).

Linux has inherited both.

    • Early UNIX inter-process communication

    • Inter-process communication based on System V

    • Based on socket inter-process communication

    • POSIX interprocess communication.

UNIX inter-process communication includes: pipeline, FIFO, signal.

System V interprocess communication includes: System V Message Queuing, System V Semaphore, System V shared memory

POSIX interprocess communication includes: POSIX Message Queuing, POSIX semaphore, and POSIX shared memory.

Because of the diversity of UNIX version numbers, the Electronic and Electrical Project Association (IEEE) has developed a standalone UNIX standard, a new ANSI UNIX standard called the Portable Operating System interface (Psoix) for the computer environment.

Most of the existing UNIX and popular version numbers follow the POSIX standard. And Linux follows the POSIX standard from the start;

BSD is not not involved in interprocess communication within a single machine (the socket itself can be used for interprocess communication within a single machine).

In fact, very many UNIX version number of the single-machine IPC left with BSD traces. such as 4.4BSD supported anonymous memory mapping, 4.3+BSD to the implementation of reliable signal semantics and so on.

Inter-process communication using Linux
    1. Piping (pipe), flow piping (s_pipe) and well-known piping (FIFO)

    2. Signal (signal)

    3. Message Queuing

    4. Shared memory

    5. Signal Volume

    6. Socket (socket)

Piping (pipe)

There are two kinds of communication methods, one is half-duplex communication, the data can only flow in one direction, and the other is only can be used between the processes with affinity. A process's affinity typically refers to a parent-child process relationship.

Flow pipeline s_pipe: To remove the first limitation, it can be transmitted in two ways.

Pipelines can be used for communication between affinity processes. Named pipes: Name_pipe overcomes the limitations of pipe without name, so that it has no function other than the pipe. It also agreed to communication between unrelated processes;

Signal Volume (Semophore)

A 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. Prevents a process from being interviewed for shared resources. Other processes have also been interviewed for the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.

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. In addition to supporting UNIX early signal semantic function Sigal, Linux also supports the semantics of POSIX.1-compliant signal function sigaction (in fact, the function is based on BSD, BSD in order to achieve a reliable signal mechanism. Also can unify the external interface, uses the Sigaction function once again realizes the signal function);

Message Queuing (Messages queue)

Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers.

Message queue overcomes the shortcomings of less signal transmission information, the pipeline can only carry the unformatted byte stream, and the buffer size is limited.

Message Queuing is a linked table of messages. Contains POSIX Message Queuing system V Message Queuing. A process with sufficient permissions can add messages to the queue, and the process that is given Read permission can read the messages in the queue.

Message queue overcomes the shortcoming that the signal carrying information is small, the pipeline can only carry the unformatted byte stream and the buffer size is limited.

Signal (Singal)

A signal is a more complex form of communication that notifies the receiving process that an event has occurred.

Primarily as a means of synchronizing between processes and between different threads of the same process.

Shared memory

Shared memory is the mapping of memory that can be asked by other processes. This piece of shared memory is created by a process. However, multiple processes can be interviewed. Shared memory is the fastest IPC approach and is designed specifically for inefficient execution of other interprocess communication methods.

It often communicates with other mechanisms. such as signal volume, with use. To enable synchronization and communication between processes.

Enables multiple processes to access the same piece of memory space. is the fastest available IPC form.

is designed for inefficient execution of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores. To achieve inter-process synchronization and mutual exclusion.

Socket (socket)

The socket is also an inter-process communication mechanism, which differs from other communication mechanisms. It can be used for process communication between different machines.

More general inter-process communication mechanisms. Can be used for inter-process communication between different machines.

Originally developed by the BSD branch of the UNIX system, it is now generally possible to migrate to other Unix-like systems: both Linux and System V variants support sockets.

Inter-process communication in various ways efficiency comparison
type No connection Reliable Flow Control Record message Types Priority Level
Ordinary pipe N Y Y N
Flow pipe N Y Y N
Name Pipe (FIFO) N Y Y N
Message Queuing N Y Y Y
Signal Volume N Y Y Y
Shared storage N Y Y Y
UNIX Stream socket N Y Y N
UNIX packet Socket Y Y N N

Note: No connection: There is no need to call some form of open, there is the ability to send messages flow control:

Assuming system resources are scarce or cannot receive many other messages, the sending process can control traffic

Comparison and advantages and disadvantages of various communication methods

    1. Pipe: Slow speed. Limited capacity, only a parent-child process can communicate

    2. FIFO: No matter what process can communicate between, but slow

    3. Message Queuing: The capacity is limited by the system, and the first time you read it, consider the last time you have not finished reading the data

    4. Semaphore: cannot transmit complex messages, only can be used to synchronize

    5. Shared memory Area: very easy to control the capacity, fast, but to maintain synchronization, for example, a process in writing. There is also a process to pay attention to read and write issues, which is equivalent to thread safety in threads. Of course. Shared memory areas can be used as inter-thread communication, but it is not necessary to have shared a piece of memory within the same process between threads.

Suppose the user passes less information or needs to signal to trigger some behavior. The soft interrupt signaling mechanism mentioned above is a simple and effective method for inter-process communication.

But if the amount of information required to pass between processes is greater or there is a need to exchange data between processes, then other means of communication need to be considered.

Nameless pipes are simple and convenient. But limited to the way communication works. And it is only possible to share pipelines between the process that created it and its descendants:

A well-known pipeline can be used in a process that is free of any relationship. But because it exists in the system for a long time. Improper use easy error. So ordinary users are generally not recommended to use.

Message buffering can no longer be limited to parent-child processes. and the consent of the random process through the sharing of Message Queuing to achieve inter-process communication, and by the system call function to achieve the synchronization between message sending and receiving, so that users in the use of message buffering to communicate with no longer need to consider synchronization problems, easy to use. However, the duplication of information requires additional CPU time and is not suitable for situations where there is a large amount or frequent operation.

Shared memory uses the memory buffer to exchange information directly for the disadvantage of message buffering, without copying. Fast and informative are its strengths.

However, shared memory is communicated by attaching a shared memory buffer directly to the virtual address space of the process, so. The synchronization of Read and write operations between these processes is not possible with the operating system. Must be resolved by each process using other synchronization tools. In addition, because the memory entity exists in the computer system. So it can only be shared by processes that are in the same computer system.

Network communication is not convenient.

Shared memory blocks provide a mechanism for efficient two-way communication between arbitrary numbers of processes.

Every user is able to read and write data. However, a protocol must be reached and followed between all programs to prevent the emergence of competing states such as overwrite memory space before reading the information.

Unfortunately, Linux cannot strictly guarantee exclusive access to shared memory blocks, even when you create new chunks of shared memory by using ipc_private, and you cannot guarantee access exclusivity. At the same time, multiple processes that use shared memory blocks must use the same key value in coordination.

Summary of several ways of inter-process communication between Linux--linux kernel anatomy (vii)

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.