Summary of inter-process communication between Linux

Source: Internet
Author: User
Tags mutex posix semaphore

Just asked for a marriage leave, during leave to do some technical summary, one of which is the Linux process of communication between the summary.

Linux provides a variety of ways to communicate between processes, as listed below:

    • Pipe (piping)
    • FIFO (advanced first Out, also known as pipe)
    • Domain_socket (Domain sockets)
    • Xsi-semaphore (XSI signal volume)
    • Xsi-message_queue (XSI Message Queuing)
    • Xsi-shared_memory (XSI shared memory)
    • Posix-semaphore (POSIX semaphore)
    • Mutex (mutex)

Where semaphore and mutexes are synchronous between processes or threads, but synchronization is essentially a form of communication, it is also listed here.

PIPE

Pipelines are the oldest and most commonly used inter-process communication method, and we use the LS in the Linux command line | grep Crond is the use of pipelines to redirect the output of the previous process to the input of the latter process. The main disadvantages of the pipeline are:

    1. Can only be used between a parent-child or other process with a common ancestor;
    2. is simplex communication.
Fifo

In order to overcome the shortcomings that pipelines can only be used for parent-child processes, a well-known pipeline is introduced, also known as first-in, first-out queues. FIFO operations are similar to normal files, but must have both a write and a read end to operate.

The main improvements relative to Pipe,fifo are:

    1. Can be used between any of the two processes
    2. Access permissions can be controlled through the properties of the FIFO file
Domain Socket

A domain socket is a type of communication similar to a network socket, but between two processes dedicated to the same host, it is a duplex mode of communication.

XSI Semaphore

XSI is the abbreviation for the X/open system interfaces, which is a standard for the systems interface, which specifies three modes of communication, namely semaphore, Message queue, shared memory, and they have a consistent programming interface. There are two types of semaphores that can be used on a Linux system, one for Xsi_semaphore and one for Posix_semaphore, and the other to explain what is posix_semaphore.

In non-mandatory cases, individuals are not recommended to use XSI interprocess communication because it is more complex. In addition to shared memory, there are other workarounds for the two XSI interprocess communication methods. The approximate flow of xsi interprocess communication is as follows:

    1. Two processes get a key
    2. Two processes use this key to get the ID of interprocess communication
    3. Two processes for interprocess communication
    4. One of the processes frees the ID of interprocess communication

The most troublesome thing is that the ID of interprocess communication does not automatically release with the end of the process and must be released manually.

Semaphores are used to protect the access of a different process or thread to a limited resource, before the process accesses the resource, the semaphore needs to be acquired, and the semaphore is released after the access is over. When the semaphore current value equals 0 o'clock, the process that gets the semaphore must wait until there are other processes releasing the semaphore.

XSI Message Queue

Message Queuing is another way of XSI inter-process communication through two processes that read and write to the same kernel message queue for interprocess communication.

XSI Shared Memory

Shared memory is one of the most efficient inter-process communication methods that directly maps a piece of memory to two processes, and then two processes read and write this memory to enable interprocess communication. The price of it is to keep the process synchronized to prevent data inconsistencies.

POSIX Semaphore

Compared to XSI semaphore,posix Semaphore to be easy to use more.

Mutex

In general, mutexes are used only for inter-thread synchronization, but they can also be used for interprocess communication by mapping a mutex variable to the address space of two different processes and setting the properties of the mutex's inter-process sharing. If you have a special hobby for a mutex, you can use it.

Code

All the code has been uploaded to Https://github.com/clpsz/linux-ipcs. Although each directory has only one main function, the code uses fork to create two processes, but apart from pipes, other interprocess communication methods are not limited to parent-child processes, and fork is used only to facilitate testing.

Summary of inter-process communication between Linux

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.