Linux inter-process communication (IPC)

Source: Internet
Author: User
1. pipelines: pipelines are one of the first UnixIPC formats supported by Linux. they have the following features: pipelines are half-duplex and data can only flow in one direction. two pipelines must be established when both parties need to communicate with each other; it can only be used between parent and child processes or brothers (kinship processes); single...
1. pipelines: pipelines are one of the original Unix IPC formats supported by Linux. they have the following features: pipelines are half-duplex and data can only flow in one direction. two pipelines need to be established when both parties need to communicate; it can only be used between parent and child processes or brothers (kinship processes). it forms an independent file system: an MPs queue is a file for processes at both ends of the MPs queue, but it is not a common file, it does not belong to a certain file system, but a self-built Portal, a separate file system, and only exists with memory. Data reading and writing: the content written by a process to the pipeline is read by the process at the other end of the pipeline. The written content is added at the end of the MPs queue buffer, and data is read from the buffer header each time. Www.2cto.com I/O functions of common files can be used in pipelines, such as close, read, and write. Read data from the pipeline: if the write end of the pipeline does not exist (or when a write end has been closed), after all data is read, it is deemed that the data has been read to the end, and the number of bytes returned by the read function is 0. (technically, when there is a process on the write end of the pipeline, the file will not end) when the write end of the pipeline exists, if the number of bytes requested is greater than PIPE_BUF, the number of existing data bytes in the pipeline is returned. if the number of bytes requested is not greater than PIPE_BUF, return the number of existing data bytes in the MPs queue (in this case, the data volume in the MPs queue is smaller than the requested data volume), or the number of returned bytes (in this case, the data volume in the MPs queue is not smaller than the requested data volume ). Note: PIPE_BUF is defined in include/linux/limits. h. different kernel versions may vary. Posix.1 requires that PIPE_BUF be at least 512 bytes, and red hat 7.2 is 4096 ). Write data to the MPs queue: when www.2cto.com writes data to the MPs queue, linux does not guarantee the atomicity of the write. Once the MPs queue buffer has an idle area, the write process tries to write data to the MPs queue. If the read process does not read data from the buffer in the pipeline, the write operation will be blocked. Note: it is meaningful to write data to the MPs queue only when the read end of the MPs queue exists. Otherwise, the process that writes data to the pipeline receives the SIFPIPE signal from the kernel. The application can process the signal or ignore it (the default action is to terminate the application ). A pipeline can be used to redirect input and output. it directs the output of one command to the input of another command. The main limitation of a pipeline is its characteristics: only one-way data stream is supported; it can only be used between kinship-related processes; there is no name; the buffer of the MPs queue is limited (the MPs queue system exists in the memory and a page size is allocated to the buffer when the MPs queue is created). The MPs queue transmits a non-formatted byte stream, this requires that the reader and writer of the MPs queue have to specify the data format in advance, such as the number of bytes counted as a message (or command, or record). 2. FIFO: it can be used to exchange information between multiple non-relatives. FIFO open rules: if a read-only FIFO is enabled for the current open operation, if a corresponding process has enabled this FIFO for writing, the current open operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for writing (the blocking flag is set for the current open operation); or, it will be returned successfully (the blocking flag is not set for the current open operation ). If the current enable operation is to enable the FIFO for writing, if a corresponding process has opened the FIFO for reading, the current enable operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for reading (the blocking flag is set for the current open operation); or, An ENXIO error is returned (the blocking flag is not set for the current open operation ). Read data from FIFO: Convention: If a process blocks the access to FIFO in order to read data from FIFO, the read operation in the process is a read operation with a blocking flag. Www.2cto.com if a process writes to enable FIFO and there is no data in the current FIFO, the read operation with the blocking flag is blocked all the time. -1 is returned if no blocking mark is set for the read operation. The current errno value is EAGAIN, prompting you to try again later. For read operations with the blocking flag configured, there are two reasons for blocking: data in the current FIFO, but other processes are reading the data. In addition, there is no data in the FIFO. The reason for blocking is that there are new data writes in The FIFO, regardless of the size of the data written to the message or the amount of data requested by the read operation. The read blocking mark only applies to the first read operation of the process. if there are multiple read operation sequences in the process, after the first read operation is awakened and the read operation is completed, other read operations to be executed will not be blocked, even if there is no data in the FIFO when the read operation is executed (at this time, the read operation returns 0 ). If no process write is enabled, the read operation with the blocking flag set will be blocked. Note: if data exists in the FIFO, the read operation with the blocking flag is not blocked because the number of bytes in the FIFO is smaller than the number of bytes requested to read, the read operation returns the existing data volume in the FIFO. Writing data to a FIFO: Convention: If a process blocks the opening of a FIFO to write data to a FIFO, write operations in the process are the write operations with a blocking flag. For write operations with a blocking flag set: when the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of write operations. If the idle buffer of the pipeline is insufficient to accommodate the number of bytes to be written, the system goes to sleep until the buffer can accommodate the number of bytes to be written. When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing. As soon as the FIFO buffer has an idle area, the write process will attempt to write data to the pipeline, and the write operation will return after writing all the data written by the request. For write operations without blocking flag set: when the data volume to be written to www.2cto.com is larger than PIPE_BUF, linux will no longer guarantee the atomicity of write operations. After the buffer is fully written into all the FIFO idle buffers, the write operation returns. When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing. If the current FIFO idle buffer can accommodate the number of bytes written by the request, the result is returned successfully. if the current FIFO idle buffer cannot accommodate the number of bytes written by the request, the EAGAIN error is returned, reminder to write again later; 3.XSI IPc structure they all belong to the IPC structure in the kernel, they are described using identifiers. this identifier is a non-negative integer. Unlike the file descriptor, it is not used to delete the recycled integer at the time of creation. Instead, each time it is plus 1, until the maximum integer value is 0. the identifier is the internal name of the IPC object, and its external name is the key (key). its basic type is key_t. Is defined as a long integer. the key is changed from the kernel to the identifier. xsi ipc disadvantages: the content in message queue and shared memory will not be automatically deleted; file descriptor is not used, and multi-channel I/o functions cannot be used: select and poll advantages: reliability, the stream is controlled. you can use non-first-in-first-out (1) message queue www.2cto.com. The maximum flexibility of a message queue is that we can define the data type of the message passed to the queue by ourselves, message Queue consumes less memory than the channel (2) shared memory is the fastest IPC (3) Semaphore on linux, which consumes 60% more than the semaphore; however, to implement simple functions, record is generally used because it is simpler. Author: Suo long
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.