6 ways to communicate between Linux processes

process concept

A process is the concept of an operating system. Whenever we execute a program, a process is created for the operating system. In this process, resources are allocated and released. A process can be thought of as a single execution of a program.

The concept of process communication

Process user space is independent of each other and generally cannot access each other. But in many cases, processes need to communicate with each other to complete a certain function of the system. Processes coordinate their behavior by communicating with the kernel and with other processes.

Application scenarios of process communication

Data transfer: One process needs to send its data to another process, the amount of data sent is between one byte and several megabytes.

Shared data: Multiple processes want to operate shared data, and the modification of shared data by one process should be seen by other processes immediately.

Notification events: A process needs to send a message to another or a group of processes to notify it (them) that some event has occurred (such as to notify the parent process when a process terminates).

Resource sharing: The same resources are shared among multiple processes. To do this, the kernel needs to provide locking and synchronization mechanisms.

Process control: Some processes want to completely control the execution of another process (such as the Debug process). At this time, the controlling process hopes to be able to intercept all traps and exceptions of another process, and be able to know its state changes in time.

How processes communicate

6 Ways of Communication Between Linux Processes 6 Ways of Communication Between Linux Processes

1. Pipes

Pipes are divided into named pipes and unnamed pipes

Nameless pipe is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with kinship. The kinship of processes generally refers to the parent-child relationship. Dumb pipes are generally used for communication between two different processes. When a process creates a pipe and calls fork to create its own child process, the parent process closes the read pipe end, and the child process closes the write pipe end, which provides a way of data flow between the two processes.

A named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.

2. Semaphore

A semaphore is a counter that can be used to control the access of multiple threads to shared resources. It is not used for exchanging large amounts of data, but for synchronization between multiple threads. It is often used as a locking mechanism to prevent a process from When accessing a resource, other processes also access the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.

Linux provides a set of well-designed semaphore interfaces to operate on signals. They are not just for binary semaphores. These functions will be introduced below, but please note that these functions are used to group semaphores. value to operate on. They are declared in the header file sys/sem.h.

semget function

Its role is to create a new semaphore or acquire an existing semaphore

semop function

Its role is to change the value of the semaphore

semctl function

This function is used to directly control the semaphore information

3. Signal

A signal is a relatively complex communication method used to notify the receiving process that an event has occurred.

4. Message queue

The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. The message queue overcomes the characteristics of less signal transmission information, the pipeline can only carry unformatted byte streams, and the buffer size is limited. The message queue is UNIX A mechanism for sharing resources between different processes under UNIX. UNIX allows different processes to send formatted data streams to any process in the form of message queues. Processes with operation rights to the message queue can use msget to complete the message queue. Operation control. By using message types, processes can read messages in any order, or prioritize messages.

5. Shared memory

Shared memory is to map a piece 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 IPC (inter-process communication) method, which is aimed at other processes. It is specially designed for the low efficiency of inter-process communication. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

6. Sockets

Socket, that is, socket is a communication mechanism, with this mechanism, the development of the client/server (that is, the process to be communicated) system can be carried out on a local single machine or across a network. That is, it allows processes on computers that are not on the same computer but connected via a network to communicate. Also because of this, sockets clearly distinguish between client and server.

The characteristics of a socket are determined by three properties: domain, type, and protocol.

Can be used for different and inter-process communication

The original text comes from: https://www.linuxprobe.com/linux-process-method.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325857208&siteId=291194637