在类Unix操作系统里面,。dup2和dup都通过系统调用来产生一份file descriptor 的拷贝。
 
   dup对我来说还很简单
 
   int dup(int filedes);
 
   dup2就有点犯迷糊了
 
   int dup2(int filedes1,int filedes2);
 
   其实这样declaration更好
 
   int dup2(int oldfd,int newfd)
 
   下面是apue给出的解释
 
   With dup2, we specify the value of the new descriptor with the fd2 argument.
 
   If fd2 is already open, it is first closed. If fd equals fd2,t hen dup2 re turns fd2 without closing it.
 
   Otherwise, theFD_CLOEXECfile descriptor flag is cleared forfd2, so that fd2 is left open if the process calls exec
 
   我就比较疑惑,如果newfd是STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO,(0,1,2)三个中的一个呢?
 
   dup2是返回错误还是 “fd2 is already open, it is first closed”呢?
 
   Just test it.
 
   #include
 
   #include"fcntl.h"
 
   #include
 
   int main()
 
   {
 
   int file_descriptor;
 
   if((file_descriptor = open("./text.t",O_RDWR)) < 0)
 
   {
 
   printf("error\n");
 
   }
 
   else
 
   {
 
   printf("file descriptor is %d\n",file_descriptor);
 
   }
 
   printf("dup2 return value:%d\n dup return value\
 
   %d\n",dup2(file_descriptor,0),dup(file_descriptor));
 
   printf("dup2 return value:%d\ndup return value\
 
   %d\n",dup2(file_descriptor,1),dup(file_descriptor));
 
   printf("dup2 return value:%d\ndup return value\
 
   %d\n",dup2(file_descriptor,2),dup(file_descriptor));
 
   close(file_descriptor);
 
   return 0;
 
   }
 
   Aha! Something interesting happened.
 
   运行结果:
 
   file descriptor is 3
 
   dup2 return value:0
 
   dup return value 4
 
   以上就是这段code的运行结果。嗯。。。我有三个printf语句,为啥米只答应了第一个,WTF。。。
 
   问题都在第二个printf语句中调用的dup2 www.jx-jf.com
 
   dup2的第二个参数是1,1是什么?STDIN_FILENO宏的本质就是1.这里还是那句话“fd2 is already open, it is first closed”
 
   三个标准流在文件进程开始的时候肯定是都被打开的,already open是一定的事情 www.yzyedu.com
 
   So。。。dup2非常“老实本分"的把STDOUT_FILENO在第二printf的时候关闭了
 
   这个时候printf的输出就不会被打印在标准输出设备上面了,因为STDOUT_FILENO已经被关闭了!!
 
   如果这个时候只要把第二个printf里面dup2的第二个参数改成大于2的数字就没事了。
 
   code:
 
   #include
 
   #include"fcntl.h"
 
   #include
 
   int main()
 
   {
 
   int file_descriptor;
 
   if((file_descriptor = open("./text.t",O_RDWR)) < 0)
 
   {
 
   printf("error\n");
 
   }
 
   else
 
   {
 
   printf("file descriptor is %d\n",file_descriptor);
 
   }
 
   printf("dup2 return value:%d\n dup return value\
 
   %d\n",dup2(file_descriptor,0),dup(file_descriptor));
 
   printf("dup2 return value:%d\ndup return value\
 
   %d\n",dup2(file_descriptor,10),dup(file_descriptor));
 
   printf("dup2 return value:%d\ndup return value\
 
   %d\n",dup2(file_descriptor,11),dup(file_descriptor));
 
   close(file_descriptor);
 
   return 0;
 
   }
 
   运行结果:
 
   file descriptor is 3
 
   dup2 return value:0
 
   dup return value 4
 
   dup2 return value:10
 
   dup return value 5
 
   dup2 return value:11
 
   dup return value 6
 
   这个时候就很清楚了。
 
   Similarly,the call
 
   dup2(fd, fd2);
 
   is equivalent to
 
   close(fd2);
 
   fcntl(fd, F_DUPFD, fd2);
 
   当然也不完全相同,不同点如下:
 
   1. dup2 is an atomic operation, whereas the alternate form involves two function
 
   calls. It is possible in the latter case to have a signal catcher called between the
 
   closeand the fcntlthat could modify the file descriptors. (W edescribe
 
   signals in Chapter 10.) The same problem could occur if a different thread
 
   changes the file descriptors. (Wedescribe threads in Chapter 11.)
 
   2. Ther eare s ome errnodifferences between dup2 and fcntl.

用dup2和dup产生一份file descriptor 的拷贝的更多相关文章

  1. 文件描述符file descriptor与inode的相关知识

    每个进程在Linux内核中都有一个task_struct结构体来维护进程相关的 信息,称为进程描述符(Process Descriptor),而在操作系统理论中称为进程控制块 (PCB,Process ...

  2. [svc]linux中的文件描述符(file descriptor)和文件

    linux中的文件描述符(file descriptor)和文件 linux为了实现一切皆文件的设计哲学,不仅将数据抽象成了文件,也将一切操作和资源抽象成了文件,比如说硬件设备,socket,磁盘,进 ...

  3. 近期编程问题——epoll failed:bad file descriptor

    出现问题:epoll_wait:Bad file descriptor 原因:IO时间的socket描述符在epoll_ctl处理前就关闭了. 解决方法:不要在epoll_ctl之前关闭socket描 ...

  4. mongodb在ubuntu下的couldn‘t remove fs lock errno:9 Bad file descriptor的错误

    按照官网上的安装方法: 在ubuntu系统下有可能出现如下错误: couldn't remove fs lock errno:9 Bad file descriptor 此时需要修改文件所有者 $ s ...

  5. shell 中最常使用的 FD (file descriptor)

    在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...

  6. python print 在windows上 出现 Bad file descriptor error

    先说一下情况,一个python写的采集程序,做成windows服务在windows上运行. 这个问题出现的挺奇特,本来一套采集程序,一个采集文件的时候没问题,两个采集文件的时候也没问题,当三个采集文件 ...

  7. windows pm2 启动nodejs失败:Error: EBADF: bad file descriptor, uv_pipe_open

    windows下打开命令窗口,安装pm2:npm install pm2 -g pm2成功安装,在项目目录下用pm2启动服务:pm2 start index.js,结果启动失败,错误如下: .pm2\ ...

  8. fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument

    fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument <error>status:4 er ...

  9. dpdk EAL: Error reading from file descriptor 23: Input/output error

    执行test程序时输出: EAL: Error reading from file descriptor 23: Input/output error 原因: 在虚拟机添加的网卡,dpdk不支持导致的 ...

随机推荐

  1. BZOJ 1021 [SHOI2008]Debt 循环的债务

    1021: [SHOI2008]Debt 循环的债务 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 694  Solved: 356[Submit][S ...

  2. 「Poetize10」能量获取

    描述 Description “封印大典启动,请出Nescafe魂珠!”随着 圣主applepi一声令下,圣剑护法rainbow和魔杖护法freda将Nescafe魂珠放置于封印台上.封印台是一个树形 ...

  3. bzoj3573[Hnoi2014]米特运输

    http://www.lydsy.com/JudgeOnline/problem.php?id=3573 好吧,虽然这是day1最后一题,但却是最水的一题....(前提:看懂题目) 仔细看题! 仔细看 ...

  4. SQL server 分离数据库与删除数据库的区别

    今天,在sql server 中,分离数据库,然后就问了一下,与删除数据库的区别 区别在于(百度一下): 分离后,.mdb和.log文件都在,以后你需要用的时候,还可以用附加数据库的方法加上去,分离数 ...

  5. Java---练习:文件切割与合并(1)

    实现对大文件的切割与合并. 按指定个数切(如把一个文件切成10份)或按指定大小切(如每份最大不超过10M),这两种方式都可以. 示例程序说明: 文件切割:把一个文件切割成多个碎片,每个碎片的大小不超过 ...

  6. Java学习日记8-包、环境变量和访问控制

    Java中的包.环境变量和访问控制 一.java中的包 Java利用包来组织代码,一来使大型项目的代码结构清晰,二来包是一个命名空间的划分,即不同包中可以有相同名字的类,只需在在类名前加上包名即可区分 ...

  7. Eucalyptus使用的技术

    libvirt Libvirt 库是一种实现 Linux 虚拟化功能的 Linux® API,它支持各种虚拟机监控程序,包括 Xen 和 KVM,以及 QEMU 和用于其他操作系统的一些虚拟产品. N ...

  8. F - Power Network - poj 1459(简单最大流)

    题目大意:题目说了一大堆,其实都是废话......让人有些不知所云,其实就是给了一些电厂,和一些消费点,然后里面有一些路线什么的,求出消费点可以最多消费的电量是多少. 输入大意: 分析:懂了题意就是一 ...

  9. Git详解之二 Git基础

    Git 基础 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置: ...

  10. 两个关于XML解析报错问题小记

    Caused by: org.xml.sax.SAXParseException: The string "--" is not permitted within comments ...