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

查看:33
本文介绍了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 告诉 shell 给命令一个文件描述符 2,它是描述符 1 的副本.(即 stderr & stdout 指向同一个 fd).
现在在 non-existing file 上调用 ls 的错误消息和 existing filels 的正确输出显示在 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
", 12);
    close(p[STDIN]);
    close(p[STDOUT]);
}

孩子将读取端复制到文件描述符0上,关闭文件dep 中的脚本和 wc 中的 execs.当 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天全站免登陆