dup, dup2 - 复制一个文件描述符
内容简介
#includeint dup(int oldfd) int dup2(int oldfd, int newfd)
描述
dup() 和%uA0dup2() 创建文件描述符的副本%uA0oldfd.
After a successful return from%uA0dup() or%uA0dup2(),the old and new file descriptors may be used interchangeably. They refer to the same open file description (see%uA0open(2)) and thus share file offset and file status flags for example, if the file offset is modified by using%uA0lseek(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%uA0fcntl(2)) for the duplicate descriptor is off.
dup() 使用编号最小的未用描述符的新的描述符。
dup2()%uA0使得newfd是oldfd副本,先关闭newfd,如果必要的话。
返回值
dup() and%uA0dup2() return the new descriptor, or -1 if an error occurred (in which case,errno%uA0is set appropriately).%uA0
错误
标签 | 描述 |
---|---|
EBADF | oldfd%uA0isn’t an open file descriptor, or%uA0newfd%uA0is out of the allowed range for file descriptors. |
EBUSY | (Linux only) This may be returned by%uA0dup2() during a race condition with%uA0open() and%uA0dup(). |
EINTR | The%uA0dup2() call was interrupted by a signal. |
EMFILE | The process already has the maximum number of file descriptors open and tried to open a new one. |
警告
The error returned by%uA0dup2() is different from that returned by%uA0fcntl(...,%uA0F_DUPFD, ...)when%uA0newfd%uA0is out of range. On some systems%uA0dup2() also sometimes returns%uA0EINVALlike%uA0F_DUPFD.
If%uA0newfd%uA0was open, any errors that would have been reported at%uA0close() time, are lost. A careful programmer will not use%uA0dup2() without closing%uA0newfd%uA0first.
遵循于
SVr4, 4.3BSD, POSIX.1-2001.