File Descriptor Redirection
cat<test01 : Redirecting input to test01 file
cat>test02<test01 : Redirect the standard correct output to the test02 file, and the input device is redirected to the test01 file. Content is overwritten if test02 exists. The output file does not exist and is created automatically.
cat>>test02<test01 : Output append to test02
cat>test02 2>error <test01 : Redirect the standard output to the test02 file, and the error output redirects to the error file. are covered. 2>>error is appended.
cat>test02 2>&1<test01 :& represents the union, redirecting both correct and error messages to the test02 file
Cat 2>&1 1>test02<test01 : Union no transitivity, standard error output in current terminal, correct information input test02
The whole of the following does not figure out how the file descriptor is redirected? What is the mechanism of popen?
REDIRECT Programming
Input redirection: Turn off standard input devices, open or copy normal files, and make their file descriptors 0
Output redirection: Turn off standard output devices and open or copy normal files so that their file descriptors are 1
Error redirection: Turn off standard error input devices, open or copy normal files so that their file descriptor is 2
int dup (int __fd): Copies an open file descriptor that shares the same file table entry as the original file descriptor
The following code redirects the output to the write side of the pipeline
int f_des[2];p ipe (f_des); // Create a nameless pipe close (Fileno (stdout)); // turn off the standard output device DUP (f_des[1]); // returns the lowest available file descriptor, which is a standard output device that has been turned off (descriptor 1)
int dup2 (int __fd, int __fd2) : If FD2 is a file descriptor that is already open, first close the file and then copy it. The success returns the new file descriptor FD2, otherwise returns-1.
Examples of redirection programming:
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>#include<errno.h>#include<sys/stat.h>#defineBuffer_size 1024intMainintargcChar*argv[]) { intFD; CharBuffer[buffer_size]; if(ARGC! =2) {fprintf (stderr,"usage:%s outfilename\n\a", argv[0]); Exit (Exit_failure); } //Open redirect File if(FD = open (argv[1], o_wronly| O_creat| O_trunc, s_irusr| S_IWUSR)) = =-1) {fprintf (stderr,"Open%s error:%s\n\a", argv[1], strerror (errno)); Exit (Exit_failure); } if(Dup2 (FD, Fileno (stdout)) = =-1) {fprintf (stderr,"Redirect Standard out error:%s\n\a", Strerror (errno)); \a is a bell character, a drop of exit (Exit_failure) will appear; } fprintf (stderr,"Now , please input string"); fprintf (stderr,"(to quit use ctrl+d\n)"); while(1) {fgets (buffer, buffer_size, stdin); if(feof (stdin)) Break; Write (Fileno (stdout), buffer, strlen (buffer)); } exit (exit_success);}
Implementing Who|sort with redirects
The parent process opens two sub-processes, pointing to who,sort and redirecting the corresponding end, closing the connection to the other end.
The parent process closes the connection in two directions of the pipeline, waiting for the child process to exit.
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>#include<fcntl.h>#include<stdlib.h>intMainintargcChar*argv[]) { intfds[2]; ifPipe (FDS) = =-1) {perror ("Pipe"); Exit (Exit_failure); } if(fork () = =0)//child processes that perform sort { Charbuf[ -]; Dup2 (fds[0],0);//REDIRECT standard inputClose (fds[1]); EXECLP ("Sort","Sort", (Char*)0); } Else { if(fork () = =0) {dup2 (fds[1],1);//REDIRECT standard outputClose (fds[0]); EXECLP ("W.H.O.","W.H.O.",(Char*)0); } Else{Close (fds[0]); Close (fds[1]); Wait (NULL); //wait for the child process to exitWait (NULL); } } return 0;}
Stream redirection
Stream redirection can be implemented in POSIX2 via function Popen (), Pclose ()
file *popen (__const char * __command, __const char * __modes): Creates a child process and executes the program indicated in the first parameter in the child process, returning a file pointer. The second parameter represents the I/O mode.
- If output redirection, you need to set the second parameter to "R" permission, which can be read by the process
- If you are entering a redirect, you need to set the second parameter to "W", which can be written by the process
int Pclose (FILE * __stream) : Close the corresponding Stream object
echo Test|cat with Flow redirection
#include <stdio.h>#include<unistd.h>#include<limits.h>#include<fcntl.h>#include<stdlib.h>intMainintargcChar*argv[]) {FILE* Finput, *Foutput; CharBuffer[pipe_buf]; intN; Finput= Popen ("Echo test!","R"); Connect the output of the echo Test command to the read end Foutput= Popen ("Cat","W"); Connect the cat's command input to the write end read (Fileno (finput), buffer, strlen ("test!")); Read the output of ECHO test to buf write (Fileno (foutput), Buffer,strlen ("Test")); Read the contents of the pipe as Cat input pclose (finput); Pclose (Foutput); printf ("\ n"); Exit (exit_success);}
"Linux Advanced Programming" (Chapter Nineth) interprocess communication-Pipeline 2