Linux Kernel Analysis Based on several methods of inter-process communication in Linux (7)
Inter-process communication Overview
The purpose of Process Communication
Data Transmission
A process needs to send its data to another process. The data volume sent is between one byte and several Mb.
Shared data
Multiple processes want to operate on shared data.
Notification
A process needs to send a message to another process or a group of processes to notify it of an event (such as notifying the parent process when the process is terminated ).
Resource Sharing
Multiple processes share the same resource. To achieve this, the kernel needs to provide a lock and synchronization mechanism.
Process Control
Some processes want to completely control the execution of another process (such as the Debug process). At this time, the control process wants to block all the traps and exceptions of another process and be able to know its status changes in time.
Development of inter-process communication (IPC) in Linux
The process communication methods in linux are basically inherited from the process communication methods on the Unix platform. The Bell Labs and BSD (Berkeley Software release center at the University of California Berkeley), which have contributed significantly to the development of Unix, have different emphasis on inter-process communication.
The former improves and expands the inter-process communication methods in the early days of Unix to form a "system v ipc", and the communication process is limited to a single computer;
The latter skips this restriction and forms a socket-based inter-process communication mechanism.
In Linux, the two are inherited.
Early UNIX Process Communication
Inter-process communication based on System V
Inter-process communication based on Socket
POSIX inter-process communication.
The communication methods between UNIX processes include pipelines, FIFO, and signals.
System V inter-process communication methods include: System V message queue, System V signal light, and System V shared memory
POSIX inter-process communication includes posix message queue, posix signal lamp, and posix shared memory.
Due to the diversity of Unix versions, the Institute of electronic and electrical engineering (IEEE) developed an independent Unix standard, this new ANSI Unix standard is called the portable operating system interface (PSOIX) in the computer environment ). Most of the existing Unix and popular versions follow the POSIX standard, while Linux has followed the POSIX standard from the very beginning;
BSD is 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 Single-host IPC have traces of BSD, such as the anonymous memory ing supported by 4.4BSD and the realization of reliable signal semantics by 4.3 + BSD.
Inter-process communication in linux
Pipe, s_pipe, and FIFO)
Signal (signal)
Message Queue
Shared Memory
Semaphores
Socket)
Pipeline (pipe)
Pipeline communication has two restrictions: one is half-duplex communication, data can only flow in one way, and the other is only used between unrelated processes. The kinship of a process usually refers to the parent-child process relationship.
Stream pipeline s_pipe: removes the first restriction and supports bidirectional transmission.
The pipe can be used for communication between kinship processes. The named pipe: name_pipe overcomes the pipe's no name restriction. Therefore, apart from the pipeline's functions, it also allows communication between unrelated processes;
Semaphores (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 lock mechanism to prevent other processes from accessing a shared resource. Therefore, it is mainly used for synchronization between processes and between different threads in the same process.
Signals are complex communication methods used to notify the receiving process of an event. In addition to inter-process communication, processes can also send signals to the process itself; in addition to the early Unix signal semantic function sigal, linux also supports the signal function sigaction whose semantics complies with the Posix.1 standard (in fact, this function is based on BSD, BSD in order to achieve reliable signal mechanism, it can also unify external interfaces and implement the signal function again using the sigaction function );
Message queue)
A message queue is a linked list of messages stored in the kernel and identified by the Message Queue identifier. The message queue has overcome the disadvantages of few signal transmission information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
Message Queue is a chain table of messages, including Posix Message Queue system V message queue. A process with sufficient permissions can add messages to the queue. A process with the read permission can read messages from the queue. The message queue overcomes the disadvantages of low signal carrying information, and the pipeline can only carry unformatted byte streams and limited buffer size.
Signal (singal)
A signal is a complex communication method used to notify the receiving process that an event has occurred.
It is mainly used for synchronization between processes and between different threads of the same process.
Shared memory)
Shared memory is the memory mapped to a segment that can be accessed by other processes. The shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specially designed for the low efficiency of communication between other processes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
This allows multiple processes to access the same memory space, which is the fastest available IPC form. It is designed to reduce the running efficiency 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)
The Interprocess interface is also a communication mechanism between processes. Unlike other communication mechanisms, it can be used for process communication between different machines.
A more general inter-process communication mechanism can be used for inter-process communication between different machines. It was initially developed by the BSD branch of the Unix System, but now it can be transplanted to other Unix-like systems: both Linux and System V variants support sockets.
Efficiency Comparison of various inter-process communication modes
| Type |
No connection |
Reliable |
Flow Control |
Record Message Type |
Priority |
| Normal PIPE |
N |
Y |
Y |
|
N |
| Stream PIPE |
N |
Y |
Y |
|
N |
| Name PIPE (FIFO) |
N |
Y |
Y |
|
N |
| Message Queue |
N |
Y |
Y |
|
Y |
| Semaphores |
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: you do not need to call some form of OPEN to have message sending capability Flow Control:
If the system resources are insufficient or cannot receive more messages, the sending process can control the traffic.
Comparison and advantages and disadvantages of various communication modes
MPs queue: slow and limited capacity. Only Parent and Child processes can communicate with each other.
FIFO: Any process can communicate with each other, but the speed is slow.
Message Queue: The capacity is limited by the system. Note that when reading data for the first time, you must consider the problem of not reading data for the last time.
Semaphores: complex messages cannot be transmitted and can only be synchronized.
Shared Memory zone: it can easily control the capacity and speed, but must be synchronized. For example, when a process is writing, the other process must pay attention to the read/write issue, which is equivalent to thread security, of course, the shared memory area can also be used for inter-thread communication, but this is not necessary. threads have already shared a piece of memory in the same process.
The Soft Interrupt signal mechanism mentioned above is a simple and effective method for inter-process communication if the user transmits less information or needs to trigger certain behaviors through signals.
However, if the amount of information required to be transferred between processes is large or there is a requirement for data exchange between processes, other communication methods should be considered.
The unknown MPs queue is simple and convenient, but is limited to one-way communication. You can only share the MPs queues between the processes that create the MPs queue and their children:
Although a famous pipeline can be provided to any link process, it is not recommended for common users because it has been used in the system for a long time and is prone to errors due to improper use.
Message buffering can no longer be confined to parent-child processes, but allows any process to communicate with each other by sharing message queues. The system calls functions to synchronize message sending and receiving, so that users do not need to consider synchronization issues when using message buffering for communication, which is convenient to use. However, information replication requires additional CPU time, which is not suitable for situations where the amount of information is large or operations are frequent.
The shared memory directly exchanges information using the memory buffer to address the disadvantages of message buffer, without the need for replication. The advantage is quick and informative.
However, shared memory communication is achieved by attaching the shared memory buffer directly to the virtual address space of the process. Therefore, the operating system cannot synchronize read/write operations between these processes. Each process must use other synchronization tools. In addition, because memory entities exist in computer systems, they can only be shared by processes in the same computer system. Network Communication is not convenient.
Shared memory blocks provide an efficient two-way communication mechanism between any number of processes. Each user can read and write data, but all programs must comply with certain protocols to prevent the occurrence of competitive states such as overwriting memory space before reading information.
Unfortunately, Linux cannot strictly guarantee exclusive access to shared memory blocks, even when you create a new shared memory block using IPC_PRIVATE. At the same time, multiple processes that use shared memory blocks must use the same key value.