dup,dup2 function "Go"

Source: Internet
Author: User

Transferred from: http://eriol.iteye.com/blog/1180624

Transferred from: http://www.cnblogs.com/jht/archive/2006/04/04/366086.html

DUP and dup2 are also two very useful calls that are used to copy the descriptor of a file. They are often used to redirect stdin, stdout, and stderr of processes. The prototypes of these two functions are as follows:

C code
    1. #include <unistd.h>
    2. int dup ( int oldfd);
    3. int dup2 ( int OLDFD, int targetfd);

DUP () function

With the function DUP, we can copy a descriptor. Passed to the function an existing descriptor, it will return a new descriptor, the new descriptor is a copy of the descriptor passed to it. This means that both descriptors share the same data structure. For example, if we perform a lseek operation on a file descriptor, the location of the first file is the same as the second one. Here is a snippet of code to illustrate how the DUP function is used:

C code
    1. int fd1, FD2;
    2. ...
    3. FD2 = DUP (FD1);

It is important to note that we can create a descriptor before calling fork, which is the same as invoking the DUP to create a descriptor, and the child process will also receive a copy of the descriptor.

Dup2 () function

The DUP2 function is similar to the DUP function, but the DUP2 function allows the caller to specify the ID of a valid descriptor and a target descriptor. When the DUP2 function returns successfully, the target descriptor (the second argument of the Dup2 function) becomes a copy of the source descriptor (the first parameter of the DUP2 function), in other words, the two file descriptor now points to the same file and is the file to which the function's first argument points. Let's illustrate this in a piece of code:

C code
    1. int OLDFD;
    2. OLDFD = open ("App_log", (O_rdwr |  o_create), 0644);
    3. Dup2 (OLDFD, 1);
    4. Close (OLDFD);

In this example, we open a new file called "App_log" and receive a file descriptor called FD1. We call the DUP2 function with the arguments OLDFD and 1, which causes the file descriptor represented by 1 to be replaced with our newly opened file descriptor (that is, stdout, because the standard output file has an ID of 1). Anything written to StdOut will now be written to a file named "App_log". Note that the DUP2 function immediately closes the OLDFD after it has been copied, but does not close the newly opened file descriptor because the file Descriptor 1 now also points to it.

Example

Let's introduce a more in-depth example code. Recalling the command line pipeline, we can connect the standard output of the Ls–l command to the WC–L command as standard input. Next, we will use a C program to illustrate the implementation of this process. The code is shown below.

C code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int main ()
  5. {
  6. int pfds[2];
  7. if (pipe (pfds) = = 0) {
  8. if (fork () = = 0) {
  9. Close (1);
  10. Dup2 (Pfds[1], 1);
  11. Close (Pfds[0]);
  12. EXECLP ( "ls", "ls", "-l", NULL);
  13. } Else {
  14. Close (0);
  15. Dup2 (Pfds[0], 0);
  16. Close (pfds[1]);
  17. EXECLP ( "WC", "WC", "- L", NULL);
  18. }
  19. return 0;
  20. }

In the sample code, you first establish a pipeline in line 9th, and then divide the application into two processes: a child process (13–16) and a parent process (line 20–23). Next, the STDOUT descriptor (line 13th) is closed first in the subprocess, and then the LS–L command function is provided, but it is not written to stdout (line 13th), but rather to the output of the pipeline we created, which is done via the DUP2 function to redirect. In line 14th, use the Dup2 function to redirect stdout to the pipeline (Pfds[1]). After that, turn off the input of the pipe immediately. Then, using the EXECLP function to replace the image of the process with the process image of the command ls–l, any output of it will be sent to the input side of the pipeline once the command executes.

Now let's look at the receiving end of the pipe. As you can see from the code, the receiving side of the pipeline is assumed by the parent process. First close the stdin descriptor (line 20th), because we do not receive data input from standard device files such as the machine's keyboard, but instead receive data from the output of other programs. Then, once again, using the DUP2 function (line 21st), let the input of the pipeline be input, which is achieved by redirecting the file descriptor 0 (that is, the regular stdin) to Pfds[0]. Close the stdout end of the pipe (pfds[1]) because it is not available here. Finally, using the EXECLP function to replace the image of the parent process with the process image of the command wc-l, the command wc-l the contents of the pipeline as its input (line 23rd).

The function of these two functions is to redirect the output
The header file that defines the two functions is unistd.h, and it is interesting to see for yourself what this header file contains

To mention, this header file defines the following three constants

      • STDERR_FILENO= 2 Standard Error output
      • STDIN_FILENO= 0 Standard Input
      • STDOUT_FILENO= 1 Standard Output

Brothers Learn how to use network programming when 0,1,2 these parameters, you need to know what the rep means.

To say the meaning of these two functions, or to see a specific code

int FD, FD2;
mode_t Fd_mode = s_irusr| s_iwusr| s_irgrp| S_iroth;

void Redir_stdout (const char *filename)
{
Fd2=dup (Stdout_fileno);
FD = open (filename, o_wronly|  O_creat, Fd_mode); Open File operation
Dup2 (FD, Stdout_fileno); Redirect output to the file identified by FD
Close (FD);
}  


Fd2=dup (Stdout_fileno); Description FD2 represents the standard output
If we want to redirect the output just to FD and redirect back to standard output, we can do this with the following code:

void Resume_stdout ()//recovery output, directing standard output to FD2,FD2 represents standard output
{
Dup2 (FD2, Stdout_fileno);
Close (FD2);

dup,dup2 function "Go"

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.