There are several methods for inter-process communication (IPC) in Linux. The following is a brief introduction:
1. Pipeline (PIPE)
Pipelines are the earliest IPC supported by Linux. pipelines can be divided into unknown pipelines and famous pipelines.
(1) The Nameless pipeline has the following characteristics:
1) pipelines are half-duplex and can only support one-way data flow. Two pipelines must be established when two processes need to communicate with each other;
2) an unknown pipeline is created using the pipe () function and can only be used between parent and child processes or brother processes;
3) pipelines are essentially an independent file for processes at both ends of the communication and only exist in the memory;
4) read/write operations on data: one process writes data to the pipeline, and the data written is added to the end of the pipeline buffer; the other process reads data in the buffer area of the pipeline.
(2) famous Pipelines
The famous pipeline is also half-duplex, but it allows communication between unrelated processes. Specifically, a famous Pipeline provides a path name to be associated with it and is stored in a file system in the form of FIFO (first-in-first-out. In this way, even unrelated processes can communicate with each other through FIFO, as long as they can access the provided path.
It is worth noting that writing data to the MPs queue is meaningful only when the MPs queue has a read end. Otherwise, the process that writes data to the pipeline receives the sigpipe signal from the kernel. The application can customize the signal processing function or directly ignore the signal.
Ii. semaphores (semophore)
A semaphore is a counter that can control the synchronous access to resources by multiple threads or processes between processes. It is often implemented as a lock mechanism. Essentially, semaphores are a protected variable and can only be accessed through initialization and two standard atomic operations (P/V. (P and V Operations are also called wait (s), signal (s ))
Iii. Signal)
Signal is one of the oldest methods used in UNIX systems for inter-process communication. The operating system sends a signal to notify a process of a scheduled event. processes that receive the signal can process the event in different ways, first, you can use the default processing mechanism-process interruption or exit. First, you can ignore the signal and customize the processing function of the signal to execute corresponding actions.
The kernel generates signals for processes to respond to different events. These events are the signal sources. The signal source can be: abnormal, other processes, terminal interruptions (CTRL-C, CTRL + \, etc.), job control (foreground, background process management, etc ), quota-based issues (CPU times out or files are too large), kernel notifications (such as I/O readiness), and alarms (timers ).
4. Message Queue)
A message queue is a linked list of messages. It allows one or more processes to write messages to it and one or more processes to read messages to it. In Linux, A Message Queue vector table msgque is maintained to represent all message queues in the system.
Message Queues overcome the disadvantages of low signal transmission information, and pipelines can only support unformatted byte streams and buffer limitations.
5. Shared Memory)
The shared memory is mapped to a memory segment that can be accessed by other processes. The shared memory is created by a process and can be mounted to the shared memory by other processes. Shared memory is the fastest IPC Mechanism, but because Linux itself cannot implement synchronous control over it, user programs need to perform concurrent access control, therefore, it generally integrates other communication mechanisms to implement inter-process communication, such as semaphores.
6. Socket)
Socket is also a communication mechanism between processes, but its main difference with other communication methods is that it can implement process communication between different hosts. A set of interfaces can be seen as the endpoints of inter-process communication. Each set of interfaces has a unique name. Other processes can access, connect, and communicate with each other.