Can someone explain what dup() in C does?

余生颓废 提交于 2019-12-02 16:20:58

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

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.

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".

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.

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

Just a tip about "duplicating standard output".

On some Unix Systems (but not GNU/Linux)

fd = open("/dev/fd/1", O_WRONLY);

it is equivalent to:

fd = dup(1);

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

A question arose again: How can I dup() a file descriptor that I already closed?

I doubt that you conducted the above experiment with the shown result, because that would not be standard-conforming - cf. dup:

The dup() function shall fail if:

[EBADF]
The fildes argument is not a valid open file descriptor.

So, after the shown code sequence, newfd must be not 1, but rather -1, and errno EBADF.

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!