linux下dup函数,dup()函数 Unix/Linux

dup, dup2 - 复制一个文件描述符

内容简介

#include int dup(int oldfd);

int dup2(int oldfd, int newfd);

描述

dup() 和 dup2() 创建文件描述符的副本 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() 使用编号最小的未用描述符的新的描述符。

dup2() 使得newfd是oldfd副本,先关闭newfd,如果必要的话。

返回值

dup() anddup2() return the new descriptor, or -1 if an error occurred (in which case,errnois set appropriately).

错误

标签

描述

EBADF

oldfd isn’t an open file descriptor, or newfd is out of the allowed range for file descriptors.

EBUSY

(Linux only) This may be returned by dup2() during a race condition with open() and dup().

EINTR

The dup2() 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 dup2() is different from that returned by fcntl(..., F_DUPFD, ...)when newfd is out of range. On some systems dup2() also sometimes returns EINVALlike F_DUPFD.

If newfd was open, any errors that would have been reported at close() time, are lost. A careful programmer will not use dup2() without closing newfd first.

遵循于

SVr4, 4.3BSD, POSIX.1-2001.

另请参阅

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值