The use of nameless pipes and dup,dup

Source: Internet
Author: User

Resources:

Http://www.tldp.org/LDP/lpg/node11.html

http://blog.csdn.net/yeyuangen/article/details/6852682

Http://blog.sina.com.cn/s/blog_65c5c5990100mx6d.html

Pipeline is a very important way of communication in Linux , is to connect the output of a program directly to the input of another program , often said pipe refers to the nameless pipe , A nameless pipe can only be used between a relationship-related process, which is the biggest difference between it and a named pipe.

The following is an example of the simplest anonymous pipeline.

child process, closes the read-out end of the pipe and writes the data to the write end of the pipe

In the parent process, the write end of the pipeline is closed, and the data is read out at the read-out end of the pipeline.

NOTES:FD[0] is set up for reading, fd[1] is set up for writing
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<stdlib.h>#include<string.h>intMainvoid){    intfd[2], nbytes;    pid_t Childpid; Char    string[] ="Hello, world!\n."; Charreadbuffer[ the];        Pipe (FD); if((Childpid = fork ()) = =-1) {perror ("Fork"); Exit (1); }    if(Childpid = =0)    {        /*Child process closes up input side of pipe*/Close (fd[0] ); /*Send "string" through the output side of pipe*/Write (fd[1],string, (Strlen (string)+1)); Exit (0); }    Else    {        /*Parent process closes up output side of pipe*/Close (fd[1] ); /*Read in a string from the pipe*/nbytes= Read (fd[0], Readbuffer,sizeof(Readbuffer)); if(Nbytes! =-1) {printf ("Received string:%s", Readbuffer); }                }                return(0);}

The following uses the DUP with the pipe.

Call Dup2 (fd[1], Stdout_fileno) in the child process; Then printf ("Hello world\n"), the data will be written to fd[1]

Call Dup2 (Fd[0], Stdin_fileno) in the parent process; Then fgets (readbuffer, sizeof (Readbuffer), stdin) will read the data of fd[0].

#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<stdlib.h>#include<string.h>intMainvoid){    intfd[2], nbytes;    pid_t Childpid; Char    string[] ="Hello, world!\n."; Charreadbuffer[ the];        Pipe (FD); if((Childpid = fork ()) = =-1) {perror ("Fork"); Exit (1); }    if(Childpid = =0)    {        /*Child process closes up input side of pipe*/Close (fd[0] ); /*Send "string" through the output side of pipe*/dup2 (fd[1], Stdout_fileno); //write (fd[1], String, (strlen (String) +1));printf"Hello world\n");        Fflush (stdout); Exit (0); }    Else    {        /*Parent process closes up output side of pipe*/Close (fd[1] ); /*Read in a string from the pipe*/dup2 (fd[0], Stdin_fileno); //nbytes = Read (Fd[0], Readbuffer, sizeof (Readbuffer));Fgets (Readbuffer,sizeof(Readbuffer), stdin); if(Nbytes! =-1) {printf ("From the stdin,received string:%s", Readbuffer); }                }                return(0);}

Often, the descriptors in the child is duplicated onto standard input or output. The child can then exec () another program, which inherits the standard streams. Let's look at the dup2 () system call:

Our child process redirects its output to the write end of the pipeline, and then the parent process redirects its input to the read end of the pipeline

Sub-process Close pipe read close (fd[0]);  Call Dup2 (fd[1], Stdout_fileno); Directs the write end multiplicity of the pipeline to standard output

The parent process closes the pipe write-end Close (fd[1]);    Call Dup2 (Fd[0], Stdin_fileno); Directs the read end multiplicity of the pipe to the standard input

The child can then exec () another program, which inherits the standard streams.

Work Flow:

The child process calls EXECLP ("ls", "ls", "1", NULL); ----> Standard OUTPUT-----> write-side-------for pipelines >

The read end of the pipe (parent process)-------> Standard input---->EXECLP ("WC", "WC", "-l", NULL);

The result we see is the result of ls-1|wc-l.

Use of PIPELINE commands:

First Command | Second command

Use the result of the first command as a parameter to the second command

#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<stdlib.h>#include<string.h>intMainvoid){    intfd[2], nbytes;    pid_t Childpid; Char    string[] ="Hello, world!\n."; Charreadbuffer[ the];        Pipe (FD); if((Childpid = fork ()) = =-1) {perror ("Fork"); Exit (1); }    if(Childpid = =0)    {        /*Child process closes up input side of pipe*/Close (fd[0] ); /*Send "string" through the output side of pipe*/dup2 (fd[1], Stdout_fileno); EXECLP ("ls","ls","-1", NULL); Exit (0); }    Else    {        /*Parent process closes up output side of pipe*/Close (fd[1] ); /*Read in a string from the pipe*/dup2 (fd[0], Stdin_fileno); EXECLP ("WC","WC","- L", NULL); }            return(0);}

The use of nameless pipes and dup,dup

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.