dup() and
dup2() create a copy of the file descriptor
oldfd.
After a successful return from dup() or dup2(),
the old and new file descriptors may be used interchangeably.
They refer to the same open file description (see
open(2))
and thus share file offset and file status flags;
for example, if the file offset is modified by using
lseek(2)
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
(FD_CLOEXEC; see
fcntl(2))
for the duplicate descriptor is off.
dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2()
makes newfd be the copy of oldfd, closing newfd first if necessary.