3 advanced ways to communicate between Linux processes and their pros and cons

Source: Internet
Author: User
Tags message queue posix semaphore signal handler

Because different processes are running in their own different memory spaces. The other party is not aware of the modification of the variable. therefore The transfer of information between processes cannot be done directly through variables or other data structures, but only through interprocess communication.

Depending on the amount of information in the process communication, process communication can be divided into two types: control information communication and large-scale data information communication. The former is called low-level communication, which is called advanced communication.

Low-level communication is mainly used for synchronization between processes, mutual exclusion, termination, suspension and so on control information transmission.

Advanced communications are primarily used for interchange and sharing of inter-process data blocks common advanced communications are pipelines (pipes), message queues (messages), shared memory (Gkfx Mem0ry), and so on.

Here is the main comparison of the high-level communication of these three ways of characteristics.

Pipeline Communication (pipe)
Two processes use pipelines for communication. The process of sending information is called a write process. The process of receiving information is called a read process. The intermediary medium of the pipeline communication mode is the file. This file is usually called a pipe file. It's like a pipe. Connects a write process to a read process to achieve communication between two processes. The write process writes information to the pipeline file through the write-side (the sending side), and the read process reads the information from the pipe file through the read-out end (the receiving side). The two processes are continuously written and read, which will form a pipeline for both parties to pass information through pipelines.
Using the system call pipe () You can create a nameless pipe file, often called a nameless pipe or pipe, and use the system call Mknod () to create a well-known pipe file. This is often called a well-known pipe or FIFO. The nameless pipe is a non-permanent
Long-lasting pipeline communication mechanism. When the process it accesses is all terminated, it is also revoked. Nameless pipes can only be used between processes that have family ties. Well-known pipelines can exist in the system for a long time. It is also available for processes of any relationship, but improper use can lead to errors. Therefore, the operating system to the management of the named pipe to the system to control the pipeline file is created, you can use the system call write () and read () to achieve the read and write operation of the pipeline, after the communication, close () to the pipeline file closed.

Message buffering communication (MSG)
Multiple independent processes can communicate with each other through a message buffering mechanism. This communication is implemented with the message buffer as the intermediate medium. Both send and receive operations on both sides of the communication are in message units. In memory, message buffers are organized into queues, often called message queues. Once created, Message Queuing can be shared by multiple processes. The process that sends the message can send any message to the specified message queue at any time and check to see if there is a message waiting for it to be sent by the receiving process. If any, wake it up: The process that receives the message can get the message on the specified message queue when the message is needed. If the message hasn't arrived yet. The sleep state waits.

Shared Memory communication
The disadvantage of CPU-intensive message replication is required for message buffering. The OS provides a way for direct data exchange between processes a shared memory as the name implies. This mode of communication allows multiple processes to communicate using the same memory segment (as intermediate media) with the support of an external communication protocol or a synchronous, mutually exclusive mechanism. It is one of the most effective means of data communication, which is characterized by no intermediate links. Connect the shared memory page directly via the attached connection. Mapped into the respective virtual address space of the processes that communicate with each other. This allows multiple processes to access the same physical memory page directly. Like accessing your own private space (but in essence not private but shared). Therefore, this inter-process communication method is the quickest way to realize communication among the processes in the same computer system. And its limitations lie in this. Processes that share memory must coexist with the same computer system. There is physical memory that can be shared before the line.

Three ways to characterize (pros and cons):

1. The nameless pipe is simple and convenient. But limited to the way communication works. And the pipeline can only be shared between the process that created it and its descendants: A well-known pipeline can be used by a process that is free of any relationship. However, because of its long-term presence in the system, improper use error prone.

2. Message buffering can no longer be limited to parent-child processes. It allows any process to implement interprocess communication through shared Message Queuing. and is called by the system function to achieve the synchronization between message sending and receiving. This makes it unnecessary for users to consider synchronization issues when communicating using message buffering. Easy to use, but the duplication of information requires additional CPU time. It is not suitable for occasions with large amount of information or frequent operation.

