#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
当调用dup函数时,内核在进程中创建一个新的文件描述符,此描述符是当前可用文件描述符的最小数值,这个文件描述符指向oldfd所拥有的文件表项。
dup2和dup的区别就是可以用newfd参数指定新描述符的数值,如果newfd已经打开,则先将其关闭。如果newfd等于oldfd,则dup2返回newfd, 而不关闭它。dup2函数返回的新文件描述符同样与参数oldfd共享同一文件表项。
APUE用另外一个种方法说明了这个问题:
实际上,调用dup(oldfd)等效于,fcntl(oldfd, F_DUPFD, 0)
而调用dup2(oldfd, newfd)等效于,close(oldfd);fcntl(oldfd, F_DUPFD, newfd);
dup函数实例:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char* argv[])
{
int fd = open("hello", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if(fd < 0)
{
printf("Open Error!!\n");
return 0;
}
int nfd = dup(fd);
if(nfd < 0)
{
printf("Error!!\n");
return 0;
}
char buf[1000];
int n;
while((n = read(STDIN_FILENO, buf,1000)) > 0)
{
if(write(nfd, buf, n) != n)
{
printf("Write Error!!\n");
return 0;
}
}
return 0;
}
上面代码中,nfd 拷贝了 fd,所以 write ( nfd, buf, n ) 这语句写到 nfd 所代表的文件时也就是写到 fd 所代表的文件。程序执行完后可以在相应的目录的hello文件中看到输出。
[lingyun@localhost dup]$ gcc dup.c
[lingyun@localhost dup]$ ls
a.out dup.c
[lingyun@localhost dup]$ ./a.out
hello world
^C
[lingyun@localhost dup]$ ls
a.out dup.c hello
[lingyun@localhost dup]$ cat hello
hello world
[lingyun@localhost dup]$
dup2函数实例:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char* argv[])
{
int fd = open("hello.file", O_CREAT|O_RDWR|O_TRUNC,S_IRUSR|S_IWUSR);
if(fd < 0)
{
printf("Open Error!!\n");
return 0;
}
int nfd = dup2(fd, STDOUT_FILENO);
if(nfd < 0)
{
printf("Error!!\n");
return 0;
}
char buf[5];
int n;
while((n = read(STDIN_FILENO, buf, 5)) > 0)
if(write(STDOUT_FILENO, buf, n) != n)
{
printf("Write Error!!\n");
return 0;
}
return 0;
}
上面的例子使用dup2将标准输出重定向为hello.file文件,如下所示:
[lingyun@localhost dup2]$ ls
dup2.c
[lingyun@localhost dup2]$ gcc dup2.c
[lingyun@localhost dup2]$ ./a.out
hello world
^C
[lingyun@localhost dup2]$ cat hello.file
hello world
[lingyun@localhost dup2]$
1658

被折叠的 条评论
为什么被折叠?



