[Inter-process communication method]

Interprocess communication (IPC, Interprocess communication) is a set of programming interfaces that allow programmers to coordinate different processes so that they can run simultaneously in an operating system, and communicate and exchange information with each other. This enables a program to handle the requests of many users at the same time. Because even if only one user makes a request, it may lead to the running of multiple processes in an operating system, and the processes must talk to each other. The IPC interface provides this possibility. Each IPC method has its own advantages and limitations, and in general, it is not common for a single program to use all IPC methods.

  

IPC methods usually include pipes (including unnamed pipes and named pipes), message queues, semaphores, shared storage, Sockets, Streams, etc. Among them, Socket and Streams support IPC of two processes on different hosts.



 

communication between processes

# Pipe: Pipe is a half-duplex communication method, data can only flow in one direction, and can only be used between processes with affinity. The affinity of a process usually refers to a parent-child process relationship.

# Named pipe : Named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.

# 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 a shared resource while a process is accessing the resource. Therefore, it is mainly used as a synchronization method between processes and between different threads in the same process.

# Message queue (message queue) : The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. Message Queuing overcomes the shortcomings of less information in signaling, pipes can only carry unformatted byte streams, and limited buffer size.

# Signal ( sinal ): Signal is a relatively complex communication method used to notify the receiving process that an event has occurred.

# Shared memory ( 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 way of IPC, and it's designed for the inefficiencies of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.

# Socket (socket): The socket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for different and inter-process communication.

 

There are three types of pipes:

1) Ordinary pipeline PIPE usually has two restrictions, one is simplex, which can only be transmitted in one direction; the other is that it can only be used between parent-child or sibling processes.

2) Stream pipeline s_pipe: The first limitation is removed, it is half-duplex and can transmit in both directions.

3) Named pipes: name_pipe, removes the second limitation and can communicate between many unrelated processes.

 

 

Purpose of IPC

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

2) 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.

3) 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).

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

5) 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 intercept all traps and exceptions of another process, and be able to know its state changes in time.

Processes coordinate their behavior by communicating with the kernel and with other processes. Linux supports a variety of inter-process communication (IPC) mechanisms, two of which are signals and pipes. In addition to this, Linux also supports the IPC mechanism of System V (named after the Unix version that first appeared).

Guess you like

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