|
|
HP-UX Reference > Ddup2(2)HP-UX 11i Version 2: December 2007 Update |
|
NAMEdup2() — duplicate an open file descriptor to a specific slot DESCRIPTIONfildes is a file descriptor obtained from a creat(), open(), dup(), fcntl(), or pipe() system call. fildes2 is a non-negative integer less than the maximum value allowed for file descriptors. dup2() 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 dup2() has the following in common with fildes:
The new file descriptor is set to remain open across exec() system calls. See fcntl(2). This routine is found in the C library. Programs using dup2() but not using other routines from the Berkeley importability library (such as the routines described in bsdproc(3C)) should not give the -lBSD option to ld(1). RETURN VALUEUpon successful completion, dup2() returns the new file descriptor as a non-negative integer, fildes2. Otherwise, it returns -1 and sets errno to indicate the error. ERRORSdup2() fails if the following is true:
WARNINGSA multithreaded application or an application with a signal handler can exhibit a race between dup2() 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 dup2() call was made, the other thread or signal handler may win the race and acquire that descriptor (e.g., in an open() call). The thread calling dup2() 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 dup2(), or by providing user-level synchronization (or signal disabling) which makes sure that the dup2() thread and another thread or signal handler don't make competing calls into the kernel at the same time. |
|