dup

\* dup eg. *\
    int fd=open("text.txt", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
    if(fd < 0)
    {
        printf("Open Error!\n");
        return 0;
    }
    int fd2=dup(fd);
    if(fd2<0)
    {
        printf("Error!\n");
        return 0;
    }


\* dup2 eg. *\
    int oldfd;
    int fd;
    int t;

    char *buf="This is a test!!!!\n";
    if((oldfd=open("mine.txt",O_RDWR|O_CREAT,0644))==-1)
    {
        printf("open error\n");
        exit(-1);
    }

    fd=dup2(oldfd,fileno(stdout));
    if(fd==-1)
    {
        printf("dup2 error\n");
        exit(-1);
    }

    t=strlen(buf);
    if(write(fileno(stdout),buf,t)!=t)//本应该写入到stdout的信息,但是标准输出已经重定向到目标文件中,故向标准输出写的数据将会写到目标文件中。
    {
        printf("write error!\n");
        exit(-1);
    }
    close(fd);
  • int fileno(FILE *stream) 把文件流指针转换成文件描述符
  • 子进程dup对父进程无效,但对exec有效

注意

管道与重定向常常需要使用dup与dup2复制句柄,其中dup2又较为常用,但是使用dup2有几个小坑需要注意。

int dup2(int oldfd, int newfd);

man手册页上是这样讲的,oldfd是想要复制的句柄,newfd是复制到的句柄号,如果newfd已经打开,dup2会先尝试关闭

复制完成后,oldfd与newfd都将指向同一文件实例。一般需要close(oldfd)来减少不必要的引用。所以一般人可能会这样写代码:

1 if (dup2 (oldfd, newfd) != -1)
2 close (oldfd);
但是有个例外的情况,就是oldfd==newfd,照man手册页上讲,此时dup2将什么也不做,直接返回成功

此时oldfd与newfd相同,而close(oldfd)将导致文件的唯一引用被关闭,后续的操作可想而知也会失败。

所以万无一失的dup2使用方法是这样:

1 if (oldfd != newfd) {
2 if (dup2 (oldfd, newfd) != -1)
3 close (oldfd);
4 }

posted @ 2020-02-18 12:08  friedCoder  阅读(411)  评论(0)    收藏  举报