Analysis of Linux Kernel (7) Summary of Several Methods of Communication Between Linux Processes

Interprocess Communication Overview


Purpose of process communication


  • Data transmission
    A process needs to send its data to another process, the amount of data sent is between one byte and several Mbytes

  • Shared data
    Multiple processes want to operate on shared data, one process

  • Notifications
    A process needs to send a message to another or a group of processes to notify it (them) that some event has occurred (eg, to notify the parent process when a process terminates).

  • Resource sharing
    The same resources are shared among multiple processes. To do this, the kernel needs to provide locking and synchronization mechanisms.

  • Process control
    Some processes want to completely control the execution of another process (such as the Debug process). At this time, the controlling process hopes to intercept all traps and exceptions of another process, and be able to know its state changes in time.

Development of Linux Interprocess Communication (IPC)


The process communication method under linux is basically inherited from the process communication method on the Unix platform. The two major contributors to the development of Unix, AT&T's Bell Labs and BSD (Berkeley Software Distribution Center at the University of California, Berkeley), have different emphases on interprocess communication.

The former system improves and expands the early inter-process communication means of Unix, forming "system V IPC", and the communication process is limited to a single computer;

The latter skips this limitation and forms an inter-process communication mechanism based on sockets.

Linux inherits both

  • Early UNIX Interprocess Communication

  • Interprocess communication based on System V

  • Socket-based inter-process communication

  • POSIX interprocess communication.

UNIX inter-process communication methods include: pipes, FIFOs, and signals.

System V inter-process communication methods include: System V message queue, System V semaphore, System V shared memory

POSIX inter-process communication includes: posix message queue, posix semaphore, posix shared memory.

Due to the diversity of Unix versions, the Institute of Electrical and Electronics Engineers (IEEE) developed a separate Unix standard, this new ANSI Unix standard called Portability Operating System Interface for Computing Environments (PSOIX). Most of the existing Unix and popular versions follow the POSIX standard, and Linux follows the POSIX standard from the beginning;

BSD is not not involved in inter-process communication within a single machine (socket itself can be used for inter-process communication within a single machine). In fact, many Unix versions of stand-alone IPC have traces of BSD, such as the anonymous memory mapping supported by 4.4BSD, the implementation of reliable signal semantics in 4.3+BSD, and so on.

Interprocess communication method used by linux


  1. Pipes (pipe), stream pipes (s_pipe) and named pipes (FIFO)

  2. signal

  3. message queue

  4. Shared memory

  5. signal

  6. socket

pipe (pipe)


There are two limitations to the communication method of pipes. One is half-duplex communication, in which data can only flow in one direction, and the other is that it can only be used between processes with affinity. The affinity of a process usually refers to a parent-child process relationship.

Stream pipeline s_pipe: removes the first limitation and can transmit in both directions.

Pipes can be used for communication between processes with affinity. Named pipes: name_pipe overcomes the limitation that pipes have no names. Therefore, in addition to the functions of pipes, it also allows communication between processes without affinity;

Semaphore ( semaphore )


A semaphore is a counter that can be used to control access to a shared resource by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while a process is accessing the resource. Therefore, it is mainly used as a synchronization method between processes and between different threads in the same process.

Signal is a relatively complex communication method, used to notify the receiving process that some event has occurred. In addition to being used for inter-process communication, the process can also send signals to the process itself; linux supports semantic functions in addition to the early Unix signal semantic function sigal. The signal function sigaction that conforms to the Posix.1 standard (actually, this function is based on BSD. In order to realize a reliable signal mechanism, BSD can unify the external interface and re-implement the signal function with the sigaction function);

message queue


A message queue is a linked list of messages, stored in the kernel and identified by a message queue identifier. Message Queuing overcomes the shortcomings of less information in signaling, pipes can only carry unformatted byte streams, and limited buffer size.

