Linux DUP and dup2 function parsing

Source: Internet
Author: User

1. File descriptor in the kernel data structure
Before I go into specific dup/dup2, I think it's necessary to look at the file descriptor's pattern in the kernel. During the existence of a process, some files will be opened, which will return some file descriptors, run a process from the shell, 3 file descriptors exist by default (0, 1, 2), 0 is associated with the standard input of the process, 1 is associated with the standard output of the process, 2 is associated with the standard error output of the process, What open file descriptors a process currently has can be viewed through the/proc/process id/fd directory. Can clearly explain the problem:
Process Table Entry

————————————————

FD Flag File pointer

_____________________

FD 0:|________|____________|------------> File Table

FD 1:|________|____________|

FD 2:|________|____________|

FD 3:|________|____________|

| .......         |

|_____________________|

Figure 1

The file table contains: File status flag, current file offset, v node pointer, these are not the focus of this article, we only need to know that each open file descriptor (FD flag) in the process table has its own file table entries, by the file pointer.
2. dup/dup2 function
Both the Apue and man documents have a concise statement of what these two functions do: Copying an existing file descriptor.
#include <unistd.h>
int dup (int oldfd);
int dup2 (int oldfd, int newfd);
Analyzing this process from the diagram, when invoking the DUP function, the kernel creates a new file descriptor in the process that is the smallest numeric value of the currently available file descriptor, which points to the file table entries owned by OLDFD.
Process Table Entry

————————————————

FD Flag File pointer

_____________________

FD 0:|________|____________| ______

FD 1:|________|____________|----------------> | |

FD 2:|________|____________| | File Table |

FD 3:|________|____________|----------------> |______|

| .......         |

|_____________________|

Figure 2: After invoking the DUP
2, if the value of OLDFD is 1 and the current file descriptor has a minimum value of 3, then the new descriptor 3 points to the file table entry owned by descriptor 1.
The difference between dup2 and DUP is that the value of the new descriptor can be specified with the NEWFD parameter, and if NEWFD is already open, it is closed first. If NEWFD equals OLDFD, DUP2 returns NEWFD without closing it. The new file descriptor returned by the DUP2 function also shares the same file table entry as the parameter OLDFD.
Apue illustrates this problem in a different way:
In fact, calling DUP (OLDFD) is equivalent to
Fcntl (OLDFD, F_DUPFD, 0)
Instead, calling Dup2 (OLDFD, NEWFD) is equivalent to
Close (OLDFD);
Fcntl (OLDFD, F_DUPFD, NEWFD);

examples: Both DUP and dup2 can be used to copy an existing file descriptor. Often used to redirect the process of STDIN, STDOUT, STDERR. The DUP function of the DUP function is defined in<unistd.h>, the prototype of the function is:intDUP (intfiledes); The function returns a new descriptor, which is a copy of the descriptor passed to it, and returns if an error occurs-1. The new file descriptor returned by the DUP must be the smallest value in the currently available file descriptor. The new file descriptor returned by this function shares the same file data structure as the parameter filedes. Example of the DUP function: [[email protected] dup]$ Lsdup.c[[email protected] dup]$ cat dup.c/********************************************************************************* * Copyright: (C) fulinux& Lt [Email protected]> * All rights reserved.         * * FILENAME:DUP.C * description:this file * * version:1.0.0 (07/31/2013~) *                  Author:fulinux <[email protected]> * changelog:1, Release initial version on "07/31/2013 04:00:06 PM" * ********************************************************************************/#include<stdio.h>#include<unistd.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    intFD = open ("Hello", o_creat| o_rdwr| O_trunc, s_irusr|s_iwusr); if(FD <0) {printf ("Open error!! \ n"); return 0; }    intNFD =DUP (FD); if(NFD <0) {printf ("error!! \ n"); return 0; }    Charbuf[ +]; intN;  while(n = Read (Stdin_fileno, buf, +)) >0)    {        if(Write (NFD, buf, n)! =N) {printf ("Write error!! \ n"); return 0; }    }    return 0;} In the above code, NFD copies the FD, so write (NFD, BUF, N) writes to the file that the NFD represents, which is the file that the FD represents. You can see the output in the appropriate directory's Hello file after the program finishes executing. [Email protected] dup]$ gcc dup.c [[email protected] dup]$ LSA. outDup.c[[email protected] dup]$./A. outHello World^C[[email protected] dup]$ LSA. outdup.c Hello[[email protected] dup]$ cat Hello Hello world[[email protected] dup]$ dup2 function dup2 function defined in<unistd.h>, the prototype of the function is:intDup2 (intFiledes,intfiledes2) Similarly, the function returns a new file descriptor and returns if an error occurs-1. Unlike the DUP, dup2 can specify the value of the new descriptor with the Filedes2 parameter. If the filedes2 is already open, turn it off first. If Filedes equals Filedes2, DUP2 returns FILEDES2 without closing it. Similarly, the returned new file descriptor shares the same file data structure as the parameter filedes. dup2 Function Example: [[email protected] dup2]$ Lsdup2.c[[email protected] dup2]$ cat dup2.c/********************************************************************************* * Copyright: (C) fulinux& Lt [Email protected]> * All rights reserved.         * * FILENAME:DUP2.C * description:this file * * version:1.0.0 (07/31/2013~) *                  Author:fulinux <[email protected]> * changelog:1, Release initial version on "07/31/2013 08:22:19 PM" * ********************************************************************************/#include<stdio.h>#include<unistd.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    intFD = open ("Hello.file", o_creat| o_rdwr| o_trunc,s_irusr|s_iwusr); if(FD <0) {printf ("Open error!! \ n"); return 0; }    intNFD =dup2 (FD, Stdout_fileno); if(NFD <0) {printf ("error!! \ n"); return 0; }    Charbuf[5]; intN;  while(n = Read (Stdin_fileno, buf,5)) >0)        if(Write (NFD, buf, n)! =N) {printf ("Write error!! \ n"); return 0; }    return 0;} The example above uses DUP2 to redirect the standard output to the Hello.file file as follows: [[email protected] dup2]$ lsdup2.c[[email protected] dup2]$ gcc dup2.c [email Protected] dup2]$./A. outHello World^C[[email protected] dup2]$ cat hello.file Hello world[[email protected] dup2]$

Transferred from: http://blog.csdn.net/fulinus/article/details/9669177

Linux DUP and dup2 function parsing

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.