复制文件描述符并独立搜索它们

Duplicating file descriptor and seeking through both of them independently

我有一个打开的文件描述符,我想复制它以便独立地对它们进行读取和查找。我看了

int dup(int old_fd)

系统调用。问题是它真的不适合这里。手册页说明如下 http://man7.org/linux/man-pages/man2/dup.2.html :

After a successful return, the old and new file descriptors may be used interchangeably. They refer to the same open file description (see open(2)) and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the file descriptors, the offset is also changed for the other.

有没有办法复制文件描述符使它们完全独立?

不——至少在 POSIX 定义的机制中没有。

如果你想完全独立于文件描述符,你需要避免共享打开的文件描述,这意味着一个独立的open()或等价物。

可能有一种 Linux 特定的机制可以完成我从未听说过的工作。但是,查看 http://man7.org/linux/man-pages/man2/ 处对 Linux 的系统调用并没有提供启发。

在 Linux 中,打开 /proc/<pid>/fd/<n> 会打开当前在 fd N 打开的文件,但这是一个新副本,而不是链接副本,例如你和 dup() 和朋友一起得到的那个。

这应该创建一个文件,其中包含 bar、一堆零字节,然后是 foo。与使用dup().

的版本对比
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(void)
{
    int fd1, fd2;
    char buffer[50];

    fd1 = open("testfile", O_CREAT | O_TRUNC | O_RDWR, 0600);
    sprintf(buffer, "/proc/self/fd/%d", fd1);
#ifndef USE_DUP
    fd2 = open(buffer, O_RDWR);
    if (fd2 == -1) {
        perror("open");
    }
#else
    fd2 = dup(fd1);
#endif
    if (lseek(fd1, 16, SEEK_SET) == -1) {
        perror("lseek");
    }
    if (write(fd1, "foo", 3) == -1) {
        perror("write(fd1)");
    }
    if (write(fd2, "bar", 3) == -1) {
        perror("write(fd2)");
    }
}