UNIX重定向--dup(2)函数
用同一个open()打开的文件可以有多个描述字与它相连,这种描述字称为重复描述字。重复一个描述字有两种方法:用函数dup()或dup2(),或用函数fcntl()。
#include <unistd.h>
int dup (int old);
int dup2 (int old,int new);
dup()复制描述字old至一个新描述字,新描述字保证是当前未打开的最小编号可用描述字。dup2()复制描述字old至编号为new的描述字。如果new已经打开,它将首先被关闭。如果new等于old,dup2()返回new但不关闭它。
这两个函数调用成功返回新的文件描述字。所返回的新描述字与参数old给定的描述字引用同一个打开的文件,即共享同一个系统打开文件表项
在这个图示中,假定进程一开始便执行
newfd = dup(1);
因此newfd的值一定是3(因为描述字0、1、2已经由shell打开),它与描述字1都指向标准输出文件,因为它的进程打开文件表项由描述字1的表项复制而来。
正因为重复描述字共享同一个系统打开文件表项,因此,它们共享文件位置和一组文件状态标签。但是它们都有自己的文件描述字标签。这两个dup函数总是清除新描述字中的执行即关闭标签FD_CLOEXEC。
重复一个文件描述字的主要用途是实现输入输出重定向,即改变一个特定文件描述字对应的文件或管道。当使用管道进行进程间的通信时,这两个函数十分有用。第11章讨论进程间通信时将见到应用这两个函数的例子。
下面这个程序是用dup2()简单重定向的例子。它将标准输出文件重定向至名为myoutput的文件。运行这个程序可以看到printf()的输出不在终端而在文件myoutput中。
int main(void){ int fd; if((fd = open("myoutput",O_WRONLY|O_CREAT,0644)) == -1) err_exit("myoutput"); if(dup2(fd,STDOUT_FILENO) == -1) /* 重复标准输出至fd相连的文件myoutput */ err_exit("Redirect standard output failed"); printf("this is a test program for redirect \n"); close(fd);}
#include <sys/stat.h>#include <string.h>#include <fcntl.h>#include <io.h>int main(void){ #define STDOUT 1 //标准输出文件描述符 号 int nul, oldstdout; char msg[] = "This is a test"; /* create a file *///打开一个文件,操作者具有读写权限 如果文件不存在就创建 nul = open("DUMMY.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE); /* create a duplicate handle for standard output *///创建STDOUT的描述符备份 oldstdout = dup(STDOUT); /* redirect standard output to DUMMY.FIL by duplicating the file handle onto the file handle for standard output. *///重定向STDOUT到nul dup2(nul, STDOUT); /* close the handle for DUMMY.FIL *///重定向之后要关闭nul close(nul); /* will be redirected into DUMMY.FIL *///写入数据 write(STDOUT, msg, strlen(msg)); /* restore original standard output handle *///还原 dup2(oldstdout, STDOUT); /* close duplicate handle for STDOUT */ close(oldstdout); return 0;}//结果就是msg写到了文件中而不是STDOUT