[LINUX] Types of interprocess communication

Introduction of Communication agenda

WHOAMI IPFactory 1 year @ n01e0 I usually make Linux kernel modules.

WHAT IS IPC IPC is an abbreviation for * Inter Process Communication *. It means ** data exchange between processes **, and various types of IPC are available in Linux. In this article, I will only give a brief introduction to each, and will not explain the implementation in detail. This is because the goal is to understand the general characteristics of each function and to make the best choice for the reader.

IPC classification

IPC is roughly classified into three types according to its function.

--Communication --Mechanism for data communication and exchange between processes --Synchronization --Mechanism for synchronization between processes and threads --Signal --The signal is special and can be used for synchronization and communication depending on the conditions.

IPC specifications

IPC has been implemented and mixed according to several standards in UNIX's long history. There are mainly * System V * implementations and * POSIX * implementations. Overall, POSIX is easier to handle, but POSIX's IPC is implemented from kernel version 2.6, so it's less portable.

SYSTEM V IPC and POSIX IPC

In System V, the data structure of each IPC is in kernel space, Dynamically generated when a process requests ** IPC resources (semaphores, message queues, shared memory regions) **. Each IPC resource has a unique identifier. The user can distinguish each IPC with a different IPC key. This relationship is similar to the file-file descriptor relationship. However, the difference between files is that they remain in memory unless the process explicitly releases them. There is a ** POSIX IPC ** that provides similar functionality, which is designed to be multi-thread safe. Another major difference is that POSIX allows each IPC to be named and distinguished. There is a rule for naming, it must be a null-terminated string that starts with / and is at least one character long except /. Although there are small differences in the specifications, the major difference in the concept of each IPC is the management method of each object (resource).

IPC comparison

The major difference between System V and POSIX is the IPC object identifiers and handlers, which are listed.

IPC type identifier handler
pipe None File descriptor
FIFO path File descriptor
System V semaphore System V IPC key System V IPC ID
POSIX Anonymous Semaphore None Semaphore pointer
POSIX named semaphore POSIX IPC path Semaphore pointer
System V message System V IPC key System V IPC ID
POSIX message POSIX IPC path Message queue descriptor
System V shared memory System V IPC key System V IPC ID
POSIX shared memory POSIX IPC path File descriptor
UNIX domain socket path File descriptor
Internet domain socket IP address port File descriptor

IPC on Linux introduced in this article

PIPE

What is a pipe

It's a very familiar concept for readers who live in the shell on a daily basis. To briefly restate what a pipe (|) is in a shell ** Connect the output of one process to the input of another process ** It means that. Similarly, pipes as ** IPC ** are often used to connect data output and input. I tried to explain the data structure of pipes by taking something as an example, but I can't think of any good examples other than pipes. Since it is a cylinder, input / output is first-in first-out. In summary, a pipe is a ** unidirectional first-in, first-out data structure **.

Implementation

