The most common application of pipelines in shell is to connect the input and output of different processes, such as converting the output of process A into the input of process B. Test the shell command "cat pipe3.c | more". The process "more" uses the output of the process "cat pipe3.c. Example 1. Redirect the standard input, standard output, and standard error output to the file descriptor fd1, fd2, and fd3 respectively. A: copy the file descriptor fd1 to file descriptor 0 to redirect the standard input. Other similar statements are as follows: Dup2 (fd1, 0);/* Copy fd1 to file descriptor 0 and change the standard input to fd1 */ Dup2 (fd2, 1);/* Copy fd2 to file descriptor 1 and change the standard output to fd2 */ Dup2 (fd3, 2);/* Copy fd3 to file descriptor 2 and change the standard error output to fd3 */ After "dup2 (fd1, 0)" is executed, file descriptor 0 corresponds to the file corresponding to fd1, and some standard output functions, for example, printf and puts still write content to descriptor 0 to achieve redirection. Model The following describes how to connect a standard input of a parent process to a standard input of a child process using a MPs queue: (1) Create an MPS queue and return the two file descriptors fildes [0] And fildes [1] of the unnamed MPs queue. (2) create a sub-process that inherits the file descriptor of an unknown pipeline. (3) The parent process closes the output end of the MPs queue, that is, closes the read-only file descriptor fileds [0]. (4) The parent process redirects the standard output (stdout, file descriptor 1) to the file descriptor fileds [1]. (5) The sub-process closes the input of the pipeline, that is, it closes writing only the file descriptor fileds [1]. (6) The sub-process redirects the standard input (stdin, file descriptor 0) to the file descriptor fileds [0]. The final pipeline flow is shown in Figure 11-7:
Figure 11-7 parent-child process standard I/O pipeline (parent process flows to child process) Instance This section designs an pipeline to connect the standard output stream of the parent process to the standard input stream of the child process. The "Hello!" output by the parent process to stdout !" It is directly transferred to the stdin of the sub-process and obtained by the sub-process "gets (buf)" statement. As shown in Code 11-6: Code 11-6 the output of the parent process connects to the input communication instance of the child process (section from/code/chapter11/pipe3.c) # Include # Include Int main () { Int fildes [2]; Pid_t pid; Int I, j; Char buf [256]; If (pipe (fildes)/* Create pipeline and sub-process */ { Fprintf (stderr, "error! \ N "); Return 1; } If (pid = 0) { /*---Sub-process ――――――――――――――――――*/ Close (fildes [1]); Dup2 (fildes [0], 0);/* redirect stdin to fildes [0 */ Close (fildes [0]); Gets (buf);/* read the input, which is actually to read the output of the parent process */ Fprintf (stderr, "child: [% s] \ n", buf ); Return 2; } /*---Parent process ――――――――――――――――――*/ Close (fildes [0]); Dup2 (fildes [1], 1);/* redirect stdout to fildes [1 */ Close (fildes [1]); Puts ("Hello! ");/* Output, and add the input information of the sub-process */ Return 0; } Compile and run code 11-6: # Make pipe3 Cc-O-o pipe3 pipe3.c #./Pipe3 Child: [Hello!] In the above example, the standard output of the parent process has been redirected to the pipeline, so the parent process puts failed to print the result to the screen.This article is from the ChinaUnix blog. If you want to view the original text, click:Http://blog.chinaunix.net/u3/95011/showart_1913104.html |