Linux interprocess communication (ii) The Nameless pipeline of pipeline communication and its basic experiment

Source: Internet
Author: User
Tags file system

Pipe Introduction

Pipelines are a way of interprocess communication in Linux, which connects the output of a program directly to the input of another program (in fact, I prefer to liken the pipe to the pipe of a rural land). The main pipeline of Linux includes two kinds: nameless pipe and famous pipe. This section mainly talks about the nameless pipeline, first introduces these two pipes. (the feature is very important!) )

1, Nameless pipe

The nameless pipe is an original method of piping communication in Linux, as shown in Figure one (left), which has the following characteristics:

① It can only be used for communication between relational processes (that is, between a parent-child process or a sibling process);

② It is a Half-duplex communication mode, with fixed read-end and write-end;

The ③ pipeline can also be viewed as a special kind of file, and it can also use the normal read () and write () functions for its reading and writing. However, it is not a normal file and does not belong to any other file system and exists only in memory.

2, famous pipeline (FIFO)

A well-known pipeline is an improvement to a nameless pipe, as shown in Figure 1 (right), which has the following characteristics:

① it can communicate with each other between two processes that are unrelated to each other;

② the pipe can be indicated by a pathname and is visible in the file system. After the pipeline is established, two processes can read and write as normal files, which is very convenient to use.

③fifo strictly follow the advanced first out rules, to the pipeline and FIFO read always return data from the beginning, write to them is to add the data to the end, they do not support such as Lseek () and other file positioning operations.

Nameless pipes and their system calls

1, pipe creation and piping instructions

Pipelines are the means of communication based on file descriptors, when a pipe is established, it creates two file descriptors fd[0] and fd[1], where fd[0] is fixed for the read pipe, and fd[1 is fixed for the write pipe, as shown in Figure 2, which constitutes a half-duplex channel.

When the pipe is closed, you can close the two file descriptors, using the normal close () function to turn off individual file descriptors.

2. Pipe creation function

Creating a pipe can be implemented by calling pipe (), as shown in the following table

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.