the source of Linux process communication
The process communication method under Linux is basically inherited from the process communication means on UNIX platform. The two main at&t that make a significant contribution to the development of UNIX (formerly American Telephone & Telegraph abbreviations, Bell Labs and BSD (UC Berkeley's Berkeley Software Release Center), which is also the origin of the American telephone and Telegraph company, have a different focus on interprocess communication. The former has systematically improved and expanded the early interprocess communication means of UNIX, formed "system V IPC", the communication process is confined to a single computer, the latter has skipped the limit, and formed the interprocess communication mechanism based on socket (socket). . Linux inherits both.
initially, the UNIX IPC includes : piping, FIFO, signal.
System V IPC includes : System V Message Queuing, System V Semaphore, System V shared memory area.
POSIX IPC includes POSIX Message Queuing, POSIX semaphore, POSIX shared memory area.
There are two points to be brief: 1 due to the diversity of UNIX versions, the Institute of Electrical and Electronic Engineering (IEEE) has developed an independent UNIX standard, the new ANSI Unix standard known as the Portable Operating system interface for the computer environment (portable operating System Interface,psoix). Most of the existing UNIX and popular versions follow the POSIX standard , while Linux follows the POSIX standard from the outset; 2 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, many UNIX versions of stand-alone IPC are left with BSD traces, such as 4.4BSD-supported anonymous memory mappings, 4.3+BSD implementations of reliable signal semantics, and so on.
The various IPC tools supported by Linux, in the next discussion in this article, to avoid conceptual confusion, the discussion of all issues ultimately boils down to interprocess communication in a Linux environment, with as few references to UNIX versions as possible. And, for different implementations of Linux-supported communications, such as the POSIX shared memory area and the System V shared memory area of two implementations for shared memory, the POSIX API will be introduced primarily.
Several main means of interprocess communication under Linux
1 Pipeline (Pipe) and famous pipe (named Pipe,fifo)
Anonymous piping is one of the first forms of UNIX IPC supported by Linux, with the following characteristics: The pipe is Half-duplex, the data flows only in one direction, and two pipes are required to communicate; the streams pipe is a two-way (Full-duplex) pipe, and a single streams pipe can be used to the parent, Child processes provide bidirectional data flow. Solaris supports streams pipelines, and Linux's optional add-on packs also provide streams pipelines. Can only be used between a parent-child process or a sibling process (a relational process); A separate file system: The pipe is a file for the process at both ends of the pipe, but it is not a normal file, it does not belong to a file system, it is a separate file system, And only exists in the memory. Read and write data: a process that writes to the pipe is read by the process on the other side of the pipe. The written content is added to the end of the pipe buffer each time, and data is read from the head of the buffer each time.
Different points of the two:
(1) Anonymous pipe it has no name and can only be used for communication between relational processes. A named pipe FIFO overcomes the restriction that a pipe has no name, so it allows communication between unrelated processes in addition to the functionality that the pipe has.
(2) An anonymous pipe is a file for processes at both ends of the pipeline, but not a type of Linux file, is not part of a file system, and exists only in memory; the pipe () function opens two file descriptors, respectively, for reading and writing. A named pipe provides a path name associated with it, a type of Linux file that exists in the file system as a FIFO file form.
Same point:
(1) Pipeline and FIFO data is a byte stream, the application must be determined in advance specific transport "Protocol", using the dissemination of messages of specific significance.
(2) one-way (Half-duplex) data flow.
(3) The system's two restrictions on piping and FIFO (maximum number of descriptors opened at any time in a process), pipi_buf (the maximum amount of data that can be written atomically to a pipe or FIFO, POSIX requires at least 512). Open_max
4 is an IPC that continues with the process(the IPC object persists until the last process that opened the object closes the object.) )
Pipelines are used in two areas:
(1) In the shell is often used in the pipeline (as input input redirection), in this application mode, the creation of the pipeline for the user is transparent;
(2) for the relationship between interprocess communication, the user creates the pipeline, and completes the read and write operation.
2) UNIX Domain Protocol
Reference article: UNIX Domain protocol
The UNIX domain protocol is not an actual protocol family, but rather a method of executing client/server communications on a single host , using APIs (socket APIs) for executing client/server communications on different hosts.
UNIX domain sockets only replicate data, do not perform protocol processing, do not need to add or remove network headers, do not have to compute checksums, do not produce sequential numbers, do not need to send a confirmation message. UNIX domain sockets provide streaming and datagram two kinds of interfaces. UNIX Domain Datagram services are reliable and do not lose messages or deliver errors. It is a mixture between the socket and the pipe .
reasons to use UNIX domain sockets :
(1) UNIX domain sockets are often one-fold faster than TCP sockets on the same host at both ends of the communication (TCPV3). UNIX domain sockets only replicate data, do not perform protocol processing, do not need to add or remove network headers, do not have to compute checksums, do not produce sequential numbers, do not need to send a confirmation message.
(2) can be used to pass a descriptor between different processes on the same host.
(3) The newer implementations of UNIX domain sockets provide the customer's credentials (user ID and group ID) to the server, providing additional security measures.
To create a pair of unnamed, interconnected UNIX domain sockets, users can use their network-oriented domain sockets interface or use the Socketpair function.
3) signal (Signal)
A signal is a more complex form of communication used to inform the receiving process that an event occurs, in addition to communication between processes, the process can send signals to the process itself; Linux in addition to supporting the UNIX early signal semantic function signal, It also supports the signal function sigaction of semantics conforming to POSIX.1 standard (in fact, the function is based on BSD, BSD in order to achieve reliable signal mechanism, but also can unify the external interface, with the Sigaction function to realize the signal function). The sigaction contains information about the signal generation.
4) Message Queuing
Message Queuing is a linked table of messages, including POSIX message queues and System V message queues. A process with sufficient permissions can add messages to the queue, and processes that are given Read permission can read messages in the queue. Message Queuing overcomes the lack of information load, the pipeline can only host unformatted byte streams and buffer size limitations. and Message Queuing is persisted with the kernel (the IPC object persists until the kernel restarts or the object is deleted).
5) Shared memory
Reference article: Process communication mode: Shared memory Area
Mmap:linux Environment interprocess Communication (V): Shared memory (top)
System v Shared Memory: Linux environment interprocess communication (V): Shared memory (bottom)
Enables multiple processes to access the same memory space, which is the fastest available form of IPC . is designed for the low efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as the signal volume, to achieve synchronization and mutual exclusion between processes .
6) Signal Quantity (semaphore)
Primarily as a means of synchronization between processes and between different threads of the same process.
various synchronization methods :
Several ways to thread synchronization: Reference article: Process synchronization and Thread synchronization
7) Sleeve Interface (socket)
A more general interprocess communication mechanism that can be used for interprocess 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: Linux and System V variants support sockets.
reference materials:
anonymous pipes and Named pipes:Linux environment interprocess communication (i)
Signal:Linux Environment interprocess communication (II): Signal (upper) Linux Environment interprocess communication (II): Signal (next)
Message Queuing:Linux Environment interprocess communication (iii)
Semaphore:Linux Environment interprocess communication (iv)
Shared Memory:Linux Environment interprocess communication (V): Shared memory (upper) Linux Environment interprocess communication (V): Shared memory (next)
Sockets:Linux Environment interprocess communication (vi)