linux-Piping Pipe

Source: Internet
Author: User

Piping (pipe)

In Linux, pipelines are also a kind of file, but more special, we can use the pipe function to create a pipeline, its prototype is declared as follows:

# Inlcude <unistd.h>
intpipe (int fields[2]);

tune? Use the pipe function to open up in the kernel? A buffer (called a pipe)? For communication, does it have a read end? A write end, then the
Pass the Filedes parameter out to the user program two? File descriptor, Filedes[0] point to the read end of the pipe, filedes[1] point to the
write end of the pipe (very well remember, like 0 is standard output?). So the pipeline in the? User program looks like a. File that opens
, via Read (Filedes[0]), or write (filedes[1]); Read and write data to this file is actually in the read-write kernel buffer
area. The pipe function is returned with a success of 0, and the adjustment is returned with a failure-1.

In the following example, a pipeline is created as a communication buffer, the parent process creates a child process, and the child process writes a string to the pipeline through the fields[1] descriptor of the pipeline.

The parent process uses the pipe's fields[0] to read the string from the pipeline and display it:

#include

<stdio.h>
#Include <stdlib.h>
#Include <unistd.h>
#Include <String.h>
#Include <errno.h>
#Include <sys/types.h>
#Include <sys/wait.h>

#define Buf_siz255Message buffer size

int main (int argc,Char **argv)
{
Char Buffer[buf_siz +1];
int fd[2];

Receive a string as parameter
if (argc! =2)
{
fprintf (stderr,"Usage:%s string\n\a", argv[0]);
Exit1);
}

Create pipe for communication
if (pipe (FD)! =0)
{
fprintf (stderr,"Create Pipe Error:%s\n\a", Strerror (errno));
Exit1);
}

if (fork () = =0)In child process write MSG to pipe
{
Close (fd[0]);
printf"Child%ld Write to Pipe\n\a", Getpid ());
snprintf (buffer, Buf_siz,'%s ', argv[1]);
Write (fd[1], buffer, strlen (buffer));
printf"Child%ld Quit.\n\a", Getpid ());
}
ElseIn the parent process, read MSG from the pipe
{
Close (fd[1]);
        printf ( "Parent %ld  Read from pipe\n\a ",  getpid ());
        memset (Buffer, 1);
        read (Fd[0], buffer, &NBSP;BUF_SIZ);
        printf ( "Parent %ld  Read : \n%s\n ",  getpid (),  buffer);
        exit (return  0;
}

Capacity of the pipe:

Method One: Use Linux's ulimit-a to view system limits:

So the size of one atom input is: 512Byte * 8=4096byte;

To view the number of buffer entries: cat/usr/src/kernels/3.10.0-327.el7.x86_64/include/linux/pipe_fs_i.h file,

found that there are 16 buffer entries, and then calculated the capacity of the pipeline size: 16*4096byte=64kb;

Method Two: We can also check the manual: Man 7 Pipe query capacity pipe capacity:

Internal organization of pipelines:

In Linux, the implementation of pipelines does not use a dedicated data structure, but instead uses the file structure of the filesystem and the index node inode of the VFS.

By pointing two file structures to the same temporary VFS index node, the VFS index node points to a physical page. There are two

File data structures, but they define files with a different operation routine address, one of which is a routine address that writes data to the pipe, and the other is from the pipe

The sample address of the data read out in the Tao. In this way, the user program's system call is still the usual file operation, and the kernel uses this abstract mechanism to implement the pipe

The special operation of the road.

linux-Piping Pipe

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.