DUP and DUP2 functions

Source: Internet
Author: User

The following two functions can be used to copy an existing file descriptor:

#include <unistd.h>int dup (int filedes), int dup2 (int filedes,int filedes2);                                                                                                 return value for two functions: Returns a new file descriptor if successful, or 1 if an error occurs

The new file descriptor returned by the DUP must be the minimum value in the currently available file descriptor. With dup2, you can specify the value of the new descriptor with the Filedes2 parameter. If the filedes2 is already open, it is now closed. If Filedes equals Filedes2, DUP2 returns FILEDES2 without closing it.

The new file descriptors returned by these functions share the same file table entry as the parameter Filedes . , we assume that the following is done:

Newfd=dup (1);

When this function starts executing, it is assumed that the next available descriptor is 3 (which is very likely because 0,1 and 2 are opened by the shell). Because two descriptors point to the same file table entry, they share the same file status flag (read, write, add, and so on) along with the same current file offset.

Each file descriptor has its own set of file descriptor flags.

Another way to copy a descriptor is to use the FCNTL function, which, in fact, can be called:

DUP (filedes);

is equivalent to

Fcntl (filedes,f_dupfd,0);

and call

Dup2 (Filedes,filedes2);

is equivalent to

Close (Filedes2);

Fcntl (Filedes,f_dupfd,filedes2);

In the latter case, dup2 is not exactly equivalent to close plus fcntl. The difference between them is:

1) dup2 is an atomic operation, and close and fcntl include two function calls, and it is possible to insert the Execute signal capture function between close and fcntl, which may modify the file descriptor.

2) dup2 and fcntl have some different errno.

The emphasis explains two places:

    • The 3rd picture, to execute dup2(fd, 1); , the file descriptor 1 originally pointed tty , now to point to the new file somefile , the original closed, but tty the file originally has two reference count, and the file descriptor save_fd also point to it, so just the reference count minus 1, Does not really close the file.

    • The 5th picture, to execute dup2(save_fd, 1); , the file descriptor 1 originally pointed somefile , now to point to the new file tty , the original closed, somefile originally only a reference count, so this time reduced to 0, is really closed.

the 1.dup () and dup2 () functions are formatted as follows:

#include <unistd.h>
int dup (int oldfd);
int dup2 (int oldfd, int newfd);

DUP () uses the lowest-numbered unused descriptor for the new descriptor.
dup2 () makes NEWFD is the copy of OLDFD, closing NEWFD first ifnecessary, but note the following:
*if OLDFD is not a valid file descriptor, then the call fails, and NEWFD are not closed.
*if OLDFD is a valid file descriptor, and NEWFD have the same value as OLDFD, then dup2 () does nothing, and RET Urns NEWFD.

1) We can use these two functions to copy the file descriptor.
2) where OLDFD and NEWFD are the pre-copy file descriptors and the copied file descriptors respectively.
3) calls to both functions copy the file descriptor OLDFD, and their return values are new file descriptors.
4) The difference is that the return value of the DUP () is the smallest unused file descriptor; The return value of Dup2 () is the pre-developed file descriptor NEWFD.
5) for dup2 (), if the file descriptor NEWFD is being used, the NEWFD is closed first, and if NEWFD and OLDFD are not closed, the file is returned normally.
PS: This is my own classification of the words in the book.


2. The first thing to understand, but also to understand the file descriptor clear:
Relationship: Process---(owned)---> (several) file descriptors ()---(corresponding)---> Files
|---> Files sec-character (0)
|---> File descriptors (1)---> Files (1)
a process (n)--|---> File descriptor (2)
|---> File descriptors (3)---> Files (3)
                            |.
                            |.
                            |.
|---> File descriptor (1023)---> File (...)
PS1: A file opened by an open () function can have a number of descriptors associated with it;
each process in Ps2:linux can have 1024 file descriptors;
PS3: The first three bits of the file descriptor 0, 1, 2 correspond respectively:
Stdin_fileno 0 Standard input file
Stdout_fileno 1 Standard output file
Stderr_fileno 2 standard error output file
so it's good to start understanding how the DUP (int oldfd) and dup2 (int oldfd, int newfd) functions work:
DUP () better understand:
The system assigns a new, unused, least-valued file descriptor to the file that the parameter oldfd within the DUP () function points to and returns the value.
dup2 () more difficult to understand:
1) dup2 () first see if OLDFD is a valid file descriptor, if not, the call fails, and the NEWFD file descriptor is not closed;
2) If OLDFD is a valid file descriptor, detects if the NEWFD is used, if used, turns it off and points newfd to the file that OLDFD points to and returns NEWFD;
3) If NEWFD is the same as OLDFD, the file is returned normally without closing.

DUP and DUP2 functions

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.