Inter-process communication in Linux: pipelines and famous Pipelines

Source: Internet
Author: User
Article Title: inter-process communication in Linux: pipelines and famous pipelines. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Pipelines and famous Pipelines

In this series, the author outlines several main methods for inter-process communication in linux. Among them, pipelines and named pipelines are one of the earliest inter-process communication mechanisms. pipelines can be used for inter-process communication with kinship. Famous pipelines overcome the restrictions that pipelines do not have names, in addition to the functions of pipelines, it also allows communication between unrelated processes. Recognizing the read and write rules of pipelines and named pipelines is the key to applying them in programs. This article discusses in detail the communication mechanism of pipelines and named pipelines, the instance is used to verify its read/write rules by program. This helps to enhance the reader's perceptual knowledge of the read/write rules, and also provides application examples.

1. Pipeline overview and related API applications

1.1 key concepts related to pipelines

As one of the first Unix IPC formats supported by Linux, pipelines have the following features:

The pipeline is half-duplex, and data can only flow in one direction. when both parties need to communicate, two pipelines need to be established;
It can only be used between parent and child processes or sibling processes (kinship processes );
An independent file system is formed: 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 file system, but a self-built portal, A file system exists only in 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.

1.2 create an MPS queue:

# Include Int pipe (int fd [2])
The two ends of the pipeline created by this function are in the middle of a process, which does not make much sense in actual application. Therefore, after a process creates a pipeline by pipe (), it generally fork another sub-process, then, the communication between parent and child processes is realized through the pipeline (so it is not difficult to launch. As long as there is a kinship between the two processes, the kinship here refers to a common ancestor, you can use pipelines for communication ).

1.3 MPs queue read/write rules:

The describe characters fd [0] and fd [1] can be used to describe the two ends of the MPs queue respectively. Note that the two ends of the MPs queue are fixed with tasks. That is, one end can only be used for reading. It is represented by the descriptive word fd [0] And called as the pipeline Reading end. The other end can only be used for writing, which is represented by the descriptive word fd [1, it is called the pipe write end. An error occurs if you try to read data from the write end of the pipeline or write data to the read end of the pipeline. Generally, file I/O functions can be used in pipelines, such as close, read, and write.

Read data from the MPs queue:

If the write end of the pipeline does not exist, it is deemed that the data has been read to the end, and the number of read bytes returned by the READ function is 0;
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 ).
Read rule verification for pipelines:

/*************** Readtest. c *****************/# include # Include

Write Data to the MPs queue:

When writing data to an MPS queue, linux does not guarantee the atomicity of writing data. As soon as 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 ).
Verification of write rules for pipelines 1: dependency of the write end on the read end

# Include # Include Main () {int pipe_fd [2]; pid_t

The output result is: Broken pipe because the pipeline and the read ends of all its fork () products have been closed. If the read end is retained in the parent process, that is, after writing pipe, the read end of the parent process is shut down and pipe is written normally. You can verify this conclusion by yourself. Therefore, when writing data to the MPs queue, there should be at least one process, in which the read end of the MPs queue is not closed; otherwise, the above error will occur (the MPs queue is broken and the process receives the SIGPIPE signal, the default action is Process Termination)

Verification of write rules for pipelines 2: linux does not guarantee atomic verification of write Pipelines

# Include # Include # Include Main (int argc

Conclusion:

When the number of writes is less than 4096, the write is non-atomic!
If you change the number of bytes written to the parent process to 5000 twice, you can easily draw the following conclusion:
When the amount of data written to the MPs queue exceeds 4096 bytes, the idle space in the buffer zone is written into the data (completed) until all data is written. If no process reads data, the data is blocked.

[1] [2] [3] Next page

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.