linux之dup和dup2函数解析

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

————————————————shell

   fd标志 文件指针数据结构

       _____________________函数

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

fd 1:|________|____________|.net

fd 2:|________|____________|指针

fd 3:|________|____________|code

      |     .......         |blog

      |_____________________|进程

图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!!\n");
        return 0;
    }


    int nfd = dup(fd);
    if(nfd < 0)
    {
        printf("Error!!\n");
        return 0;
    }


    char buf[1000];
    int n;


    while((n = read(STDIN_FILENO, buf,1000)) > 0)
    {
        if(write(nfd, buf, n) != n)
        {
            printf("Write Error!!\n");
            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!!\n");
        return 0;
    }


    int nfd = dup2(fd, STDOUT_FILENO);
    if(nfd < 0)
    {
        printf("Error!!\n");
        return 0;
    }


    char buf[5];
    int n;


    while((n = read(STDIN_FILENO, buf, 5)) > 0)
        if(write(nfd, buf, n) != n)
        {
            printf("Write Error!!\n");
            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

相关文章
相关标签/搜索