PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT | |
DUP(3P) POSIX Programmer's ManualDUP(3P)This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
dup, dup2 — duplicate an open file descriptor
#include <unistd.h> int dup(intfildes); int dup2(intfildes, intfildes2);
Thedup() function provides an alternative interface to the service provided byfcntl() using the F_DUPFD command. The calldup(fildes) shall be equivalent to: fcntl(fildes, F_DUPFD, 0); Thedup2() function shall cause the file descriptorfildes2 to refer to the same open file description as the file descriptorfildes and to share any locks, and shall returnfildes2. Iffildes2 is already a valid open file descriptor, it shall be closed first, unlessfildes is equal tofildes2 in which casedup2() shall returnfildes2 without closing it. If the close operation fails to closefildes2,dup2() shall return -1 without changing the open file description to whichfildes2 refers. Iffildes is not a valid file descriptor,dup2() shall return -1 and shall not closefildes2. Iffildes2 is less than 0 or greater than or equal to {OPEN_MAX},dup2() shall return -1 witherrno set to[EBADF]. Upon successful completion, iffildes is not equal tofildes2, the FD_CLOEXEC flag associated withfildes2 shall be cleared. Iffildes is equal tofildes2, the FD_CLOEXEC flag associated withfildes2 shall not be changed. Iffildes refers to a typed memory object, the result of thedup2() function is unspecified.Upon successful completion a non-negative integer, namely the file descriptor, shall be returned; otherwise, -1 shall be returned anderrno set to indicate the error.
Thedup() function shall fail if:EBADFThefildes argument is not a valid open file descriptor.EMFILEAll file descriptors available to the process are currently open. Thedup2() function shall fail if:EBADFThefildes argument is not a valid open file descriptor or the argumentfildes2 is negative or greater than or equal to {OPEN_MAX}.EINTRThedup2() function was interrupted by a signal. Thedup2() function may fail if:EIOAn I/O error occurred while attempting to closefildes2.The following sections are informative.Redirecting Standard Output to a File S The following example closes standard output for the current processes, re-assigns standard output to go to the file referenced bypfd, and closes the original file descriptor to clean up. #include <unistd.h> ... int pfd; ... close(1); dup(pfd); close(pfd); ...Redirecting Error Messages The following example redirects messages fromstderr tostdout. #include <unistd.h> ... dup2(1, 2); ...
Implementations may use file descriptors that must be inherited into child processes for the child process to remain conforming, such as for message catalog or tracing purposes. Therefore, an application that callsdup2() with an arbitrary integer forfildes2 risks non-conforming behavior, anddup2() can only portably be used to overwrite file descriptor values that the application has obtained through explicit actions, or for the three file descriptors corresponding to the standard file streams. In order to avoid a race condition of leaking an unintended file descriptor into a child process, an application should consider opening all file descriptors with the FD_CLOEXEC bit set unless the file descriptor is intended to be inherited acrossexec.
Thedup() function is redundant. Its services are also provided by thefcntl() function. It has been included in this volume of POSIX.1‐2017 primarily for historical reasons, since many existing applications use it. On the other hand, thedup2() function provides unique services, as no other interface is able to atomically replace an existing file descriptor. Thedup2() function is not marked obsolescent because it presents a type-safe version of functionality provided in a type-unsafe version byfcntl(). It is used in the POSIX Ada binding. Thedup2() function is not intended for use in critical regions as a synchronization mechanism. In the description of[EBADF], the case offildes being out of range is covered by the given case offildes not being valid. The descriptions forfildes andfildes2 are different because the only kind of invalidity that is relevant forfildes2 is whether it is out of range; that is, it does not matter whetherfildes2 refers to an open file when thedup2() call is made.
None.
close(3p),fcntl(3p),open(3p) The Base Definitions volume of POSIX.1‐2017,unistd.h(0p)
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online athttp://www.opengroup.org/unix/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group 2017DUP(3P)Pages that refer to this page:unistd.h(0p), sh(1p), close(3p), fstatvfs(3p), open(3p), posix_spawn(3p), posix_spawn_file_actions_addclose(3p), posix_spawn_file_actions_adddup2(3p), posix_typed_mem_open(3p), shm_open(3p), write(3p)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |