Section: Linux Programmer's Manual (2)
Return to Main Contents
dup, dup2, dup3 - duplicate a file descriptor
int dup(int oldfd);
int dup2(int oldfd, int newfd);
#define _GNU_SOURCE /* See feature_test_macros(7) */
int dup3(int oldfd, int newfd, int flags);
These system calls create a copy of the file descriptor
uses the lowest-numbered unused descriptor for the new descriptor.
makes newfd be the copy of oldfd, closing newfd
first if necessary, but note the following:
is not a valid file descriptor, then the call fails, and
is not closed.
is a valid file descriptor, and
has the same value as
does nothing, and returns
After a successful return from one of these system calls,
the old and new file descriptors may be used interchangeably.
They refer to the same open file description (see
and thus share file offset and file status flags;
for example, if the file offset is modified by using
on one of the descriptors, the offset is also changed for the other.
The two descriptors do not share file descriptor flags
(the close-on-exec flag).
The close-on-exec flag
for the duplicate descriptor is off.
is the same as
The caller can force the close-on-exec flag to be set
for the new file descriptor by specifying
See the description of the same flag in
for reasons why this may be useful.
fails with the error
On success, these system calls
return the new descriptor.
On error, -1 is returned, and
is set appropriately.
isn't an open file descriptor, or
is out of the allowed range for file descriptors.
(Linux only) This may be returned by
during a race condition with
call was interrupted by a signal; see
contain an invalid value.
was equal to
The process already has the maximum number of file
descriptors open and tried to open a new one.
was added to Linux in version 2.6.27;
glibc support is available starting with
SVr4, 4.3BSD, POSIX.1-2001.
The error returned by
is different from that returned by
fcntl(..., F_DUPFD, ...)
is out of range.
On some systems
also sometimes returns
was open, any errors that would have been reported at
time are lost.
A careful programmer will not use
This page is part of release 3.27 of the Linux
A description of the project,
and information about reporting bugs,
can be found at
- RETURN VALUE
- CONFORMING TO
- SEE ALSO
This document was created by
using the manual pages.
Time: 19:49:18 GMT, April 27, 2011