What are the three communication methods between Linux processes?

青灯夜游
Release: 2021-07-23 16:46:40
Original
7144 people have browsed it

Three ways to communicate between Linux processes: 1. Pipe communication. The process that sends information is called a writing process, and the process that receives information is called a reading process. 2. Message buffer communication uses the message buffer as the intermediate medium, and the sending and receiving operations of both communicating parties are based on messages. 3. Shared memory communication.

What are the three communication methods between Linux processes?

The operating environment of this tutorial: Ubuntu 16.04 system, Dell G3 computer.

3 ways of inter-process communication in Linux

Because different processes run in different memory spaces. Modifications of variables by one party are invisible to the other party. therefore. Information transfer between processes cannot be carried out directly through variables or other data structures, but can only be accomplished through inter-process communication.

According to the difference in the amount of information during process communication, process communication can be divided into two major types: communication of control information and communication of large amounts of data information. The former is called low-level communication, and the latter is called high-level communication.

Low-level communication is mainly used for the transmission of control information such as synchronization, mutual exclusion, termination, suspension, etc. between processes.

Advanced communication is mainly used for the exchange and sharing of data blocks between processes. Common advanced communications include pipelines (PIPE), message queues (MESSAGE), shared memory (SHARED MEM0RY), etc.

Here we mainly compare the characteristics of these three methods of advanced communication.

Pipe Communication (PIPE)

When two processes communicate using pipes. The process that sends information is called the writing process. The process that receives information is called a reading process. The intermediate medium of pipeline communication is files. This kind of file is usually called a pipeline file. It connects a writing process and a reading process together like a pipe to achieve communication between the two processes. The writing process writes information into the pipe file through the writing end (sending end); the reading process reads information from the pipe file through the reading end (receiving end). The two processes coordinate and continuously write and read, forming a pipeline in which both parties transfer information through pipes.

Use the system call PIPE() to create an unnamed pipe file, usually called an unnamed pipe or PIPE; use the system call MKNOD() to create a named pipe file. Often called a named pipe or FIFO. An anonymous pipe is a non-permanent pipe communication mechanism. It will be undone when all the processes it accessed terminate. Unnamed pipes can only be used between processes with family ties. Well-known pipes can exist in the system for a long time. It is also provided for use by processes of any relationship, but improper use can easily lead to errors. Therefore, the operating system hands over the management rights of the named pipe to the system for control. After the pipe file is created, the read and write operations on the pipe can be realized by system calls WRITE() and READ(); after the communication is completed, CLOSE() can be used to The pipeline file is closed.

Message buffer communication (MESSAGE)

Multiple independent processes can communicate with each other through the message buffer mechanism. This kind of communication is realized with the message buffer as the intermediate medium. The sending and receiving operations of both communicating parties are based on messages. In memory, message buffers are organized into queues, often called message queues. Once created, the message queue can be shared by multiple processes. The process that sends a message can send any number of messages to the specified message queue at any time and check whether there is a receiving process waiting for the message it sends. If there is, wake it up: and the process receiving the message can get the message from the designated message queue when it needs the message. If the news hasn't come yet. Then it goes to sleep state and waits.

Shared Memory Communication (SHARED MEMORY)

In view of the shortcomings of message buffering that requires CPU to copy messages. OS provides a communication method for direct data exchange between processes - shared memory. As the name implies. This communication method allows multiple processes to communicate using the same memory segment (as an intermediate medium) with the support of external communication protocols or synchronization and mutual exclusion mechanisms. It is one of the most effective data communication methods, and its characteristic is that there are no intermediate links. Directly attach shared memory pages through. Map into the respective virtual address spaces of the processes communicating with each other. This allows multiple processes to directly access the same physical memory page. It's like accessing your own private space (but it's not actually private but shared). Therefore, this inter-process communication method is the fastest way to achieve communication between processes in the same computer system. And therein lies its limitation. That is, processes that share memory must coexist in the same computer system. Only physical memory can be shared.

Characteristics (advantages and disadvantages) of the three methods:1. Unnamed pipes are simple and convenient. But it is limited to the working mode of one-way communication. And the sharing of pipes can only be achieved between the process that created it and its descendant processes: although named pipes can be provided to processes of any relationship. However, because it exists in the system for a long time, it is prone to errors if used improperly.

2. Message buffering can no longer be limited to parent-child processes. It allows any process to achieve inter-process communication through a shared message queue. And the system calls functions to achieve synchronization between message sending and receiving. As a result, users no longer need to consider synchronization issues when using message buffers for communication. It is easy to use, but copying information requires additional CPU time. Not suitable for situations with large amounts of information or frequent operations.

3. Shared memory uses the memory buffer to directly exchange information based on the shortcomings of the message buffer. No copying is required. Its advantages are speed and large amount of information. However, shared memory communication is achieved by attaching the shared memory buffer directly to the virtual address space of the process. Therefore, the synchronization problem of read and write operations between these processes cannot be realized by the operating system. This must be resolved by each process using other synchronization tools. In addition, since the memory entity exists in the computer system. Therefore, it can only be shared by processes in the same computer system. Inconvenient for network communication.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What are the three communication methods between Linux processes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!