linux-C 重定向句柄操作(dup,duo2,dup3)详解及demo

dup类函数的作用

复制文件描述符

1.dup


int dup(int oldfd);

参数:
  • oldfd 被复制的文件描述符
返回值:
  • 成功后,将返回新的文件描述符。 如果出错,则返回-1,并正确设置errno。
详解

dup()系统调用生成文件描述符oldfd的一个副本,从未使用的文件描述符当中选取最小的文件描述符作为新的文件描述符。

当成功返回后,旧的文件描述符和新的文件描述符可以互换使用,这两个描述符指向了同一个打开的文件描述符(见系统调用open()),他们共享同一个文件偏移量和文件状态标志。比如说,当使用lseek操作某一个文件描述符,改变了该文件的偏移量的时候,使用另外一个描述符看到的是相同的偏移量。

这两个文件描述符不会共享文件描述符标志(例如 close-on-exec标志)。如果旧的文件描述符有close-on-exec标志,则新的文件描述符没有。

2.dup2

 #include <unistd.h>

int dup2(int oldfd, int newfd);

参数:
  • oldfd 被复制的文件描述符
  • newfd 被粘贴的文件描述符
返回值:
  • 成功后,返回新的文件描述符。 如果出错,则返回-1,并正确设置errno。
详解

dup2()系统调用完成和dup相同的功能。但是dup2并不将未使用的文件描述符的最小值作为新的文件描述符,它使用参数newfd作为新的文件描述符。如果newfd在之前已经打开了,会先将打开的文件关闭,在完成文件描述符的复制操作。

关闭newfd之前打开的文件和复制newfd新的文件描述符这个操作是原子的。这个操作的原子性至关重要,因为实现close()和dup()两个函数相同的功能涉及到竞争问题。也就是说在关闭了newfd打开的文件之后,将newfd复制为新的文件描述符之前,newfd有可能已经被重新定义成其他文件的文件描述符了。这种情况有可能会发生,比如说主程序被一个信号打断,或者在完成了close操作之后,又有一个并行执行的线程分配了这个文件描述符。

注意
  • 如果oldfd不是一个有效的文件描述符,此次调用失败,newfd打开的文件也不关闭
  • 如果oldfd是一个有效的文件描述符,并且newfd=oldfd,dup2什么也不做,直接返回newfd()

3.dup3

     #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);

参数:
  • oldfd 被复制的文件描述符
  • newfd 被粘贴的文件描述符
  • flag 标志
返回值:
  • 成功后,返回新的文件描述符。 如果出错,则返回-1,并正确设置errno。
详解

dup3实现和dup2相同的功能,但有以下差别:

调用这可以设定close-on-exec标志,这个操作可以通过指定flags参数里面的O_CLOEXEC。参考open系统调用里面相同的标志参数,那里面解释了为什么这样做是有效的。

如果oldfd等于newfd,dup3调用失败,伴随这一个错误EINVAL。

ERRORS
       EBADF  oldfd isn't an open file descriptor.

       EBADF  newfd  is out of the allowed range for file descriptors (see the
              discussion of RLIMIT_NOFILE in getrlimit(2)).

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

       EINTR  The  dup2() or dup3() call was interrupted by a signal; see sig‐
              nal(7).

       EINVAL (dup3()) flags contain an invalid value.

       EINVAL (dup3()) oldfd was equal to newfd.

       EMFILE The per-process limit on the number of open file descriptors has
              been  reached  (see  the  discussion  of  RLIMIT_NOFILE in getr‐
              limit(2)).

Demo

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define OPEN_FILE "./log"

int main()
{

	int fd = open(OPEN_FILE, O_RDWR | O_CREAT, 0666);
	if( fd < 0 ){
		perror("open");
		return 1;
	}
	printf("dup begin : %d\n",fd);
	//------dup()-----
	//close(1);              //需要手动关闭为文件描述符
	//dup(); uses  the lowest-numbered unused descriptor for the new descriptor.
	//int new_fd = dup(fd);
	
	//-----dup2()------
	int new_fd = dup2(fd, 1);//不许要手动关闭文件描述符号,两个参数分别为旧、新,把旧的描述符关闭,再把新的描述符复制到旧的上面。
	printf("dup end : new : %d, old : %d\n",new_fd,fd);
	int count = 10;
	while(count){
		printf("hello world ; %d\n",count);
		count--;
	}
	fflush(stdout);//重定向到文件就由行缓冲变成满缓冲,所以得刷新
	close(fd);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45309916/article/details/107708099