dup2函数

dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符。它们经常用来重定向进程的stdin、stdout和stderr。

    dup2函数跟dup函数相似,但dup2函数允许调用者规定一个有效描述符和目标描述符的id。dup2函数成功返回时,目标描述符(dup2函数的第二个参数)将变成源描述符(dup2函数的第一个参数)的复制品,换句话说,两个文件描述符现在都指向同一个文件,并且是函数第一个参数指向的文件。下面我们用一段代码加以说明:


DUP(2)                                                                                 Linux Programmer's Manual                                                                                 DUP(2)

NAME
       dup, dup2, dup3 - duplicate a file descriptor

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

DESCRIPTION
       These system calls create a copy of the file descriptor oldfd.

       dup() uses the lowest-numbered unused descriptor for the new descriptor.

       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.

       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.

       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.

       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.

RETURN VALUE
       On success, these system calls return the new descriptor.  On error, -1 is returned, and errno is set appropriately.



main.c


int oldfd;
oldfd = open("app_log", (O_RDWR | O_CREATE), 0644 ); 

dup2( oldfd, 1 ); 

close( oldfd );  


 

    在本例中,我们打开了一个新文件,称为“app_log”,并收到一个文件描述符,该描述符叫做fd1。我们调用dup2函数,参数为oldfd和1,这会导致用我们新打开的文件描述符替换掉由1代表的文件描述符(即stdout,因为标准输出文件的id为1)。任何写到stdout的东西,现在都将改为写入名为“app_log”的文件中。需要注意的是,dup2函数在复制了oldfd之后,会立即将其关闭,但不会关掉新近打开的文件描述符,因为文件描述符1现在也指向它。


    #include <sys/stat.h>  
    #include <string.h>  
    #include <fcntl.h>  
    #include <stdio.h>  
    #include <unistd.h>  
    int main(void)  
    {
       int nul, oldstdout;  
       char msg[] = "This is a test";  
       //打开一个文件,操作者具有读写权限 如果文件不存在就创建  
       nul = open("DUMMY.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);  
       /* create a duplicate handle for standard 
          output */  
       oldstdout = dup(STDOUT);  
       /* 
          redirect standard output to DUMMY.FIL 
          by duplicating the file handle onto the 
          file handle for standard output. 
       */  
       dup2(nul, STDOUT_FILENO);  
       /* close the handle for DUMMY.FIL */  
       close(nul);  
       /* will be redirected into DUMMY.FIL */  
       write(STDOUT_FILENO, msg, strlen(msg));  
       /* restore original standard output 
          handle */  
       dup2(oldstdout, STDOUT_FILENO);  
       /* close duplicate handle for STDOUT_FILENO */  
       close(oldstdout);  
       return 0;  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值