Can someone explain what dup() in C does?

2019-03-08 11:21发布

I know that dup, dup2, dup3 "create a copy of the file descriptor oldfd"(from man pages). However I can't digest it.

As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier to just

fd=fd2;

Whenever we want to duplicate a file descriptor?

And something else..

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

Does that mean that it can also take as value stdin, stdout or stderr if we assume that we have close()-ed one of those?

标签: c pipe dup2 dup
8条回答
我想做一个坏孩纸
2楼-- · 2019-03-08 11:39

Let's say you're writing a shell program and you want to redirect stdin and stdout in a program you want to run. It could look something like this:

fdin = open(infile, O_RDONLY);
fdout = open(outfile, O_WRONLY);
// Check for errors, send messages to stdout.
...
int pid = fork(0);
if(pid == 0) {
    close(0);
    dup(fdin);
    close(fdin);
    close(1);
    dup(fdout);
    close(fdout);
    execvp(program, argv);
}
// Parent process cleans up, maybe waits for child.
...

dup2() is a little more convenient way to do it the close() dup() can be replaced by:

dup2(fdin, 0);
dup2(fdout, 1);

The reason why you want to do this is that you want to report errors to stdout (or stderr) so you can't just close them and open a new file in the child process. Secondly, it would be a waste to do the fork if either open() call returned an error.

查看更多
萌系小妹纸
3楼-- · 2019-03-08 11:47

dup() and dup2() system call

•The dup() system call duplicates an open file descriptor and returns the new file descriptor.

•The new file descriptor has the following properties in common with the original file descriptor: 1. refers to the same open file or pipe. 2. has the same file pointer -- that is, both file descriptors share one file pointer. 3. has the same access mode, whether read, write, or read and write.

• dup() is guaranteed to return a file descriptor with the lowest integer value available.It is because of this feature of returning the lowest unused file descriptor available that processes accomplish I/O redirection.

int dup(file_descriptor)

int dup2(file_descriptor1, file_descriptor2)

查看更多
forever°为你锁心
4楼-- · 2019-03-08 11:50

Just wanted to respond to myself on the second question after experimenting a bit.

The answer is YES. A file descriptor that you make can take a value 0, 1, 2 if stdin, stdout or stderr are closed.

Example:

close(1);     //closing stdout
newfd=dup(1); //newfd takes value of least available fd number

Where this happens to file descriptors:

0 stdin     .--------------.     0 stdin     .--------------.     0 stdin
1 stdout   =|   close(1)   :=>   2 stderr   =| newfd=dup(1) :=>   1 newfd
2 stderr    '--------------'                 '--------------'     2 stderr
查看更多
神经病院院长
5楼-- · 2019-03-08 11:51

see this page, stdout can be aliased as dup(1)...

查看更多
一夜七次
6楼-- · 2019-03-08 11:52

The single most important thing about dup() is it returns the smallest integer available for a new file descriptor. That's the basis of redirection:

int fd_redirect_to = open("file", O_CREAT);
close(1); /* stdout */
int fd_to_redirect = dup(fd_redirect_to); /* magically returns 1: stdout */
close(fd_redirect_to); /* we don't need this */

After this anything written to file descriptor 1 (stdout), magically goes into "file".

查看更多
smile是对你的礼貌
7楼-- · 2019-03-08 11:55

A file descriptor is a bit more than a number. It also carries various semi-hidden state with it (whether it's open or not, to which file description it refers, and also some flags). dup duplicates this information, so you can e.g. close the two descriptors independently. fd=fd2 does not.

查看更多
登录 后发表回答