Several ways of interprocess communication (IPC)

Inter-process communication (IPC)

  • 1. Common communication methods
  • 2. Low-level IPC methods
    • document
  • 3. The IPC mechanism commonly used in this machine
    • 3.1 Anonymous pipe
    • 3.2 Named pipe FIFO
    • 3.3 Message Queue MessageQueue
    • 3.4 shared memory SharedMemory
    • 3.5 Semaphore Semaphore
    • 3.6 Signal Signal
    • 3.7 unix domain socket
  • 4. IPC mechanism on different computers
  • 5. The number of data copies of the IPC mechanism

1. Common communication methods

  1. Files: Files can be transferred between processes via fork, exec, and the file system.
  2. Pipeline pipe: Pipeline is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with kinship. Process affinity usually refers to the parent-child process relationship.
  3. Named pipe FIFO: The named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
  4. Message Queue MessageQueue: A message queue is a linked list of messages stored in the kernel and identified by a message queue identifier. The message queue overcomes the disadvantages of less signal transmission information, the pipeline can only carry unformatted byte streams, and the buffer size is limited.
  5. Shared storage SharedMemory: Shared memory is to map a section 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 method of IPC, and it is specifically designed to run inefficiently when other methods of interprocess communication run. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
  6. Semaphore Semaphore: A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing shared resources while a process is accessing the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.
  7. Signal Signal: A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.
  8. Unix domain socket: Unix domain socket is used for communication between processes running on the same computer, which can transfer open file descriptors between two processes.
  9. Socket: Socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for process communication between different machines.

2. Low-level IPC method

Documents

Inter-process communication can be realized by transmitting open files (fork, exec and file system). Different processes transmit information through one or more files. In fact, this method is used in many application systems. But in general, inter-process communication does not include this seemingly low-level communication method.

3. The IPC mechanism commonly used in this machine

3.1 Unnamed pipe

Features:
Anonymous pipes are half-duplex (that is, data can only flow in one direction) and have fixed read and write ports.

Because there is no name, unnamed pipes can only be used for communication between processes that have a relationship (also between parent-child processes or sibling processes).

The unnamed pipe only exists in memory, and its essence is a kernel buffer. Processes access data from the buffer in a first-in-first-out manner: the process at one end of the pipeline sequentially writes process data into the buffer, and the process at the other end reads data sequentially.

Unnamed pipes are created by the pipe() function:

#include <unistd.h>
int pipe(int filedis[2]);

The parameter filedis returns two file descriptors: filedes[0] is opened for reading, and filedes[1] is opened for writing. The output of filedes[1] is the input of filedes[0].

3.2 named pipe FIFO

The main difference between a named pipe and an unnamed pipe is that a named pipe has a file name, which corresponds to a disk index node. With this file name, any process with corresponding permissions can access it strong>.

In Linux, a named pipe is created through the system call mknod() or makefifo().

mkfifo myfifo
mknod myfifo p

3.3 Message Queue MessageQueue

Compared with pipeline communication, the message queue has the advantage that it does not need to follow the order of the queue when receiving, but can receive specific types of messages according to custom conditions.

3.4 Shared Memory SharedMemory

The shared memory mechanism allows two or more processes to share a given storage area, which can be mapped to its own address space by two or more processes, and one process writes the shared memory information, It can be read by other processes using this shared memory through a simple memory read error, thus realizing inter-process communication.

One of the main benefits of using shared memory for communication is high efficiency, because the process can directly read and write memory without any copy of data. For communication methods such as pipelines and message queues, it needs to be performed four times in the kernel and user space The data copy of the shared memory is only copied twice: once from the input file to the shared memory area, and once from the shared memory to the output file.

3.5 Semaphore

The semaphore (semaphore) is different from the IPC structure already introduced, it is a counter. The semaphore is used to achieve mutual exclusion and synchronization between processes, not to store inter-process communication data.

Features

  1. The semaphore is used for inter-process synchronization. To transfer data between processes, it needs to be combined with shared memory.
  2. The semaphore is based on the PV operation of the operating system, and the program’s operations on the semaphore are all atomic operations.
  3. Each PV operation on the semaphore is not limited to subtracting or adding 1 to the semaphore value, but can also add or subtract any positive integer.

P operation: decrement the value of the semaphore by 1. If the value of the semaphore is less than 0 after decrementing 1, the process is blocked and placed in the waiting queue for the semaphore.
V operation: add 1 to the value of the semaphore. If the result of adding 1 to the value of the semaphore is less than or equal to 0, release a waiting process from the waiting queue for the signal.

3.6 Signal

The kernel process uses signals to notify the user process of which system events have occurred, and the user process responds to the signal as follows:

  1. Ignore the signal: Do not do any processing on the signal, but there are two signals that cannot be ignored: SIGKILL and SIGSTOP.
  2. Capture signal: define the signal processing function, when the signal occurs, execute the corresponding processing function. But there are two signals that cannot be captured: SIGKILL and SIGSTOP.
  3. Perform default actions: Linux specifies default actions for each signal.

3.7 unix domain socket

Unix domain sockets are used for communication between processes running on the same pc, it only copies data and does not perform protocol processing (such as adding and deleting network headers, calculating calibration checksum, generate a sequence number, and send a confirmation message), So Unix domain sockets are often faster than TCP sockets where the two ends of the communication are located on the same host.

4. IPC mechanism on different computers

Socket Socket
Socket programming is one of the main ways to realize inter-process communication in different computers. The well-known WWW service, FTP service, TELNET service, etc. are all implemented based on socket programming. In addition to between computer processes in different places, sockets are also applicable to inter-process communication within the same local computer.

5. Data copy times of IPC mechanism

Shared memory is the most efficient way of inter-process communication, because the address space of the same physical memory is mapped to the address space of different processes, then the communication between different processes can be done by directly modifying the memory in the address space. The implementation of the mechanism only needs two copies, that is, data from user space to memory, and data from memory to user space.

Other inter-process communication mechanisms require four copy operations. Assuming that communication between A and B is needed now, first copy the data from the buffer of process A to the kernel, then the kernel copies the data to the memory, then the data is copied from the memory to the kernel, and finally the kernel copies the data to the process B’s buffer. Therefore, it is more efficient to use shared memory communication.