3. Shared memory for the shortcomings of message buffering and the use of memory buffers to exchange information directly, without duplication, fast, information is the advantage of large. However, shared memory is communicated by attaching a shared memory buffer directly to the virtual address space of the process. Therefore, the synchronization of read and write operations between these processes is not possible with the operating system. Must be resolved 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 that are in the same computer system. Network communication is not convenient.

Add:

1.

# pipe: A pipe is a half-duplex mode of communication in which data can only flow in one direction and can only be used between processes that have affinity. A process's affinity usually refers to a parent-child process relationship.

# famous pipe (named pipe): A well-known pipe is also a half-duplex mode of communication, but it allows communication between unrelated processes.

# semaphore (Semophore): 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 a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.

# message Queue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.

# signal (sinal): A signal is a more sophisticated means of communication that notifies the receiving process that an event has occurred.

#共享内存: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes. # Socket: Socket is also an inter-process communication mechanism, unlike other communication mechanisms, it can be used for different and inter-process communication.

The main limitations of the pipeline are reflected in its characteristics:
Only one-way data streams are supported;
Can only be used between processes that have affinity;
No Name;
The buffer of the pipeline is finite (piping is present in memory and is allocated a page size for the buffer when the pipeline is created);
The pipeline transmits the unformatted byte stream, which requires that the reader and writer of the pipeline must agree the format of the data beforehand, such as how many bytes count as a message (or command, or record), etc.

2.

Four different technologies for interprocess communication (IPC):
1. Message delivery (pipelines, fifo,posix, and System V Message Queuing)
2. Synchronization (mutexes, condition variables, read and write locks, file and record locks, POSIX and System V beacons)
3. Shared memory area (anonymous shared memory area, known as POSIX shared memory area, known as System V shared memory area)
4. Procedure call (Solaris Gate, Sun RPC)
Message Queuing and procedure calls are often used alone, meaning that they often provide their own synchronization mechanisms. Instead, shared memory areas typically require some form of synchronization provided by the application to function properly. What IPC should be used to solve a particular problem does not have a simple decision, The mechanisms provided by the various IPC forms should be gradually familiarized, and their characteristics should be compared according to the requirements of the particular application.

There are four prerequisites to consider:
1. Networked or non-networked. IPC applies to processes or threads on a single host. If your application is likely to be distributed across multiple hosts, consider using sockets instead of IPC to simplify the later work of transferring to networked applications.
2. Portability.
3. Performance, run the test program in the specific development environment, compare the performance differences of several IPC.
4. Real-time scheduling. If this feature is required and the system used also supports POSIX real-time scheduling options, consider using POSIX messaging and synchronization functions.

Some of the major differences between the various IPC:
1. Pipelines and FIFO are byte streams with no message boundaries. POSIX messages and System V messages have record boundaries maintained from the sender to the recipient (Eg:tcp is a byte stream without a record boundary, and UDP provides a message with a record boundary).
2. When a message is placed in an empty queue, POSIX Message Queuing can send a signal to a process or start a new thread. System v does not provide a similar form of notification.
3. The data bytes of the pipeline and FIFO are FIFO. POSIX messages and System V messages have priority assigned by the sender. When you read from a POSIX message queue, the message that is always the highest priority is returned first. When you read from a System V message queue, The reader can request any priority messages that it wants.
4. In the numerous messaging technologies-pipelines, Fifo,posix Message Queuing and System V Message Queuing-functions that can be called from a signal handler are only read and write (for pipelines and FIFO).

When comparing different forms of message delivery, we are interested in two measurement scales:
1. Bandwidth (bandwidth): the speed at which data is transferred through the IPC channel. To measure this value, we send a large amount of data (millions of bytes) from one process to another. We also measure this value for different sizes of I/O operations, such as write and read operations for pipelines and FIFO. Expect to see a pattern of bandwidth growth as the amount of data for each I/O operation grows.
2. Delay (latency): A small IPC message takes time from one process to another to make a process return. We measured only one 1-byte message from one process to the time (round-trip time) of a process coming back

In the real world, bandwidth tells us how long it takes to send large chunks of data through an IPC channel, but the IPC is also used to deliver small control information, and the time it takes for the system to process these small messages is delayed. These two numbers are important.

3 advanced ways to communicate between Linux processes and their pros and cons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.