dup2/dup-为什么我需要复制一个文件描述符? [英] dup2 / dup - why would I need to duplicate a file descriptor?

查看:74
本文介绍了dup2/dup-为什么我需要复制一个文件描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解dup2dup的用法.

I'm trying to understand the use of dup2 and dup.

在手册页中:

DESCRIPTION

dup and dup2 create a copy of the file descriptor oldfd.
After successful return of dup or dup2, the old and new descriptors may
be used interchangeably. They share locks, file position pointers and
flags; for example, if the file position is modified by using lseek on
one of the descriptors, the position is also changed for the other.

The two descriptors do not share the close-on-exec flag, however.

dup uses the lowest-numbered unused descriptor for the new descriptor.

dup2 makes newfd be the copy of oldfd, closing newfd first if necessary.  

RETURN VALUE

dup and dup2 return the new descriptor, or -1 if an error occurred 
(in which case, errno is set appropriately).  

我为什么需要该系统调用?复制文件描述符有什么用?

Why would I need that system call? what is the use of duplicating the file descriptor?

如果我有文件描述符,为什么要复制它?

If I have the file descriptor, why would I want to make a copy of it?

如果您能解释一下并举一个需要dup2/dup的示例,我将不胜感激.

I'd appreciate if you could explain and give me an example where dup2 / dup is needed.

谢谢

推荐答案

dup系统调用复制了一个现有的文件描述符,并返回了一个新的 引用相同的基础I/O对象.

The dup system call duplicates an existing file descriptor, returning a new one that refers to the same underlying I/O object.

Dup允许Shell实现如下命令:

Dup allows shells to implement commands like this:

ls existing-file non-existing-file > tmp1  2>&1

2>& 1告诉外壳程序给命令一个文件描述符2,该文件描述符2与描述符1相同(即stderr& stdout指向相同的fd). 现在,显示在不存在的文件上调用 ls 的错误消息以及在现有文件 ls 的正确输出在 tmp1 文件中.

The 2>&1 tells the shell to give the command a file descriptor 2 that is a duplicate of descriptor 1. (i.e stderr & stdout point to same fd).
Now the error message for calling ls on non-existing file and the correct output of ls on existing file show up in tmp1 file.

以下示例代码在连接标准输入的情况下运行程序wc 到管道的读取端.

The following example code runs the program wc with standard input connected to the read end of a pipe.

int p[2];
char *argv[2];
argv[0] = "wc";
argv[1] = 0;
pipe(p);
if(fork() == 0) {
    close(STDIN); //CHILD CLOSING stdin
    dup(p[STDIN]); // copies the fd of read end of pipe into its fd i.e 0 (STDIN)
    close(p[STDIN]);
    close(p[STDOUT]);
    exec("/bin/wc", argv);
} else {
    write(p[STDOUT], "hello world\n", 12);
    close(p[STDIN]);
    close(p[STDOUT]);
}

孩子将读取端复制到文件描述符0上,关闭文件de p和execs wc中的脚本编写者.当wc从其标准输入读取时,它将从 管道.
这就是使用dup实现管道的方式,那么,现在使用dup的一种用法就是使用管道构建其他东西,这就是系统调用的美,您可以使用已经存在的工具来构建另一件东西,这些工具又是通过还有其他的东西.. 最后,系统调用是您在内核中获得的最基本的工具

The child dups the read end onto file descriptor 0, closes the file de scriptors in p, and execs wc. When wc reads from its standard input, it reads from the pipe.
This is how pipes are implemented using dup, well that one use of dup now you use pipe to build something else, that's the beauty of system calls,you build one thing after another using tools which are already there , these tool were inturn built using something else so on .. At the end system calls are the most basic tools you get in kernel

干杯:)

这篇关于dup2/dup-为什么我需要复制一个文件描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