用dup2和dup产生一份file descriptor 的拷贝
在类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 的拷贝相关推荐
- 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 ...
- 一个 bad file descriptor 的问题
先来看一个 demo: 1 package main2 3 import (4 "fmt"5 "net"6 "os"7 "runt ...
- File Descriptor问题总结
今天客户物理机上遇到文件描述符用尽的问题,现象包括: SSH连接物理机卡住 PG服务端口TCP心跳检测失败 PSQL卡住 报错:too many open files 概念 在Linux系统中一切皆可 ...
- 文件描述符file descriptor与inode的相关知识
每个进程在Linux内核中都有一个task_struct结构体来维护进程相关的 信息,称为进程描述符(Process Descriptor),而在操作系统理论中称为进程控制块 (PCB,Process ...
- linux 安装包 在此作用域中尚未声明_Linux运行go项目报错:copy_file_range: bad file descriptor...
这两天在 Linux 环境部署一个 Go 项目遇到一个报错:copy_file_range: bad file descriptor.网上查找各种方法,花了两天的时间,经过一番折腾后才解决,觉得非常有 ...
- socket:file descriptor exceeds limit (4096/4096)
前段时间同事管理的一台DNS服务器,由于并发数太大,导致了日志中存在着大量这样的记录: socket:file descriptor exceeds limit (4096/4096) 有错误的字面意 ...
- php扩展dio,PHP Dio扩展新函数dio_fdopen参数返回--bad file descriptor的分
昨天准备做一个程序,PHP的串口扩展程序,用来做串口打开的,于是用dio_fdopen来新建一个文件: view plaincopy to clipboardprint? 1. 2.<?php ...
- 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 ...
- Ubuntu下修改file descriptor
要修改Ubuntu下的file descriptor的话,请参照一下步骤. (1)修改limits.conf $sudo vi /etc/security/limits.conf 增加一行 * - n ...
最新文章
- 读书笔记:编写高质量代码--web前端开发修炼之道(二:5章)
- .NET WinForm中给DataGridView自定义ToolTip并设置ToolTip的样式
- 深入理解分布式技术 - 理论基石 CAP
- html基础1-基本语法/段落标签/特殊符号
- python messagebox一定要指定父窗体吗,为什么要使用的MessageBox.show一个所有者窗口?...
- python时间转换
- IDF实验室-图片里的英语
- C#实现捕获当前屏幕截图(转)
- php 查找多维数组的值_php在多维数组中查找指定值的方法
- 2017蓝桥杯结果填空:迷宫
- 大数据开发笔记(十):Hbase列存储数据库总结
- java语言实现_java语言实现树
- 盘点含金量高的几种编程比赛
- cad文字宽度因子_字体宽度因子改不了 cad宽度因子无法修改
- 特定场景下的网络质量评估与预警方法介绍
- 学习笔记:强化学习与最优控制(Chapter 2)
- 使用tcpdf合成PDF文件
- 渗透分支写脚本_抖音文案怎么写吸引人?最新文案创作技巧分享(赠文案脚本模板)...
- 天津理工上机c语言报告5,天津理工大学C语言上机报告题目加答案.doc
- 到底什么是商业模式?