C-language pipelines in Unix

Source: Internet
Author: User

First, the concept of pipelines

A pipeline is a data structure of a queue type whose data is entered from one end and output at the other. The most common application for pipelines is to connect the input and output of two processes, which is to program the output of one process to the input of another process. There is a specialized pipe operator "|" in the shell, such as a shell command:

Ps-ef |grep Init

The command "PS-EF" parses all the processes that are currently running and prints the results to the screen. The process "grep init" looks for a substring containing the character "Init" from the input string and prints the result. These two brought life are connected by a pipe character and become a new application: Find the process that is being applied that contains the character "Init" in the name.

Second, nameless pipe

The nameless pipe, which is directly called a pipe, occupies two file descriptors and cannot be shared by unrelated processes, and is generally used in parent-child processes.

1. The establishment of nameless pipes

Everything in Unix is a file, and a pipe is a file that becomes a pipe file. When the system creates a pipeline, it returns two file descriptors: one file opens as a write-only, as input to the pipeline, and the other file opens as read-only, as the output of the pipeline.

In Unix, a function pipe is used to create a nameless pipe, whose prototype is:

#include <unistd.h>

int pipe (int fildes[2]);/* where fildes[0] is read and open, Fildes[1] is written, fildes[1] output is fildes[0] Input */

The function pipe creates a pipe in the kernel and assigns two file descriptors to identify both ends of the pipeline, and these two file descriptors are stored in fildes[0] and fildes[1]. General Convention Fildes[0] is the input, the process writes data to this file descriptor, Fildes[1] describes the output of the pipeline, and the process reads the data into this file descriptor. The function returns 0 if the pipe call succeeds, or 1.

2. Unidirectional Pipe flow model

Both ends of the pipe (input and output) are controlled by a process without much meaning, and if both ends of the pipeline are controlled in a non-pass process, communication between the two processes is possible. A process that has a pipeline input can send information to the pipeline, a process that has a pipeline output, and can receive information sent from a process from a pipeline.

1) pipeline flowing from parent process to child process

After a parent process creates a nameless pipe and produces a child process, both parent and child processes have access to both ends of the pipeline. Closing the pipeline output of the parent process, closing the pipeline input for the child process, forms a pipeline flow from the parent process to the child process, and the data is written by the parent process and read out from the child process. The process of creating a pipeline that flows from the parent process to the child process is as follows:

1st: Create a pipeline that returns two file descriptors Fildes[0] and fildes[1] for the nameless pipe.

int fildes[2];

Pipe (Fildes);

2nd: Create child process, child process continues nameless pipe file descriptor.

3rd: The parent process closes the output of the pipeline, which closes the read-only file descriptor Fildes[0].

Close (Fildes[0]);

4th: The child process closes the input side of the pipeline, which closes the write-only file descriptor fildes[1].

Close (fildes[1]);

2) the pipeline that pops the parent process from the child process

After a parent process creates a nameless pipe and produces a child process, both parent and child processes have access to both ends of the pipeline. Close the pipeline input of the parent process, close the pipeline output of the child process, and form a pipeline flow from the child process to the parent process, and the data is written to and read from the parent process. The process of creating a pipeline from a child process to a parent process is as follows:

1st: Create a pipeline that returns two file descriptors for nameless pipes fildes[0] and fildes[1];

2nd: Create child process, continue nameless pipe file descriptor in child process.

3rd: The parent process closes the input of the pipeline, that is, close the read-only file descriptor fildes[1];

4th: The child process closes the output of the pipeline, that is, write-only file descriptor fildes[0];

Ex: an example of a pipeline: the parent process writes a line of characters to the pipe, and the child process reads the data and prints it to the screen.

C-language pipelines in Unix

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.