在类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.

转载于:https://www.cnblogs.com/tianchaoy/p/3622414.html

用dup2和dup产生一份file descriptor 的拷贝相关推荐

  1. git或者ssh出错 fatal:open /dev/null or dup failed: No such file or directory、弹出mitty.dump文件

    使用git Bash here闪退并生成mintty.exe.stackdump文件 cmd使用git 报错 fatal:open /dev/null or dup failed: No such f ...

  2. 一个 bad file descriptor 的问题

    先来看一个 demo: 1 package main2 3 import (4 "fmt"5 "net"6 "os"7 "runt ...

  3. File Descriptor问题总结

    今天客户物理机上遇到文件描述符用尽的问题,现象包括: SSH连接物理机卡住 PG服务端口TCP心跳检测失败 PSQL卡住 报错:too many open files 概念 在Linux系统中一切皆可 ...

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

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

  5. linux 安装包 在此作用域中尚未声明_Linux运行go项目报错:copy_file_range: bad file descriptor...

    这两天在 Linux 环境部署一个 Go 项目遇到一个报错:copy_file_range: bad file descriptor.网上查找各种方法,花了两天的时间,经过一番折腾后才解决,觉得非常有 ...

  6. socket:file descriptor exceeds limit (4096/4096)

    前段时间同事管理的一台DNS服务器,由于并发数太大,导致了日志中存在着大量这样的记录: socket:file descriptor exceeds limit (4096/4096) 有错误的字面意 ...

  7. php扩展dio,PHP Dio扩展新函数dio_fdopen参数返回--bad file descriptor的分

    昨天准备做一个程序,PHP的串口扩展程序,用来做串口打开的,于是用dio_fdopen来新建一个文件: view plaincopy to clipboardprint? 1. 2.<?php ...

  8. This file can not be opened as a file descriptor; it is probably compressed

    链接:FileNotFoundException: This file can not be opened as a file descriptor; it is probably compresse ...

  9. Ubuntu下修改file descriptor

    要修改Ubuntu下的file descriptor的话,请参照一下步骤. (1)修改limits.conf $sudo vi /etc/security/limits.conf 增加一行 * - n ...

最新文章

  1. 上传图片配置控制大小_esp32-cam拍照上传云平台,http协议传输
  2. leetcode处女作
  3. MIT自然语言处理第三讲:概率语言模型(第一、二、三部分)
  4. 《设计模式详解》创建型模式 - 原型模式
  5. 【李宏毅2020 ML/DL】P84 SAGAN, BigGAN, SinGAN, GauGAN, GANILLA, NICE | More About GAN 2020
  6. 【CCCC】L3-004 肿瘤诊断 (30分),三维BFS
  7. 函数求和公式计算机出库入库,Excel 库存统计相关函数及制作库存统计表
  8. 如何使用JGIT在远程仓库获取提交详情记录
  9. 2021CentOS7系统Gnome3桌面使用Fcitx
  10. 通过phpstudy(小皮面板)搭建DVWA靶场教程
  11. 更新Windows 11后,桌面狂闪,没有显示图标,鼠标指针一直在加载中,如何解决?
  12. C++小系统——餐馆员工管理系统及餐馆点菜系统(一)
  13. 【编程马拉松】【011-鸽兔同校】
  14. R语言绘图、数据处理学习记录持续更新
  15. mysql带中文日期转换_【MySQL】日期时间格式转换_MySQL
  16. 网络安全从入门到精通的学习资源汇总
  17. Outlook 2016 pst/ost邮件数据文件迁移实现
  18. 示波器中的Trigger
  19. 区块链的3个进阶阶段
  20. 第七天之多态原理探究

热门文章

  1. 超级牛批的IP地址查询工具
  2. 毕设总结(久等了~)
  3. 电商平台搭建流程是怎样的_不懂编程怎么搭建_OctShop
  4. 社区电商平台运营中常遇到的一些问题
  5. Altium 常用快捷键
  6. 计算机设备养护知识试题,技术设备处设备管理知识培训试题库
  7. 蒲慕明院士:脑机融合技术或许会成为未来人工智能的一个热门方向
  8. android studio 读音,simplicity
  9. 微信小程序校园学生选课教学论坛信息管理系统SSM-JAVA【数据库设计、论文、源码、开题报告】
  10. kds官方android客户端,电子厨打设置(KDS/ADS)