


What are the three communication methods between Linux processes?
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
