zoukankan      html  css  js  c++  java
  • linux之dup和dup2函数解析

    1. 文件描述符在内核中数据结构
    在具体说dup/dup2之前,我认为有必要先了解一下文件描述符在内核中的形态。一个进程在此存在期间,会有一些文件被打开,从而会返回一些文件描述符,从shell中运行一个进程,默认会有3个文件描述符存在(0、1、2), 0与进程的标准输入相关联,1与进程的标准输出相关联,2与进程的标准错误输出相关联,一个进程当前有哪些打开的文件描述符可以通过/proc/进程ID/fd目录查看。 下图可以清楚的说明问题:
    进程表项

    ————————————————

       fd标志 文件指针

           _____________________

    fd 0:|________|____________|------------> 文件表

    fd 1:|________|____________|

    fd 2:|________|____________|

    fd 3:|________|____________|

          |     .......         |

          |_____________________|

    图1

    文件表中包含:文件状态标志、当前文件偏移量、v节点指针,这些不是本文讨论的重点,我们只需要知道每个打开的文件描述符(fd标志)在进程表中都有自己的文件表项,由文件指针指向。
    2. dup/dup2函数
    APUE和man文档都用一句话简明的说出了这两个函数的作用:复制一个现存的文件描述符。
    #include <unistd.h>
    int dup(int oldfd);
    int dup2(int oldfd, int newfd);
    从图1来分析这个过程,当调用dup函数时,内核在进程中创建一个新的文件描述符,此描述符是当前可用文件描述符的最小数值,这个文件描述符指向oldfd所拥有的文件表项。
      进程表项

    ————————————————

       fd标志 文件指针

           _____________________

    fd 0:|________|____________|                   ______

    fd 1:|________|____________|----------------> |      |

    fd 2:|________|____________|                  |文件表|

    fd 3:|________|____________|----------------> |______|

          |     .......         |

          |_____________________|

                    图2:调用dup后的示意图
    如图2所示,假如oldfd的值为1,当前文件描述符的最小值为3,那么新描述符3指向描述符1所拥有的文件表项。
    dup2和dup的区别就是可以用newfd参数指定新描述符的数值,如果newfd已经打开,则先将其关闭。如果newfd等于oldfd,则dup2返回newfd, 而不关闭它。dup2函数返回的新文件描述符同样与参数oldfd共享同一文件表项。
    APUE用另外一个种方法说明了这个问题:
    实际上,调用dup(oldfd)等效于
    fcntl(oldfd, F_DUPFD, 0)
    而调用dup2(oldfd, newfd)等效于
    close(oldfd);
    fcntl(oldfd, F_DUPFD, newfd);

    实例:
    
    dup 和 dup2 都可以用来复制一个现存的文件描述符。经常用来重新定向进程的 STDIN, STDOUT, STDERR。
    
    dup 函数 
    dup 函数定义在 <unistd.h> 中,函数原形为:
    
    int dup ( int filedes ) ; 
    函数返回一个新的描述符,这个新的描述符是传给它的描述符的拷贝,若出错则返回 -1。由dup返回的新文件描述符一定是当前可用文件描述符中的最小数值。这函数返回的新文件描述符与参数 filedes 共享同一个文件数据结构。
    
    dup函数实例:
    [lingyun@localhost dup]$ ls
    dup.c
    [lingyun@localhost dup]$ cat dup.c 
    /*********************************************************************************
     *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
     *                  All rights reserved.
     *
     *       Filename:  dup.c
     *    Description:  This file 
     *                 
     *        Version:  1.0.0(07/31/2013~)
     *         Author:  fulinux <fulinux@sina.com>
     *      ChangeLog:  1, Release initial version on "07/31/2013 04:00:06 PM"
     *                 
     ********************************************************************************/
    
    
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    
    int main(int argc, char* argv[])
    {
        int fd = open("hello", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
        if(fd < 0)
        {
            printf("Open Error!!
    ");
            return 0;
        }
    
    
        int nfd = dup(fd);
        if(nfd < 0)
        {
            printf("Error!!
    ");
            return 0;
        }
    
    
        char buf[1000];
        int n;
    
    
        while((n = read(STDIN_FILENO, buf,1000)) > 0)
        {
            if(write(nfd, buf, n) != n)
            {
                printf("Write Error!!
    ");
                return 0;
            }
        }
        return 0;
    
    
    }
    
    
    
    上面代码中,nfd 拷贝了 fd,所以 write ( nfd, buf, n ) 这语句写到 nfd 所代表的文件时也就是写到 fd 所代表的文件。程序执行完后可以在相应的目录的hello文件中看到输出。
    [lingyun@localhost dup]$ gcc dup.c 
    [lingyun@localhost dup]$ ls
    a.out  dup.c
    [lingyun@localhost dup]$ ./a.out 
    hello world
    ^C
    [lingyun@localhost dup]$ ls
    a.out  dup.c  hello
    [lingyun@localhost dup]$ cat hello 
    hello world
    [lingyun@localhost dup]$ 
    
    
    
    
    dup2 函数 
    dup2 函数定义在 <unistd.h> 中,函数原形为:
    
    int dup2( int filedes, int filedes2 ) 
    同样,函数返回一个新的文件描述符,若出错则返回 -1。与 dup 不同的是,dup2 可以用 filedes2 参数指定新描述符的数值。如果 filedes2 已经打开,则先将其关闭。如若 filedes 等于 filedes2 , 则 dup2 返回 filedes2 , 而不关闭它。同样,返回的新文件描述符与参数 filedes 共享同一个文件数据结构。
    
    dup2函数实例:
    
    [lingyun@localhost dup2]$ ls
    dup2.c
    [lingyun@localhost dup2]$ cat dup2.c 
    /*********************************************************************************
     *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
     *                  All rights reserved.
     *
     *       Filename:  dup2.c
     *    Description:  This file 
     *                 
     *        Version:  1.0.0(07/31/2013~)
     *         Author:  fulinux <fulinux@sina.com>
     *      ChangeLog:  1, Release initial version on "07/31/2013 08:22:19 PM"
     *                 
     ********************************************************************************/
    
    
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    
    int main(int argc, char* argv[])
    {
        int fd = open("hello.file", O_CREAT|O_RDWR|O_TRUNC,S_IRUSR|S_IWUSR);
        if(fd < 0)
        {
            printf("Open Error!!
    ");
            return 0;
        }
    
    
        int nfd = dup2(fd, STDOUT_FILENO);
        if(nfd < 0)
        {
            printf("Error!!
    ");
            return 0;
        }
    
    
        char buf[5];
        int n;
    
    
        while((n = read(STDIN_FILENO, buf, 5)) > 0)
            if(write(nfd, buf, n) != n)
            {
                printf("Write Error!!
    ");
                return 0;
            }
        return 0;
    }
    
    上面的例子使用dup2将标准输出重定向为hello.file文件,如下所示:
    [lingyun@localhost dup2]$ ls
    dup2.c
    [lingyun@localhost dup2]$ gcc dup2.c 
    [lingyun@localhost dup2]$ ./a.out 
    hello world
    ^C
    [lingyun@localhost dup2]$ cat hello.file 
    hello world
    [lingyun@localhost dup2]$ 

    转自:http://blog.csdn.net/fulinus/article/details/9669177

  • 相关阅读:
    数据结构学习笔记(特殊的线性表:栈与队列)
    数据结构学习笔记(线性表)
    使用U盘安装 OS X 的坑
    chrome插件推荐
    Mac下安装oh-my-zsh
    sublime下让代码居中
    Mac上关于shell使用Python3和C++11声明
    github学习(三)
    github学习(二)
    github学习(一)
  • 原文地址:https://www.cnblogs.com/BinBinStory/p/7348563.html
Copyright © 2011-2022 走看看