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

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

问题描述

我想了解使用 dup2 的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 ?

我倒是AP preciate如果你能解释一下,给我一个例子,其中 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的炮弹可以实现这样的命令:

Dup allows shells to implement commands like this:

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

在2>&安培; 1告诉shell给命令文件描述符2是描述符1的重复(即标准错误和放大器;标准输出指向同一个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.

下面的例子code运行具有连接标准的输入程序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]);
}

孩子的DUP读端到文件描述符0,关闭文件德
scriptors在P和高管厕所。当WC从标准输入读取,它从读取
管。结果
这是管道如何使用DUP,以及一个使用现在DUP使用管道建立别的东西来实现,这是系统调用的美女,你打造又一个使用工具,已经有一件事,这些工具是使用inturn建别的东西等..
在最后的系统调用你在内核中得到的最基本的工具。

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天全站免登陆