Linux copy file descriptor functions dup() and dup2()

Both of the following functions can be used to copy an existing file descriptor.

#include <unistd.h>
int dup(int fd);
int dup2(int fd,int fd2);

The return values ​​of the two functions: if successful, return a new file descriptor: if an error occurs, return -1

The new file descriptor returned dup()must be the smallest number of currently available file descriptors. For , the value of the new descriptor dup2()can be specified with parameters. fd2If fd2it is already open, close it first. If fdequal fd2, dup2()return fd2without closing it. Otherwise, fd2the FD_CLOEXECfile descriptor flag is cleared so that fd2it is open when the process calls exec.

The new file descriptor returned by these functions shares the same file table entry as the parameter fd

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-zeLck7p3-1655261801929) (C:\Users\Listening\AppData\Roaming\Typora\typora-user-images\ image-20220615104432679.png)]

In this diagram, we assume that the process starts by executing:

newfd dup(1);

When this function starts executing, it is assumed that the next available descriptor is 3 (which is very possible since 0, 1 and 2 are all opened by the shell). Because both descriptors point to the same file table entry, they share the same file status flags (read, write, append, etc.) and the same current file offset.

Each file descriptor has its own set of file descriptor flags. The close-on-exec flag of a new descriptor is always dupcleared by the function.

Another way to copy a descriptor is to use fcntla function. In fact, calling

dup(fd);

Equivalent to

fcntl(fd, F_DUPFD, 0);

And call

dup2(fd, fd2);

Equivalent to

close(fd2);
fcntl(fd, F_DUPFD, fd2);

In the latter case, dup2 is not exactly equivalent to closeplus fcntl. The differences between them are as follows:

  1. dup2is an atomic operation, and closebetween fcntlthe two function calls, it is possible that the signal catching function is called between closeand , which may modify the file descriptor. fcntlThe same problem will occur if a different thread changes the file descriptor
  2. dup2and fcnt1there are some different errno's.

"UNIX Environment Programming"

Guess you like

Origin blog.csdn.net/no_say_you_know/article/details/125293042