A message queue is a linked list of messages, including the Posix message queue system V message queue. Processes with sufficient permissions can add messages to the queue, and processes granted read permissions can read messages from the queue. The message queue overcomes the shortcomings of the signal carrying a small amount of information, the pipeline can only carry unformatted byte streams, and the buffer size is limited.

signal ( singal )


A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.

Mainly as a means of synchronization between processes and between different threads of the same process.

shared memory ( shared memory )


Shared memory is to map a piece of memory that can be accessed by other processes. This shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest way of IPC, and it's designed for the inefficiencies of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

It enables multiple processes to access the same memory space and is the fastest form of IPC available. It is designed for the 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 ( socket )


The socket interface is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for process communication between different machines.

A more general inter-process communication mechanism that can be used for inter-process communication between different machines. Originally developed by the BSD fork of Unix systems, it is now generally portable to other Unix-like systems: both Linux and System V variants support sockets.

Efficiency comparison of various methods of inter-process communication


Types of not connected reliable flow control log message type priority
Ordinary PIPE N AND AND   N
Stream PIPE N AND AND   N
Name PIPE (FIFO) N AND AND   N
message queue N AND AND   AND
signal N AND AND   AND
shared storage N AND AND   AND
UNIX stream SOCKET N AND AND   N
UNIX packet SOCKET AND AND N   N

Note: Connectionless: Refers to the ability to send messages without calling some form of OPEN Flow Control:

If system resources are short or cannot receive more messages, the sending process can perform flow control

Comparison and advantages and disadvantages of various communication methods

  1. Pipeline: slow, limited capacity, only parent and child processes can communicate

  2. FIFO: any process can communicate, but the speed is slow

  3. Message queue: The capacity is limited by the system, and when reading for the first time, you must consider the problem of not reading the data last time

  4. Semaphore: cannot transmit complex messages, can only be used for synchronization

  5. Shared memory area: It is easy to control the capacity, the speed is fast, but it must be kept synchronized. For example, when one process is writing, another process should pay attention to the problem of reading and writing, which is equivalent to thread safety in threads. Of course, the shared memory area is also the same It can be used for inter-thread communication, but it is not necessary. The threads already share a piece of memory in the same process.

If the user transmits less information or needs to trigger certain actions through signals. The soft interrupt signal mechanism mentioned above is a simple and effective way of inter-process communication.

However, if the amount of information required to be passed between processes is relatively large or there is a requirement to exchange data between processes, other communication methods need to be considered.

Anonymous pipeline is simple and convenient. But it is limited to the way of one-way communication. And the sharing of pipes can only be achieved between the process that created it and its descendants:

Although the named pipe can be provided to the process of any relationship. However, because it exists in the system for a long time, it is easy to make mistakes when used improperly. Therefore, it is generally not recommended for ordinary users to use it.

Message buffering can no longer be limited to parent and child processes, but allows any process to achieve inter-process communication by sharing message queues, and use system call functions to achieve synchronization between message sending and receiving, so that users can use message buffering for communication. It is no longer necessary to consider the synchronization problem, and it is easy to use, but the copying of information requires extra CPU time, which is not suitable for occasions with a large amount of information or frequent operations.

In view of the shortcomings of message buffering, shared memory uses the memory buffer to exchange information directly without copying, and its advantages are fast and large amount of information.

However, the communication mode of shared memory is realized by directly attaching the shared memory buffer to the virtual address space of the process. Therefore, the synchronization problem of read and write operations between these processes cannot be realized by the operating system. It must be resolved by the processes using other synchronization tools. In addition, since the memory entity exists in the computer system, it can only be shared by the processes in the same computer system. Inconvenient for network communication.

Shared memory blocks provide a mechanism for efficient two-way communication between any number of processes. Each user can read and write data, but all programs must reach and adhere to certain protocols to prevent race conditions such as overwriting memory space before reading information.

Unfortunately, Linux cannot strictly guarantee exclusive access to shared memory blocks, not even when you create a new shared memory block by using IPC_PRIVATE. At the same time, multiple processes using a shared memory block must coordinate the use of the same key value.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291095&siteId=291194637