The pipe () system call defined in ʻunistd.h produces two file descriptors. One is for reading and the other is for writing. These are file descriptors, but there is no corresponding path. The data written to the pipe is buffered by the data structure in the kernel until it is read. Normally, when a process reads (read ()`) an empty pipe, it stops reading until data is written to that pipe.

FIFO There is a similar function, FIFO. Also called a named pipe, as the name implies, it is a ** first-in first-out ** data structure. The only difference from pipes is that the created file descriptor has a ** name ** (path).

SIGNAL This is also a familiar concept. It can be said that signals are also a type of IPC. There are many types of signals, so let's take a few examples.

and so on. Signals are primarily used to tell a process that something has happened, or to have a process perform a particular action. The amount of information that a signal has is small, and usually it has no information other than the number (type). Also, how to handle the received signal depends on the process.

Correspondence

There are several patterns for responding to signals

  1. Ignore
  2. [Perform standard setting operation](# standard setting operation)
  3. Call Signal Handler (#Signal Handler)

ignore

With some exceptions, the received signal can be ignored. What are some signals that cannot be ignored?

Therefore, the process that receives these signals always performs the standard setting operation.

Standard setting operation

As the name implies, the standard setting operation is the reaction to each signal that is set as standard in Linux. Some signals are set to ** end **, ** dump **, ** stop **, and ** ignore ** by default.

Signal handler

In addition to the standard setting operation, the user can define the correspondence for each signal using signal ().

Implementation

When a process issues a signal to the process, the signal is received by the kernel. The kernel checks to see if the target process is running and puts the signal on hold if it is not. The signal () system call has a pointer to the function that becomes the signal handler corresponding to the signal type as an argument, and associates them.

SEMAPHORE The semaphore is a ** synchronization ** mechanism in the IPC classification. The actual state of the semaphore is an integer value. Assigned as an access control counter to protect resources shared by multiple processes

-[Integer type variable](# integer type variable) --[List of processes waiting for operation](# List of processes waiting for operation) -[Two Atomic Methods](# Two Atomic Methods)

Consists of.

Constitution

Integer type variable

It becomes a counter. This value indicates the state of the resource (whether it is available or not)

List of processes waiting for operation

List of processes waiting for resources to become available

Two atomic methods

Count up and down the semaphore. When a process accesses a resource protected by a semaphore, it first counts down the semaphore associated with the target resource. If the semaphore count becomes negative, the process will be interrupted and added to the list. When access to the resource is complete, the process counts up the semaphore. This will restart the list process when the semaphore value becomes positive.

Implementation

The semaphore itself does not limit the operation of the process, it is the user who makes the semaphore meaningful. Moreover, since the semaphore is a synchronization mechanism, it requires a very complicated operation.

System V Create or open a semaphore with semget (). Operate the semaphore with semop ().

POSIX There are two types of semaphores in POSIX -[Named semaphore](#named semaphore) -[Memory-based semaphore (anonymous semaphore)](#Memory-based semaphore)

Named semaphore

Like other POSIX IPCs, semaphores can be named and managed. Share semaphores between processes by sharing names

Memory-based semaphore

Mapped to shared memory. Share the semaphore by sharing memory between processes and threads without a name.

MESSAGE IPC message queues are, as the name implies, queues. Messages sent by a process are placed in the message queue until they are read by another process. When another process reads the message, the kernel removes the message from the queue. Therefore, the exchange of messages between processes is always one-to-one.

Implementation

System V For the data sent as a message, the priority of the message must always be specified as an integer value. The receiver receives the message by specifying the priority. (In that case, it may not be first-in first-out, so can we call it * queue *?) Get a queue using msgget (), send it with messnd (), and receive it with msgrcv ().

POSIX POSIX message queues are also named and managed. When receiving, you cannot specify the priority of the message. The oldest message with the highest priority is read. Open the queue with mq_open (), send with mq_send, and receive with mq_receive.

SHARED MEMORY IPC shared memory As the name implies, place a specific data structure in a shared memory space. Also, while other IPCs transfer data from the user space to the kernel space data structure at runtime, the overhead is small because it only maps to the shared memory space. However, there is a problem to keep in mind that the kernel is not involved. ** There is a need to prevent multiple processes from accessing at the same time **, and the semaphore mentioned above can be used for that purpose.

Implementation

System V --Create a shared memory segment or get the ID of an existing segment with shmget () --** Attach a shared memory segment to your process's memory space with shmat () ** --Detach with shmdt ().

The attached shared memory segment can be treated like normal memory in a program. The shared memory object is destroyed when all processes are detached and deleted by shmctl ().

POSIX --Open the shared memory object with shm_open (). Also, this return value is a file descriptor. --Set the MAP_SHARED flag to the obtained file descriptor andmmap ()

SOCKET socket. Sockets can be classified into two types according to the data transfer method.

--Stream socket --Datagram socket

Is called. As the name suggests, stream sockets can be treated as streams, and like files, any number of bytes can be read regardless of the written size. Datagram sockets are similar to messages, etc., with specific delimiters in the data, and reading requires reading the entire data. In addition, as another classification of sockets, there is also a classification by communication destination.

--UNIX domain socket --Internet domain socket

Called UNIX domain sockets support communication between applications within the same host, and Internet domain sockets support communication between applications connected by the Internet Protocol.

Implementation

System calls for operating sockets

We can only introduce a small part of sockets here, but it is a concept that allows a great many things. This is the limit of the IPC summary article, but there are plenty of articles about sockets, so please refer to them if necessary.

Summary

It is convenient to identify the characteristics, strengths, and weaknesses of each and use the optimum IPC. In my case, I use the POSIX message queue to exchange file paths and so on.

I've written a lot of crap, but I'd like to put together a sample of each implementation as needed.

Recommended Posts

Types of interprocess communication
Linux interprocess communication
Interprocess communication ~ shared memory ~
Types of recommendation systems
Revenge of the Types: Revenge of types
Catch multiple types of exceptions
Interprocess communication for embedded DL
Summary of Linux distribution types
I measured various methods of interprocess communication in multiprocessing of python3
I summarized 11 types of operating systems