Linux Network Programming Learning (VI)-----Pipeline (chapter fourth)

Source: Internet
Author: User

1, the definition of the pipeline

A pipeline is a one-way channel that connects the output of one program to the input of another program, such as a command: Ls-l|more, creates a pipeline, gets the output of ls-l as more input, and the data flows along the pipeline from the left side of the pipeline to the right side of the pipe.

In fact, the kernel establishes two handles for the process F1 and F2, the process writes data to the pipe through the handle F1, while the data is read from the pipe by F2, here is the same process to build its own pipeline

Inter-process pipeline: A child process that is forked out, like a parent process, has a read and write handle to the same pipe, but the pipeline is unidirectional, so to determine the flow of data, such as a parent process to a child process or a child process to a parent process, the unwanted handle is closed

2, the establishment and use of pipelines

1) Create a pipe through the pipe () function declaration is an int pipe (fd[2]), where fd[0] is used to read from the pipe, fd[1] to the pipeline to write

#include <stdio.h> #include <unistd.h> #include <sys/types.h>int main (void) {int fd[2], nbytes;pid_t Childpid;char string[] = "Hello, world!\n"; Char readbuffer[80];p ipe (FD); if ((Childpid = fork ()) = = 1) {perror ("fork"); Exit (1);} The IF (Childpid = = 0) {/* child process closes the read handle of the pipe */close (fd[0]);/* Writes a message to the pipeline via a write handle */write (Fd[1], String, strlen (String)); _exit (0);} else{/* The parent process closes the write handle of the pipe */close (fd[1]);/* Reads the message from the pipe through a read handle */nbytes = Read (Fd[0], Readbuffer, sizeof (Readbuffer));p rintf (" Received string:%s ", Readbuffer);} return (0);}

Assuming that the data flow is a child process to the parent process, the child process writes, the parent process reads, so the subprocess closes the read handle, and the parent process closes the write handle

2) using the Popen ()/pclose () function, the function declaration is file* popen (char * command, Char *type)

Popen () first calls the pipe () to establish a pipeline, then uses the fork () function to establish a subprocess, runs a shell link, and then runs the program specified by the command parameter in this environment, the data pipeline flow is controlled by type, or "R" or "W", Take one of the two, and type only a single character, such as "RW", then only take "R", and finally close with Pclose ()

#include <stdio.h> #define MAXSTRS 5int Main (void) {int cntr; FILE *pipe_fp;char *strings[maxstrs] = {"Roy", "Zixia", "Gouki", "supper", "Mmwan"};/* establish pipeline Popen with */if (PIPE_FP = Popen ("Sort", "w")) = = NULL) {perror ("Popen"); exit (1);} /* Processing loop */for (cntr=0; cntr<maxstrs; cntr++) {fputs (strings[cntr], PIPE_FP); Fputc (' \ n ', PIPE_FP);} /* Close pipe */pclose (PIPE_FP); return (0);}

Popen () can save the source code while also using arbitrary legal shell directives in "command", including redirects and pipelines, as follows: Legal

Popen ("ls ~roy", "R");p Open ("Sort >/tmp/zixia", "W");p open ("sort | Uniq | More "," w ");

The following example establishes two pipelines, a read pipeline, a write pipeline

#include <stdio.h>int main (void) {FILE *pipein_fp, *pipeout_fp;char readbuf[80];/* uses Popen to establish a read pipeline */if (( PIPEIN_FP = Popen ("ls", "r") = = NULL) {perror ("Popen"); exit (1);} /* Use Popen to create a write pipeline */if ((PIPEOUT_FP = Popen ("Sort", "w") = = NULL) {perror ("Popen") to "sort", exit (1);} /* Process Loop */while (fgets (Readbuf, PIPEIN_FP)) {fputs (readbuf, PIPEOUT_FP);} /* Close the Open pipe */pclose (PIPEIN_FP);p close (PIPEOUT_FP); return (0);}

  

3. Tips

1) pipe () call must be in front of fork ()

2) Close the unwanted pipe handle in a timely manner

3) Pipelines can only implement communication between parent-child processes, and if two processes do not have a fork () relationship, other process communication methods must be considered

Linux Network Programming Learning (VI)-----Pipeline (chapter fourth)

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.