用dup2和dup产生一份file descriptor 的拷贝
2014-03-25 09:25
218 查看
在类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.
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.
相关文章推荐
- iOS网络编程 ASIHTTPRequest 详解, http 请求终结者
- Wix 使用总结(续)--关于Feature和Component的状态判断安装过程(转)
- XAML的Resource和Style
- 预编译头文件
- Java线程池使用说明
- VS2008项目相关文件介绍
- 快速阅读等三种读书方法
- 扁平化设计的前世今生
- 获取中文字符串的拼音
- ios 编码转换 保存文件
- 获取中文字符串的拼音
- FlexPaper及二次开发
- 源程序名称AccountTest.java,实现线程同步的例子
- 黑客研究出盗取信息的无人机
- Linux操作系统基础理论(5)-----EXT2文件系统的实现机制
- 一个epoll例子(一)
- 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩阵,判断其是否能够由一个全零矩阵经过上述运算得到。
- HashTable 的内部实现
- 如何快速有效读书
- 将笔记本变身WiFi热点