You may not know Linux interprocess communication

  

Linux interprocess communication (IPC) has several ways, the following will be briefly described:

one. Pipe

Pipeline is the initial IPC mode supported by Linux. Pipes can be divided into unnamed pipes, named pipes, and so on.

(1) Anonymous pipeline, which has several characteristics:

1) The pipeline is half-duplex and can only support one-way flow of data; it needs to be established when communication between two processes is required. Two pipes;

2) Nameless pipes are created using the pipe() function and can only be used between parent and child processes or between sibling processes;

3) Pipes for both ends of the communication In other words, it is essentially a separate file that exists only in memory;

4) Data read and write operations: A process writes data to the pipeline, and the data written is added to the end of the pipeline buffer. Another process reads data at the head of the buffer in the pipeline.

(2) Named Pipes

A named pipe is also half-duplex, but it allows communication between processes that are not related. Specifically, the well-known pipeline provides a path name associated with it, and exists in the file system in the form of FIFO (first in, first out). This allows even unrelated processes to communicate with each other through FIFOs as long as they have access to the paths already provided.

It is worth noting that it only makes sense to write data to the pipeline when the pipeline has a read end. Otherwise, the process that writes data to the pipe receives the SIGPIPE signal from the kernel; the application can customize the signal handler or ignore the signal directly.

II. Semaphores

Semaphores are counters that control the simultaneous access of resources by multiple threads or processes across processes. It is often implemented as a lock mechanism. Essentially, the semaphore is a protected variable and can only be accessed through initialization and two standard atomic operations (P/V). (P, V operations are also often referred to as wait(s), signal(s))

three. Signals

Signals are one of the oldest methods of interprocess communication used in Unix systems. The operating system signals to a certain process that a predetermined event has occurred; the process that receives the signal can choose to process the signal in different ways. First, the default processing mechanism &mdash can be used; the process is interrupted or exited, and the process is ignored. The signal, as well as the processing function that customizes the signal, performs the corresponding action.

The kernel produces signals for processes to respond to different events, which are the source of the signal. The signal source can be: exception, other processes, terminal interrupt (Ctrl-C, Ctrl+\\, etc.), job control (front, background process management, etc.), allocation amount problem (cpu timeout or file too large, etc.), kernel Notifications (such as I/O Ready, etc.), alarms (timers).

Four. Message Queue

A message queue is a linked list of messages that allows one or more processes to write messages to it, and one or more processes read messages to it. Linux maintains a message queue vector table: msgque to represent all message queues in the system.

Message Queuing overcomes the lack of signaling information, and the pipeline can only support the disadvantages of unformatted byte streams and buffer limitations.

Five. Shared memory

Shared memory is mapped to a piece of memory that can be accessed by other processes. This shared memory is created by one process and then other processes can be mounted into this shared memory. Shared memory is the fastest IPC mechanism, but since Linux itself cannot implement synchronous control and requires user program for concurrent access control, it generally combines other communication mechanisms to achieve inter-process communication, such as semaphores.

Five. Sockets

Sockets are also an inter-process communication mechanism, but the main difference between it and other communication methods is that it can implement process communication between different hosts. A socket can be thought of as an endpoint for interprocess communication. The name of each socket is unique; other processes can access, connect, and communicate data.

Copyright © Windows knowledge All Rights Reserved