繁体   English   中英

为什么fork()通过dup2()关闭一个文件描述符

[英]Why fork() close one of the file descriptor by dup2()

我编写代码来理解dup2()。

int main(int argc, char* argv[]) {
    FILE *fp = fopen(argv[1],"r");
    int fdold,fdnew;
    fdold = fileno(fp);
    fdnew = dup2(fdold,fdnew);
    while (1) {
        sleep(1000);
    }
}

lsof显示了2个打开的文件描述符(/workspace/source/throw.cpp是传入的参数)

/workspace/source/thread$ lsof -p 20779
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
dup2    20779  wto  cwd    DIR    8,1     4096 946031 /workspace/source
dup2    20779  wto  rtd    DIR    8,1     4096      2 /
dup2    20779  wto  txt    REG    8,1     8672 950259 /workspace/source/dup2
dup2    20779  wto  mem    REG    8,1  1852120 135869 /lib/x86_64-linux-gnu/libc-2.17.so
dup2    20779  wto  mem    REG    8,1   149312 135845 /lib/x86_64-linux-gnu/ld-2.17.so
dup2    20779  wto    0u   CHR  136,4      0t0      7 /dev/pts/4
dup2    20779  wto    1u   CHR  136,4      0t0      7 /dev/pts/4
dup2    20779  wto    2u   CHR  136,4      0t0      7 /dev/pts/4
dup2    20779  wto    3r   REG    8,1      653 951057 /workspace/source/throw.cpp
dup2    20779  wto *767r   REG    8,1      653 951057 /workspace/source/throw.cpp

但是,当我fork()它到2个进程(代码如下)时,只打开了一个/workspace/source/throw.cpp。

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
    FILE *fp = fopen(argv[1],"r");
    int fdold,fdnew;
    fdold = fileno(fp);
    //fcntl(F_DUPFD_CLOEXEC,fdold);
    fdnew = dup2(fdold,fdnew);
    pid_t pid;
    if ((pid = fork()) < 0) {
        exit(-1);
    } else if (pid > 0) {
        waitpid(pid, WNOHANG, NULL);
        printf("parent exit\n");    
    } else {
        while (1) {
            sleep(1000);
        }
    }
    return 0;
}
  1. 问题1:是什么导致dup()d fd被关闭?
  2. 问题2:我在手册中查看了FD_CLOEXEC,但是没有通过fcntl()设置它。 fopen()会自动设置吗? 并且这个标志不仅影响fork而且影响exec系列吗?
  3. 问题3:在我用dup替换dup2之后,结果显示2个fds作为我的期望。 正如手册所说:

    “dup2()使newfd成为oldfd的副本,必要时首先关闭newfd”。

    如果newfd已经打开,是否意味着在dup之前关闭newfd?

您的代码有一个错误(在设置之前使用fdnew的值),因此其行为将是不可预测的。 在尝试了解程序正在执行的操作之前修复错误。 此外,您应该检查dup2的返回值以查看它是否成功。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM