Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The way and comparison of Inter-process Communication

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Inter-process communication is the transmission or exchange of information between different processes, so what media can be accessed by both parties between different processes? The user space of the process is independent of each other and generally cannot access each other, with the only exception being the shared memory area. However, the system space is a "public place", so the kernel can obviously provide such a condition. In addition, that is the peripheral that can be accessed by both sides. In this sense, of course, the two processes can also exchange information through ordinary files on disk, or through certain table entries and records in the registry or other databases. In a broad sense, this is also a means of inter-process communication, but it is generally not regarded as "inter-process communication". Because the efficiency of those means of communication is too low, and people's requirement for inter-process communication is to have a certain real-time performance.

1. Brief introduction of several main means of inter-process communication under Linux:

1. Pipes (Pipe) and named pipes (named pipe): pipes can be used for communication between related processes. Named pipes overcome the limitation that pipes do not have names, so in addition to the functions of pipes, they also allow communication between unrelated processes.

There are three kinds of pipes: 1) ordinary pipeline PIPE, which usually has a limitation: one is half-duplex and can only be transmitted in one way, and the other is that it can only be used between parent and child processes. 2) streaming pipeline s_pipe: the first restriction is removed and can be transmitted in both directions. 3) named pipes: name_pipe, which removes the second restriction and allows communication between many unrelated processes.

two。 Signal (Signal): a signal is a complex mode of communication that notifies the receiving process that an event has occurred. In addition to being used for inter-process communication, the process can also send signals to the process itself. Linux not only supports the early signal semantic function sigal of Unix, but also supports the signal function sigaction which semantically conforms to the Posix.1 standard (in fact, this function is based on BSD. In order to realize the reliable signal mechanism and unify the external interface, BSD uses the sigaction function to re-implement the signal function).

3. Message queues (message queues): message queues are linked lists of messages, including Posix message queues systemV message queues. Processes with sufficient permissions can add messages to the queue, and processes that are given read permission can read away messages from the queue. Message queue overcomes the shortcomings that the signal carries less information, the pipeline can only carry unformatted byte flow and the buffer size is limited.

4. Shared memory: allows multiple processes to access the same memory space, which is the fastest available IPC form. It is designed for the low efficiency of other communication mechanisms. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and mutual exclusion between processes.

5. Semaphores (semaphore): mainly used as a means of synchronization between processes and between different threads of the same process.

6. Socket (Socket): a more general mechanism for inter-process communication that can be used for inter-process communication between different machines. Originally developed by the BSD branch of the Unix system, it is now generally portable to other types of Unix systems: both Linux and System V variants support sockets.

II. Related problems

FAQ1: how do pipes relate to file descriptors and file pointers?

Answer: in fact, the use of pipes is similar to files, you can use ordinary IO functions such as read,write,open. Pipe descriptors are similar to file descriptors. In fact, the descriptors, file pointers and file descriptors used by pipes will eventually be converted into SOCKET descriptors in the system. All are limited by SOCKET descriptors in the system kernel. In essence, the pipeline in the LINUX kernel source code is realized through empty files.

FAQ2: how to use the pipeline?

Answer: there are mainly the following methods: 1) pipe, create a pipe and return 2 pipe descriptors. It is usually used for communication between parent and child processes. 2) popen, pclose: this method returns only one pipe descriptor and is often used for communication. The other side is stdin or stdout; 3) mkpipe: named pipe, which interacts with many processes.

FAQ3: comparison of advantages and disadvantages between pipeline and system IPC?

Answer: pipeline: the advantage is that all UNIX implementations are supported, and after the last process accessing the pipe is terminated, the pipe is completely deleted; the disadvantage is that the pipe only allows one-way transmission or is used between parent and child processes.

System IPC: the advantage is that it is powerful and can communicate between unrelated processes; the disadvantage is that the keyword KEY_T uses kernel identification, occupies kernel resources, and can only be explicitly deleted, and can not use some mechanisms of SOCKET, such as select,epoll.

FAQ4: what is the relationship between WINDOS interprocess communication and LINUX interprocess communication?

A: in fact, most of the process communication of WINDOS is transplanted to UNIX, the clipboard of WINDOS and file mapping can be found in the shared storage of UNIX process communication.

FAQ5: the relationship between inter-process communication and inter-thread communication?

A: because the entities that WINDOWS runs are threads, in a narrow sense, inter-process communication actually refers to the communication between threads belonging to different processes. The problem of thread synchronization between single processes can be merged into a special process communication. It uses kernel-supported system calls to keep threads synchronized. Some commonly used thread synchronization methods include: Event, Mutex, semaphore Semaphore, critical section resources and so on.

III. Efficiency comparison of various modes of inter-process communication

Types

No connection

Reliable

Flow control

Record

Message type priority

Ordinary PIPE

N

Y

Y

N

Stream PIPE

N

Y

Y

N

Name PIPE (FIFO)

N

Y

Y

N

Message queue

N

Y

Y

Y

Semaphore

N

Y

Y

Y

Shared storage

N

Y

Y

Y

UNIX stream SOCKET

N

Y

Y

N

UNIX packet SOCKET

Y

Y

N

N

Note: connectionless: the ability to send messages without calling some form of OPEN

Flow control: if the system is short of resources or cannot receive more messages, the sending process can control the flow

Reference from: http://blog.chinaunix.net/uid-21411227-id-1826900.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report