Linux system Knowledge-text flow & pipeline

Source: Internet
Author: User
Tags stdin

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Reference links

Http://www.cnblogs.com/vamei/archive/2012/09/14/2683756.html

Http://www.cnblogs.com/vamei/archive/2012/10/10/2715398.html

Text Flow principle

Everything is text, everything is text flow

When Unix executes a program, it automatically opens three streams, stdin, stdout, and stderr.

By default, stdin connects to the keyboard, stdout, and stderr connect to the screen.

Although all open, but not all must use.

Text Flow Example

Keystrokes ("ls\n") of the stdin, flow into the command line (command line is also a program),

The command line calls/bin/ls to get the output stream ("A.txt"),

The output stream flows to the screen.

redirect

Directing custom content to stdout, Echo

REDIRECT stdout, using ' > ' or ' >> '

redirect stdin, using ' < '

REDIRECT stderr, using ' 2> ' or ' 2>> '

REDIRECT stdout and stderr using ' >& '

redirect Example

Cat < a.txt > B.txt # Copy the contents of A.txt to the B.txt

Pipeline

To direct the output of one command to the input of another command

Use pipelines to connect multiple processes together, allowing each process to collaborate to achieve complex functionality.

Pipeline understanding

A pipeline is a buffer that is managed by the kernel, which is the equivalent of a small note that we put into memory.

One end of the pipeline connects the input of a process, and the other end connects to the input of another process.

The pipe does not need to be large, and he is designed as a data structure for circular data so that it can be recycled.

If there is no information in the pipeline, the process read from the pipeline waits.

When the pipeline is full of information, the process of trying to put information waits.

The pipe automatically disappears when all two processes are terminated.

Piping principle

The pipeline is built using the fork mechanism so that two processes can be connected to the same pipe.

At the beginning, the input and input of the pipeline are connected to the same process Process1,

When you fork the copy process, both connections are copied to the new process PROCESS2.

Subsequently, two processes each shut down one of their unwanted connections, forming a pipe that connects two processes

Fifo

http://blog.csdn.net/lianghe_work/article/details/47722175

Because pipelines are built on the fork mechanism, pipelines can only be applied between parent-child processes, or between two child processes that have the same ancestor. To solve this problem, Linux provides a FIFO way to establish a connection.

FIFO is also called named pipe

FIFO is a special type of file that has a corresponding path in the file system, but it is managed by the kernel.

When a process opens the file in a read manner, and another process opens the file in a written manner, the kernel creates a pipeline between the two processes.

FIFO is a file in the file system, but his content is in memory.

FIFO files still exist in the file system after the process is exited with FIFO

FIFO has a name, unrelated processes can communicate by opening a named pipe

Linux system Knowledge-text flow & pipeline

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.