Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

dup2(2) [hpux man page]

dup2(2) 							System Calls Manual							   dup2(2)

NAME
dup2 - duplicate an open file descriptor to a specific slot SYNOPSIS
DESCRIPTION
fildes is a file descriptor obtained from a or system call. fildes2 is a non-negative integer less than the maximum value allowed for file descriptors. causes fildes2 to refer to the same file as fildes. If fildes2 refers to an already open file, the open file is closed first. The file descriptor returned by has the following in common with fildes: o Same open file (or pipe). o Same file pointer (that is, both file descriptors share one file pointer.) o Same access mode (read, write or read/write). o Same file status flags (see fcntl(2), The new file descriptor is set to remain open across system calls. See fcntl(2). This routine is found in the C library. Programs using but not using other routines from the Berkeley importability library (such as the routines described in bsdproc(3C)) should not give the option to ld(1). RETURN VALUE
Upon successful completion, returns the new file descriptor as a non-negative integer, fildes2. Otherwise, it returns -1 and sets to indi- cate the error. ERRORS
fails if the following is true: fildes is not a valid open file descriptor or fildes2 is not in the range of legal file descriptors. An attempt to close fildes2 was interrupted by a signal. The file is still open. WARNINGS
A multithreaded application or an application with a signal handler can exhibit a race between on one thread and another kernel call that assigns a new file descriptor while running on a second thread or signal handler. If fildes2 is free before the call was made, the other thread or signal handler may win the race and acquire that descriptor (e.g., in an call). The thread calling can then close this file and reuse the descriptor. This results in multiple functions improperly referring to the same file. This race can be avoided either by ensuring that fildes2 references an open file before calling or by providing user-level synchronization (or signal disabling) which makes sure that the thread and another thread or signal handler don't make competing calls into the kernel at the same time. SEE ALSO
close(2), creat(2), dup(2), exec(2), fcntl(2), open(2), pipe(2), privileges(5). STANDARDS CONFORMANCE
dup2(2)

Check Out this Related Man Page

dup2(3C)						   Standard C Library Functions 						  dup2(3C)

NAME
dup2 - duplicate an open file descriptor SYNOPSIS
#include <unistd.h> int dup2(int fildes, int fildes2); DESCRIPTION
The dup2() function causes the file descriptor fildes2 to refer to the same file as fildes. The fildes argument is a file descriptor refer- ring to an open file, and fildes2 is a non-negative integer less than the current value for the maximum number of open file descriptors allowed the calling process. See getrlimit(2). If fildes2 already refers to an open file, not fildes, it is closed first. If fildes2 refers to fildes, or if fildes is not a valid open file descriptor, fildes2 will not be closed first. The dup2() function is equivalent to fcntl(fildes, F_DUP2FD, fildes2). RETURN VALUES
Upon successful completion a non-negative integer representing the file descriptor is returned. Otherwise, -1 is returned and errno is set to indicate the error. ERRORS
The dup2() function will fail if: EBADF The fildes argument is not a valid open file descriptor. EBADF The files2 argument is negative or is not less than the current resource limit returned by getrlimit(RLIMIT_NOFILE, ...). EINTR A signal was caught during the dup2() call. EMFILE The process has too many open files. See fcntl(2). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
close(2), creat(2), exec(2), fcntl(2), getrlimit(2), open(2), pipe(2), lockf(3C), attributes(5), standards(5) SunOS 5.10 19 Dec 2003 dup2(3C)
Man Page