( standard c libraries translation )dup

dup, dup2, dup3 - duplicate a file descriptor
dup, dup2, dup3 - 复制一个文件描述符

所需头文件
#include <unistd.h>

int dup(int oldfd);
int dup2(int oldfd, int newfd);

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

int dup3(int oldfd, int newfd, int flags);


These system calls create a copy of the file descriptor oldfd.
这些系统调用创建文件描述符oldfd的一个副本

dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup使用最小未使用的描述符作为新的描述符

dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:
*  If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
*  If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
dup2把newfd作为oldfd的副本,如果必要需要先关掉newfd,但是需要注意如下:
如果oldfd不是一个可用的文件描述符,调用将失败,但是newfd不关闭
如果oldfd是一个可用的文件描述符,newfd跟oldfd的值相同,newfd不被关闭

After  a  successful  return from one of these system calls, 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.
当上面的系统调用返回成功的时候,新的描述符和旧的描述符可以交换的使用,他们涉及相同的打开文件描述符,因此共享文件偏移量和文件状态符,例如,如果一个文件描述符的文件偏移量被lseek改变了,那么另一个文件描述符的偏移量也会被改变

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.
两个文件描述符不共享文件描述符标志(close-on-exec标志),close-on-exec标志标志对于描述符副本来说是关闭的

dup3() is the same as dup2(), except that:
*  The caller can force the close-on-exec flag to be set for the new file descriptor by specifying O_CLOEXEC in flags.  See the description of the same flag in open(2) for reasons why this may be useful.
*  If oldfd equals newfd, then dup3() fails with the error EINVAL.
dup3跟dup2一样,除了:
调用者可以通过定义O_CLOEXEC符号强制设置close-on-exec标志给新的文件描述符,查看open中该标志的定义来找到为什么这个会生效

On success, these system calls return the new descriptor.  On error, -1 is returned, and errno is set appropriately.
成功的时候,系统调用将返回新的文件描述符,失败的时候返回-1,errno被设置成合适的值

EBADF  oldfd isn't an open file descriptor, or newfd is out of the allowed range for file descriptors.
oldfd不是一个打开的文件苗师傅,或者newfd的值超出了文件描述符的范围
EBUSY  (Linux only) This may be returned by dup2() or dup3() during a race condition with open(2) and dup().
跟open和dup有竞争关系的时候dup2和dup3可能会返回这个
EINTR  The dup2() or dup3() call was interrupted by a signal; see signal(7).
dup2和dup3被信号中断
EINVAL (dup3()) flags contain an invalid value.  Or, oldfd was equal to newfd.
标志中包含非法值,或者oldfd和newfd相等
EMFILE The process already has the maximum number of file descriptors open and tried to open a new one.
进程已经打开了最多的文件描述符,但是依然试图打开一个新的

dup3() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9.
dup3在linux 2.6.27中被加入,glibc在2.9版本提供

dup(), dup2(): SVr4, 4.3BSD, POSIX.1-2001. dup3() is Linux-specific.
dup和dup2在SVR4,4.3BSD,POSIX.1-2001中定义,dup3是linux定义的

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 EINVAL like F_DUPFD.
dup2返回的错误跟fcntl返回的错误不一样,当newfd超出范围的时候,一些系统的dup2有时候会返回EINVAL,类似与F_DUPFD

If newfd was open, any errors that would have been reported at close(2) time are lost.  A careful programmer will not  use  dup2()  or  dup3()  without closing newfd first.
如果newfd是打开的,任何被close记录的错误都会被丢失,一个谨慎的程序员在没有关闭newfd之前不会使用dup2或者dup3


testcase如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
	int fd, newfd;
	int fd2 = 100;
	ssize_t num;

	if ((fd = open("hello.txt", O_CREAT|O_WRONLY|O_TRUNC)) == -1) {
		fprintf(stderr, "open error: %s\n", strerror(errno));
		return -1;
	}
	num = write(fd, "hello", 5);
	if (num == -1) {
		fprintf(stderr, "write error: %s\n", strerror(errno));
		return -1;
	} else if (num != 5) {
		printf("write not completed\n");
		return -1;
	}

	newfd = dup(fd);
	dup2(fd, fd2);
	
	fsync(fd);
	close(fd);

	num = write(newfd, " world", 6);
	fsync(newfd);
	close(newfd);

	num = write(fd2, "\n", 1);
	fsync(fd2);
	close(fd2);
	return 0;
}

其实就是写一个hello world到hello.txt,
O_CREAT|O_WRONLY|O_TRUNC
这三个flag组合起来使用跟creat的作用是相同的,不管open的文件是否存在,都将trunc成空文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值