Inter-process communication

Source: Internet
Author: User

Due to some memory management mechanisms, the user space of processes is independent of each other and generally cannot access each other. The only exception is shared memory. Therefore, we need to use some media to complete inter-process communication.

Inter-process communication mode:

(1) Pipelines

In Linux, pipelines are divided into nameless and named pipelines. The former is used for communication between parent and child processes, and the latter is used for communication between any two processes. An unknown pipeline is created by the pipe (INT filedis [2]) function. The filedis parameter returns two file descriptors. Filedes [0] is enabled for reading, and filedes [1] is enabled for writing. Output of filedes [1] is the input of filedes [0.

In Linux, famous pipelines can be created in two ways: Command Line mknod System Call and function mkfifo to generate a famous myfifo pipeline. After a famous pipeline is generated, you can use common file I/O functions such as open, close, read, and write to operate it.

(2) Message Queue (rarely used)

Message Queue is used for communication between processes running on the same machine. It is similar to the pipeline and is a queue used to store messages in the system kernel, it appears in the system kernel in the form of a message linked list. The node Structure in the message linked list is declared with MSG.
In fact, it is a communication method that is gradually being eliminated. We can replace it with a Stream pipeline or an interface. Therefore, we will not explain this method, we recommend that you ignore this method.

(3) shared memory (most used)

Shared memory is the fastest way to communicate between two processes on the same host. Because data does not need to be replicated between different processes, a process usually creates a shared memory zone, other processes read and write the memory area. There are two ways to get the shared memory zone: ing/dev/MEM devices and memory image files. The previous method does not bring additional overhead to the system, but is not commonly used in reality, because it controls the access to the actual physical memory. The latter method is commonly used: The shmxxx function family is used to store data using shared memory.

The first function to be used is shmget (), which obtains a shared storage identifier --> after the shared memory is created, other processes can call shmat () to connect it to their own address space.

When using shared storage to implement inter-process communication, note the synchronization of data access. Make sure that the desired data has been written when a process reads data. You can use the shmctl function to set some flags of the shared storage memory, such as shm_lock and shm_unlock. But more often we use the semaphores to synchronize shared storage data access.

(4) Signal

A semaphore is a counter used to record access to a resource (such as shared memory. Generally, to obtain shared resources, the process must perform the following operations:

(A) Test the semaphore that controls the resource

(B) When the semaphore is positive, the resource can be used. The process will subtract 1 from the semaphore.

(C) When the semaphore is 0, the resource is currently unavailable and the process enters the sleep state until the semaphore is greater than 0. The process is awakened and switched to step ()

(D) When a process no longer uses a semaphore-controlled resource, the signal value is increased by 1. If a process is sleeping and waiting for this semaphore at this time, it will be awakened.

(3) socket

Socket programming is one of the main ways to implement inter-process communication between Linux and most other operating systems. The well-known WWW Service, FTP service, and Telnet service are implemented based on a set of interface programming. In addition to remote computer processes, the set of interfaces also apply to inter-process communication within the same local computer.

Inter-process communication

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.