Preface:

In the previous review, I didn’t read much about this section and thought it was not the key content. But now I find that this section is actually quite important, involving some things at the bottom, which is also very related to Linux (I know too little about Linux, so I have to make up for it when I have free time).

Communication mode:

Since the user space of each process is independent and cannot be accessed by each other, the kernel space is used for inter-process communication for the simple reason that each process shares a kernel space.

The Linux kernel provides a number of ways to communicate between processes, the simplest of which is pipes, divided into “anonymous pipes” and “named pipes.”

1. Anonymous pipes

Logo and name as the name suggests, it has no anonymous pipe is a special file exists only in memory, did not exist in the file system, the vertical bar “|” is anonymous pipe shell command, communication data flow in a plain and limited size, is one-way communication way, the data can only flow in one direction, if you want to two-way communication, you need to create two pipes, Anonymous pipes can only be used for communication between processes that have parent-child relationships. The life cycle of anonymous pipes is established with the creation of the process and disappears with the termination of the process.

2. Name pipes

This breaks the restriction that anonymous pipes can only communicate between related processes, because the premise of using named pipes is to create a device file of type P on the file system, through which unrelated processes can communicate. In addition, whether anonymous or named pipes, the data written by a process is cached in the kernel, and the data read by another process is naturally obtained from the kernel. At the same time, the communication data follows the first-in-first-out principle, and file location operations such as LSEEK are not supported.

3. Message queues

Overcome the communication pipeline data is plain byte stream, the message queue is actually stored in the kernel “message list”, the body of the message queue can be user-defined data types, send data, will be divided into a an independent body, when receiving data, of course, also want to and the sender sends the message body is consistent with the type of data, This ensures that the data read is correct. The speed of message queue communication is not the most timely, after all, every data write and read needs to go through the process of copying between user and kernel.

4. Shared memory

Can be solved in the message queue communication between user mode and kernel mode data copy process of overhead, it directly assigned a Shared space, each process can have direct access to, like your own space access process convenient, don’t need in kernel mode or system calls, greatly improving the speed of communication, enjoy the fastest in the name of interprocess communication way. However, the convenient and efficient shared memory communication brings new problems. Multiple processes competing for the same shared resource will cause data confusion.

5.A semaphore

Semaphores, then, are needed to secure the shared resource to ensure that only one process can access it at any one time, which is mutually exclusive access. The semaphore can not only achieve mutual exclusion of access, but also achieve synchronization between processes. The semaphore is actually a counter, which represents the number of resources, and its value can be controlled by two atomic operations, namely P operation and V operation.

6.signal

A semaphore with a similar name is a signal, which has a similar name but not the same function at all. Signal is inter-process communication mechanism in the asynchronous communication mechanism, the signal can be direct interaction between the application process and the kernel, the kernel can also use signal to notify the user space, what had happened to the process of system events, the source of the signal events mainly include hardware source source (such as keyboard Cltr + C) and software (e.g., kill command), Once a signal occurs, a process can respond to it in three ways. Perform default operations. 2. Capture signals. 3. SIGKILL and SEGSTOP are two signals that the application process cannot detect and ignore, so that we can terminate or stop a process at any time.

7.The Socket communication

The communication mechanism mentioned above, all work in the same host, if you want to communicate with different host processes, then you need Socket communication. Sockets are not only used for communication between different host processes, but also for communication between local host processes. According to different Socket types, sockets can be divided into three common communication modes: TCP, UDP, and local process communication.

This is the main mechanism for interprocess communication. You might ask, what about the way threads communicate with each other?

Threads under the same process are sharing process resources, as long as the shared variables can achieve inter-thread communication, such as global variables, so the focus is not on the way of communication between threads, but on the problem of multi-thread competition for shared resources, semaphore can also achieve mutual exclusion and synchronization between threads:

  • Mutually exclusive to ensure that only one thread can access a shared resource at any time.

  • Synchronization ensures that thread A is executed before thread B;

Reference: Xiaolin Coding – Interprocess communication (link cannot be put, an article on wechat public account, written very well)