首頁  >  文章  >  資料庫  >  dup2这家伙

dup2这家伙

WBOY
WBOY原創
2016-06-07 15:07:141746瀏覽

在类Unix操作系统里面,。dup2和dup都通过系统调用来产生一份file descriptor 的拷贝。 dup对我来说还很简单 int dup(int filedes); dup2就有点犯迷糊了 int dup2(int filedes1,int filedes2); 其实这样declaration更好 int dup2(int oldfd,int newfd) 下面

在类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,then dup2 returns 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<apue.h>
#include"fcntl.h"
#include<stdio.h>
int main()
{
        int file_descriptor;
        if((file_descriptor = open("./text.t",O_RDWR)) <br>
Aha! Something interesting happened.

<p>
运行结果:</p>
<p>
file descriptor is 3<br>
dup2 return value:0<br>
 dup return value 4<br>
</p>
<p>
<span>以上就是这段code的运行结果。嗯。。。我有三个printf语句,为啥米只答应了第一个,WTF。。。</span></p>
<p>
<span>问题都在第二个printf语句中调用的dup2</span></p>
<p>
<span> dup2的第二个参数是1,1是什么?STDOUT_FILENO宏的本质就是1.这里还是那句话<span>“fd2 is already open, it is first closed”</span><br>
</span></p>
<p>
<span>三个标准流在文件进程开始的时候肯定是都被打开的,already open是一定的事情。</span></p>
<p>
<span>So。。。dup2非常“老实本分"的把STDOUT_FILENO在第二printf的时候关闭了</span></p>
<p>
<span>这个时候printf的输出就不会被打印在标准输出设备上面了,因为STDOUT_FILENO已经被关闭了!!</span></p>
<p>
<span>如果这个时候只要把第二个printf里面dup2的第二个参数改成大于2的数字就没事了。</span></p>
<p>
code:</p>
<p>
</p>
<pre class="brush:php;toolbar:false">#include<apue.h>
#include"fcntl.h"
#include<stdio.h>
int main()
{
        int file_descriptor;
        if((file_descriptor = open("./text.t",O_RDWR)) 

<p>
<span>运行结果:</span></p>
<p>
file descriptor is 3<br>
dup2 return value:0<br>
dup return value 4<br>
dup2 return value:10<br>
dup return value 5<br>
dup2 return value:11<br>
dup return value 6<br>
</p>
<p>
<span>这个时候就很清楚了。</span></p>
<p>
<span>Similarly,the call<br>
dup2(fd, fd2);<br>
is equivalent to<br>
close(fd2);<br>
fcntl(fd, F_DUPFD, fd2);<br>
</span></p>
<p>
<span>当然也不完全相同,不同点如下:</span></p>
<p>
<span><br>
</span></p>
<blockquote>
<p>
<span><strong>1. dup2 is an atomic operation, whereas the alternate form involves two function</strong></span></p>
<p>
<span><strong>calls.  It is possible in the latter case to have a signal catcher called between the</strong></span></p>
<p>
<span><strong>close and  the fcntl that  could  modify  the  file  descriptors. (We describe</strong></span></p>
<p>
<span><strong>signals  in  Chapter  10.) The  same  problem  could  occur  if  a  different  thread</strong></span></p>
<p>
<span><strong>changes the file descriptors. (We describe threads in Chapter 11.)</strong></span></p>
<p>
<span><strong><br>
</strong></span></p>
<p>
<span><strong>2.  There are some errno differences between dup2 and fcntl.</strong></span></p>
</blockquote>
<p>
<span></span></p>
<p>
<span></span></p>
<p>
<br>
</p>
<p>
update 2014.04.10</p>
<p>
<span>今天有了“更深”的认识吧</span></p>
<pre class="brush:php;toolbar:false"><span>int dup2(int filedes1,int filedes2);</span>
是把filedes2指向的流关闭,而重定向到filedes1指向的流,此时filedes1和filedes2均指向同一个流。

就这么简单。。。。















陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn