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

What are the ways of communication between linux processes

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "what are the ways of communication between linux processes". In daily operation, I believe that many people have doubts about the ways of communication between linux processes. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "which ways of communication between linux processes are there?" Next, please follow the editor to study!

There are three ways of linux inter-process communication: 1. Pipeline communication, the process that sends information is called the write process, and the process that receives the information is called the read process. 2. message buffering communication, with the message buffer as the intermediate medium, and the sending and receiving operations of both sides of the communication are based on the message. 3. Shared memory communication.

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

Three ways of linux Inter-process Communication

Because different processes run in different memory spaces, the modification of variables on one side is imperceptible to the other. Therefore, the information transmission between processes can not be carried out directly through variables or other data structures, but can only be completed through inter-process communication.

According to the amount of information in process communication, process communication can be divided into two types: the communication of control information and the communication of a large number of data information. the former is called low-level communication, and the latter is called advanced communication.

Low-level communication is mainly used for the transmission of control information such as synchronization, mutual exclusion, termination, suspension and so on.

Advanced communication is mainly used for the exchange and sharing of data blocks between processes. The common advanced communications are pipeline (PIPE), message queue (MESSAGE), shared memory (SHARED MEM0RY) and so on.

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

Pipeline Communication (PIPE)

When two processes communicate through a pipe, the process that sends the information is called the write process. The process that receives the information is called the read process. The intermediate medium of pipeline communication is the file. this kind of file is usually called pipe file. it connects a write process and a read process together like a pipe to realize the communication between the two processes. The write process writes information to the pipeline file through the write end (sender); the read process reads information from the pipeline file through the read end (receiver). If the two processes coordinate to write and read continuously, it will form an assembly line for both parties to transmit information through the pipeline.

Using the system call PIPE (), you can create an unnamed pipe file, usually called an unnamed pipe, or PIPE; can create a named pipe file using the system call MKNOD (). It is often called named pipe or FIFO. The nameless pipeline is a kind of non-permanent.

A permanent pipeline communication mechanism. When all the processes it accesses are terminated, it will also be revoked. Unnamed pipes can only be used between processes that have family ties. Named pipes can exist in the system for a long time and are provided to any relational process, but improper use can easily lead to errors. so the operating system hands over the management of named pipes to the system to control the creation of pipe files, you can read and write pipes through system calls WRITE () and READ (); after communication, you can use CLOSE () to close the pipe files.

Message buffered communication (MESSAGE)

Multiple independent processes can communicate with each other through a message buffering mechanism. this kind of communication is realized by using the message buffer as the intermediate medium. the sending and receiving operations of both sides of the communication are based on messages. In memory, message buffers are organized into queues, often referred to as message queues. Once the message queue is created, it can be shared by multiple processes. the sending process can send any message to the specified message queue at any time and check to see if there is a receiving process waiting for the message it sends. If so, wake it up: the process that receives the message can get the message from the specified message queue when it is needed. if the message hasn't arrived yet. Then go to sleep and wait.

Shared memory Communication (SHARED MEMORY)

In view of the disadvantage that message buffering needs to occupy CPU for message replication, OS provides a communication mode of direct data exchange between processes to share memory, as the name implies. This communication mode allows multiple processes to communicate with each other using the same memory segment (as an intermediate medium) under the support of external communication protocols or synchronization and mutual exclusion mechanism. it is the most effective way of data communication. Its characteristic is that there is no intermediate link. The shared memory pages are directly attached. Mapped to the respective virtual address spaces of the processes that communicate with each other, so that multiple processes can directly access the same physical memory page. just like visiting your own private space (but essentially not private but shared). Therefore, this kind of inter-process communication is the fastest way to realize communication between processes in the same computer system. And its limitation lies in this. That is, processes that share memory must coexist in the same computer system. Physical memory can only be shared.

Characteristics of the three approaches (advantages and disadvantages):

1. The nameless pipeline is 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 descendants: although the named pipeline can be provided to the process of any relationship. However, because it exists in the system for a long time, it is easy to make mistakes if it is not used properly.

two。 Message buffering can no longer be limited to parent-child processes. Any process is allowed to achieve inter-process communication by sharing message queues. Synchronization between sending and receiving messages is realized by system call functions, so that users no longer need to consider synchronization when using message buffering for communication. Easy to use However, the replication of information takes extra time to consume CPU. It is not suitable for situations with large amount of information or frequent operations.

3. In view of the shortcomings of message buffering, shared memory uses memory buffers to exchange information directly, without copying. Fast and large amount of information is its advantages. However, the communication mode of shared memory is realized by directly attaching the shared memory buffer to the virtual address space of the process. therefore, the synchronization of read and write operations between these processes cannot be realized by the operating system. It must be solved by each process using other synchronization tools. In addition, because the memory entity exists in the computer system. so it can only be shared by processes in the same computer system. It is not convenient for network communication.

At this point, the study of "what are the ways of communication between linux processes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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